Introduction of Partner Model, adding code to Tenant model
This commit is contained in:
BIN
migrations/.DS_Store
vendored
BIN
migrations/.DS_Store
vendored
Binary file not shown.
BIN
migrations/public/.DS_Store
vendored
BIN
migrations/public/.DS_Store
vendored
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,82 @@
|
||||
"""Initialize empty Tenant codes
|
||||
|
||||
Revision ID: 867deef0888b
|
||||
Revises: cab899dbb213
|
||||
Create Date: 2025-04-03 09:01:20.446536
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import uuid
|
||||
from sqlalchemy.sql import table, column, select, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '867deef0888b'
|
||||
down_revision = 'cab899dbb213'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Create a reference to the tenant table
|
||||
tenant_table = table('tenant',
|
||||
column('id', sa.Integer),
|
||||
column('code', sa.String(50)),
|
||||
schema='public' # Assuming the table is in the 'public' schema
|
||||
)
|
||||
|
||||
# Get a connection
|
||||
connection = op.get_bind()
|
||||
session = Session(bind=connection)
|
||||
|
||||
try:
|
||||
# Find all tenants with empty or null code
|
||||
# Note the updated select syntax for SQLAlchemy 2.0
|
||||
query = select(tenant_table.c.id).where(
|
||||
or_(
|
||||
tenant_table.c.code == None,
|
||||
tenant_table.c.code == ''
|
||||
)
|
||||
)
|
||||
|
||||
results = connection.execute(query)
|
||||
|
||||
# Update each tenant with a UUID-based code
|
||||
for row in results:
|
||||
tenant_id = row[0]
|
||||
code = f"TENANT-{str(uuid.uuid4())}"
|
||||
|
||||
# Update the tenant record
|
||||
update_stmt = tenant_table.update().where(
|
||||
tenant_table.c.id == tenant_id
|
||||
).values(
|
||||
code=code
|
||||
)
|
||||
|
||||
connection.execute(update_stmt)
|
||||
|
||||
# Commit changes
|
||||
session.commit()
|
||||
|
||||
# Log how many records were updated
|
||||
count_query = select(sa.func.count()).select_from(tenant_table).where(
|
||||
tenant_table.c.code.like('TENANT-%')
|
||||
)
|
||||
|
||||
updated_count = connection.execute(count_query).scalar()
|
||||
|
||||
print(f"Updated {updated_count} tenant records with UUID-based codes")
|
||||
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
print(f"Error updating tenant codes: {str(e)}")
|
||||
raise e
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
def downgrade():
|
||||
# No downgrade needed for this data migration
|
||||
pass
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Add Partner Models
|
||||
|
||||
Revision ID: 98adf66ce189
|
||||
Revises: 03a1e7633c01
|
||||
Create Date: 2025-03-31 14:43:06.833648
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '98adf66ce189'
|
||||
down_revision = '03a1e7633c01'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('partner',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('tenant_id', sa.Integer(), nullable=False),
|
||||
sa.Column('code', sa.String(length=50), nullable=False),
|
||||
sa.Column('logo_url', sa.String(length=255), nullable=True),
|
||||
sa.Column('active', sa.Boolean(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('created_by', sa.Integer(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('updated_by', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ),
|
||||
sa.ForeignKeyConstraint(['tenant_id'], ['public.tenant.id'], ),
|
||||
sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('code'),
|
||||
sa.UniqueConstraint('tenant_id'),
|
||||
schema='public'
|
||||
)
|
||||
op.create_table('partner_service',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('partner_id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=50), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('type', sa.String(length=50), nullable=False),
|
||||
sa.Column('type_version', sa.String(length=20), nullable=False),
|
||||
sa.Column('active', sa.Boolean(), nullable=True),
|
||||
sa.Column('configuration', sa.JSON(), nullable=True),
|
||||
sa.Column('system_metadata', sa.JSON(), nullable=True),
|
||||
sa.Column('user_metadata', sa.JSON(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('created_by', sa.Integer(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('updated_by', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ),
|
||||
sa.ForeignKeyConstraint(['partner_id'], ['public.partner.id'], ),
|
||||
sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
schema='public'
|
||||
)
|
||||
op.create_table('partner_tenant',
|
||||
sa.Column('partner_service_id', sa.Integer(), nullable=False),
|
||||
sa.Column('tenant_id', sa.Integer(), nullable=False),
|
||||
sa.Column('relationship_type', sa.String(length=20), nullable=False),
|
||||
sa.Column('configuration', sa.JSON(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('created_by', sa.Integer(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('updated_by', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ),
|
||||
sa.ForeignKeyConstraint(['partner_service_id'], ['public.partner_service.id'], ),
|
||||
sa.ForeignKeyConstraint(['tenant_id'], ['public.tenant.id'], ),
|
||||
sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ),
|
||||
sa.PrimaryKeyConstraint('partner_service_id', 'tenant_id'),
|
||||
schema='public'
|
||||
)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
|
||||
op.drop_table('partner_tenant', schema='public')
|
||||
op.drop_table('partner_service', schema='public')
|
||||
op.drop_table('partner', schema='public')
|
||||
# ### end Alembic commands ###
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,34 @@
|
||||
"""Add code to Tenant
|
||||
|
||||
Revision ID: cab899dbb213
|
||||
Revises: 98adf66ce189
|
||||
Create Date: 2025-04-02 16:08:06.597183
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'cab899dbb213'
|
||||
down_revision = '98adf66ce189'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('tenant', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('code', sa.String(length=50), nullable=True))
|
||||
batch_op.create_unique_constraint(None, ['code'])
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('tenant', schema=None) as batch_op:
|
||||
batch_op.drop_constraint(None, type_='unique')
|
||||
batch_op.drop_column('code')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
BIN
migrations/tenant/.DS_Store
vendored
BIN
migrations/tenant/.DS_Store
vendored
Binary file not shown.
Binary file not shown.
BIN
migrations/tenant/versions/.DS_Store
vendored
BIN
migrations/tenant/versions/.DS_Store
vendored
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user