Saltar a contenido

0036 - log_ubicaciones_promotores

"""create table fuerza_ventas.log_ubicaciones_promotores

Revision ID: 0036
Revises: 0035
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "0036"
down_revision: Union[str, Sequence[str], None] = "0035"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

def upgrade() -> None:
    op.create_table(
        'log_ubicaciones_promotores',
        sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
        sa.Column(
            "empresa_id",
            sa.BigInteger(), nullable=False),
        sa.Column('promotor_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('latitud', sa.Numeric(precision=10, scale=7), nullable=False),
        sa.Column('longitud', sa.Numeric(precision=10, scale=7), nullable=False),
        sa.Column('precision_gps_metros', sa.Numeric(precision=6, scale=2), nullable=True),
        sa.Column('bateria_porcentaje', sa.Integer(), nullable=True),
        sa.Column('is_en_movimiento', sa.Boolean(), nullable=True),
        sa.Column('fecha_hora_dispositivo', sa.DateTime(timezone=True), nullable=False),
        sa.Column('is_activo', sa.Boolean(), server_default=sa.text('true'), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('created_by', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('updated_by', postgresql.UUID(as_uuid=True), nullable=True),
        sa.PrimaryKeyConstraint('id', name='pk_log_ubicaciones_promotores'),
        sa.UniqueConstraint('empresa_id', 'id', name='uq_log_ubicaciones_promotores_empresa_id'),
        sa.ForeignKeyConstraint(['empresa_id', 'promotor_id'], ['fuerza_ventas.mae_promotores.empresa_id', 'fuerza_ventas.mae_promotores.id'], ondelete='RESTRICT', name='fk_log_ubicaciones_promotores_promotor_id'),
        schema='fuerza_ventas'
    )
    op.create_index('ix_log_ubicaciones_promotores_empresa_promotor', 'log_ubicaciones_promotores', ['empresa_id', 'promotor_id'], unique=False, schema='fuerza_ventas')
    op.create_index('ix_log_ubicaciones_promotores_empresa_activo', 'log_ubicaciones_promotores', ['empresa_id', 'is_activo'], unique=False, schema='fuerza_ventas')
    op.execute('ALTER TABLE fuerza_ventas.log_ubicaciones_promotores ENABLE ROW LEVEL SECURITY')
    op.execute('''CREATE POLICY rls_log_ubicaciones_promotores_tenant_isolation ON fuerza_ventas.log_ubicaciones_promotores FOR ALL USING (empresa_id = current_setting(\'app.current_empresa_id\', true)::BIGINT) WITH CHECK (empresa_id = current_setting(\'app.current_empresa_id\', true)::BIGINT)''')

def downgrade() -> None:
    op.execute('DROP POLICY IF EXISTS rls_log_ubicaciones_promotores_tenant_isolation ON fuerza_ventas.log_ubicaciones_promotores')
    op.drop_index('ix_log_ubicaciones_promotores_empresa_promotor', table_name='log_ubicaciones_promotores', schema='fuerza_ventas')
    op.drop_index('ix_log_ubicaciones_promotores_empresa_activo', table_name='log_ubicaciones_promotores', schema='fuerza_ventas')
    op.drop_table('log_ubicaciones_promotores', schema='fuerza_ventas')