refactor: Update foreign key constraints and primary key definitions in ORM models

This commit is contained in:
2025-11-07 12:55:04 -06:00
parent ef3924a41d
commit 6e59eee82e
4 changed files with 12 additions and 81 deletions

View File

@@ -21,26 +21,6 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('tenants',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('slug', sa.String(length=100), nullable=False),
sa.Column('type', sa.Enum('SHARED', 'DEDICATED', name='tenanttype'), nullable=False),
sa.Column('keycloak_realm', sa.String(length=255), nullable=False),
sa.Column('db_config', sa.Text(), nullable=True),
sa.Column('contact_name', sa.String(length=255), nullable=True),
sa.Column('contact_email', sa.String(length=255), nullable=True),
sa.Column('contact_phone', sa.String(length=50), nullable=True),
sa.Column('is_active', sa.Boolean(), 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.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('keycloak_realm'),
schema='a76'
)
op.create_index(op.f('ix_a76_tenants_id'), 'tenants', ['id'], unique=False, schema='a76')
op.create_index(op.f('ix_a76_tenants_name'), 'tenants', ['name'], unique=False, schema='a76')
op.create_index(op.f('ix_a76_tenants_slug'), 'tenants', ['slug'], unique=True, schema='a76')
op.create_table('containers',
sa.Column('key', sa.String(length=3), nullable=False),
sa.Column('description', sa.String(length=500), nullable=False),
@@ -149,46 +129,7 @@ def upgrade() -> None:
sa.Column('description', sa.String(length=200), nullable=False),
sa.PrimaryKeyConstraint('key', name='valuation_methods_pkey'),
schema='public'
)
op.create_table('license_usage',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('tenant_id', sa.Integer(), nullable=False),
sa.Column('period_start', sa.DateTime(timezone=True), nullable=False),
sa.Column('period_end', sa.DateTime(timezone=True), nullable=False),
sa.Column('active_users', sa.Integer(), nullable=True),
sa.Column('storage_used_gb', sa.Integer(), nullable=True),
sa.Column('operations_count', sa.Integer(), nullable=True),
sa.Column('api_calls_count', sa.Integer(), nullable=True),
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.ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
schema='a76'
)
op.create_index(op.f('ix_a76_license_usage_id'), 'license_usage', ['id'], unique=False, schema='a76')
op.create_index(op.f('ix_a76_license_usage_tenant_id'), 'license_usage', ['tenant_id'], unique=False, schema='a76')
op.create_table('licenses',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('tenant_id', sa.Integer(), nullable=False),
sa.Column('plan', sa.Enum('FREE', 'BASIC', 'PROFESSIONAL', 'ENTERPRISE', name='licenseplan'), nullable=False),
sa.Column('status', sa.Enum('ACTIVE', 'EXPIRED', 'SUSPENDED', 'PENDING', 'CANCELLED', name='licensestatus'), nullable=False),
sa.Column('max_users', sa.Integer(), nullable=False),
sa.Column('max_storage_gb', sa.Integer(), nullable=False),
sa.Column('max_monthly_operations', sa.Integer(), nullable=False),
sa.Column('feature_api_access', sa.Boolean(), nullable=True),
sa.Column('feature_advanced_reports', sa.Boolean(), nullable=True),
sa.Column('feature_integrations', sa.Boolean(), nullable=True),
sa.Column('feature_dedicated_support', sa.Boolean(), nullable=True),
sa.Column('starts_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('expires_at', sa.DateTime(timezone=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.ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
schema='a76'
)
op.create_index(op.f('ix_a76_licenses_id'), 'licenses', ['id'], unique=False, schema='a76')
op.create_index(op.f('ix_a76_licenses_tenant_id'), 'licenses', ['tenant_id'], unique=True, schema='a76')
)
op.create_table('code_pedimento_regimens',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('pedimento_code', sa.String(length=3), nullable=False),
@@ -206,12 +147,6 @@ def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('code_pedimento_regimens', schema='public')
op.drop_index(op.f('ix_a76_licenses_tenant_id'), table_name='licenses', schema='a76')
op.drop_index(op.f('ix_a76_licenses_id'), table_name='licenses', schema='a76')
op.drop_table('licenses', schema='a76')
op.drop_index(op.f('ix_a76_license_usage_tenant_id'), table_name='license_usage', schema='a76')
op.drop_index(op.f('ix_a76_license_usage_id'), table_name='license_usage', schema='a76')
op.drop_table('license_usage', schema='a76')
op.drop_table('valuation_methods', schema='public')
op.drop_table('transport_types', schema='public')
op.drop_table('transport_modes', schema='public')
@@ -229,8 +164,4 @@ def downgrade() -> None:
op.drop_index('ak_country_ame', table_name='countries', schema='public')
op.drop_table('countries', schema='public')
op.drop_table('containers', schema='public')
op.drop_index(op.f('ix_a76_tenants_slug'), table_name='tenants', schema='a76')
op.drop_index(op.f('ix_a76_tenants_name'), table_name='tenants', schema='a76')
op.drop_index(op.f('ix_a76_tenants_id'), table_name='tenants', schema='a76')
op.drop_table('tenants', schema='a76')
# ### end Alembic commands ###

View File

@@ -14,13 +14,13 @@ class ClientProvider(Base):
"""
__tablename__ = "client_provider"
__table_args__ = (
PrimaryKeyConstraint('client_id', name='client_provider_pkey'),
PrimaryKeyConstraint('id', name='client_provider_pkey'),
ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], name='fk_client_provider_tenant'),
{"schema": "a76"}
)
# Primary key
client_id: Mapped[int] = mapped_column(Integer, primary_key=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
# Basic information
type_nat_foreign: Mapped[Optional[str]] = mapped_column(String(1)) # TIPO NACIONAL/EXTRANJERO
@@ -53,13 +53,13 @@ class ClientProviderAddress(Base):
__table_args__ = (
PrimaryKeyConstraint('id', name='client_provider_address_pkey'),
ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], name='fk_client_provider_address_tenant'),
ForeignKeyConstraint(['client_id'], ['a76.client_provider.client_id'], ondelete='CASCADE', name='fk_client_provider_address_client'),
ForeignKeyConstraint(['client_id'], ['a76.client_provider.id'], ondelete='CASCADE', name='fk_client_provider_address_client'),
{"schema": "a76"}
)
# Primary key (foreign key)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
client_id: Mapped[int] = mapped_column(Integer, ForeignKey('a76.client_provider.client_id', ondelete='CASCADE'))
client_id: Mapped[int] = mapped_column(Integer, ForeignKey('a76.client_provider.id', ondelete='CASCADE'))
tenant_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True)
@@ -91,13 +91,13 @@ class ClientProviderPrograms(Base):
__table_args__ = (
PrimaryKeyConstraint('id', name='client_provider_programs_pkey'),
ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], name='fk_client_provider_programs_tenant'),
ForeignKeyConstraint(['client_id'], ['a76.client_provider.client_id'], ondelete='CASCADE', name='fk_client_provider_programs_client'),
ForeignKeyConstraint(['client_id'], ['a76.client_provider.id'], ondelete='CASCADE', name='fk_client_provider_programs_client'),
{"schema": "a76"}
)
# Primary key (foreign key)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
client_id: Mapped[int] = mapped_column(Integer, ForeignKey('a76.client_provider.client_id', ondelete='CASCADE'))
client_id: Mapped[int] = mapped_column(Integer, ForeignKey('a76.client_provider.id', ondelete='CASCADE'))
tenant_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True)

View File

@@ -26,10 +26,10 @@ if TYPE_CHECKING:
class Pedimentos(Base):
__tablename__ = 'pedimentos'
__table_args__ = (
ForeignKeyConstraint(['client_id'], ['public.client_and_providers.id']),
ForeignKeyConstraint(['regime'], ['public.pedimento_regimens.code']),
ForeignKeyConstraint(['pedimento_code'], ['public.pedimento_codes.code']),
ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id']),
ForeignKeyConstraint(['client_id'], ['a76.client_provider.id'], name='fk_pedimentos_client'),
ForeignKeyConstraint(['regime'], ['public.pedimento_regimens.code'], name='fk_pedimentos_regime'),
ForeignKeyConstraint(['pedimento_code'], ['public.pedimento_codes.code'], name='fk_pedimentos_code'),
ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], name='fk_pedimentos_tenant'),
PrimaryKeyConstraint('id', name='pedimentos_pkey'),
Index('idx_pedimentos_client_id', 'client_id'),
Index('idx_pedimentos_created_at', 'created_at'),

View File

@@ -31,7 +31,7 @@ class Tenant(Base):
type = Column(SQLEnum(TenantType), default=TenantType.SHARED, nullable=False)
# Keycloak realm asociado
keycloak_realm = Column(String(255), unique=True, nullable=False)
keycloak_realm = Column(String(255), nullable=False)
# Configuración de BD dedicada (JSON string o NULL si usa BD compartida)
db_config = Column(Text, nullable=True) # JSON: {host, port, name, user, password}