more robust migrations

This commit is contained in:
2025-06-02 18:58:48 +02:00
parent 4dbaa27cba
commit 5c6c3f436e
51 changed files with 401 additions and 273 deletions

View File

@@ -19,11 +19,20 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('message')]
with op.batch_alter_table('message', schema=None) as batch_op: with op.batch_alter_table('message', schema=None) as batch_op:
if 'has_attachment' not in columns:
batch_op.add_column(sa.Column('has_attachment', sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column('has_attachment', sa.Boolean(), nullable=True))
if 'attachment_name' not in columns:
batch_op.add_column(sa.Column('attachment_name', sa.String(length=255), nullable=True)) batch_op.add_column(sa.Column('attachment_name', sa.String(length=255), nullable=True))
if 'attachment_path' not in columns:
batch_op.add_column(sa.Column('attachment_path', sa.String(length=512), nullable=True)) batch_op.add_column(sa.Column('attachment_path', sa.String(length=512), nullable=True))
if 'attachment_type' not in columns:
batch_op.add_column(sa.Column('attachment_type', sa.String(length=100), nullable=True)) batch_op.add_column(sa.Column('attachment_type', sa.String(length=100), nullable=True))
if 'attachment_size' not in columns:
batch_op.add_column(sa.Column('attachment_size', sa.Integer(), nullable=True)) batch_op.add_column(sa.Column('attachment_size', sa.Integer(), nullable=True))
# ### end Alembic commands ### # ### end Alembic commands ###
@@ -31,11 +40,20 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('message')]
with op.batch_alter_table('message', schema=None) as batch_op: with op.batch_alter_table('message', schema=None) as batch_op:
if 'attachment_size' in columns:
batch_op.drop_column('attachment_size') batch_op.drop_column('attachment_size')
if 'attachment_type' in columns:
batch_op.drop_column('attachment_type') batch_op.drop_column('attachment_type')
if 'attachment_path' in columns:
batch_op.drop_column('attachment_path') batch_op.drop_column('attachment_path')
if 'attachment_name' in columns:
batch_op.drop_column('attachment_name') batch_op.drop_column('attachment_name')
if 'has_attachment' in columns:
batch_op.drop_column('has_attachment') batch_op.drop_column('has_attachment')
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -19,7 +19,12 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('contact')]
with op.batch_alter_table('contact', schema=None) as batch_op: with op.batch_alter_table('contact', schema=None) as batch_op:
if 'is_admin' not in columns:
batch_op.add_column(sa.Column('is_admin', sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column('is_admin', sa.Boolean(), nullable=True))
# ### end Alembic commands ### # ### end Alembic commands ###
@@ -27,7 +32,12 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('contact')]
with op.batch_alter_table('contact', schema=None) as batch_op: with op.batch_alter_table('contact', schema=None) as batch_op:
if 'is_admin' in columns:
batch_op.drop_column('is_admin') batch_op.drop_column('is_admin')
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -38,6 +38,10 @@ def upgrade():
sa.ForeignKeyConstraint(['uploaded_by'], ['user.id'], ), sa.ForeignKeyConstraint(['uploaded_by'], ['user.id'], ),
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')
) )
# Check if preferred_view column exists before trying to drop it
columns = [col['name'] for col in inspector.get_columns('room_member_permissions')]
if 'preferred_view' in columns:
with op.batch_alter_table('room_member_permissions', schema=None) as batch_op: with op.batch_alter_table('room_member_permissions', schema=None) as batch_op:
batch_op.drop_column('preferred_view') batch_op.drop_column('preferred_view')

View File

@@ -19,19 +19,30 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('room_file')]
with op.batch_alter_table('room_file', schema=None) as batch_op: with op.batch_alter_table('room_file', schema=None) as batch_op:
if 'starred' not in columns:
batch_op.add_column(sa.Column('starred', sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column('starred', sa.Boolean(), nullable=True))
# Only alter columns if they exist
if 'path' in columns:
batch_op.alter_column('path', batch_op.alter_column('path',
existing_type=sa.VARCHAR(length=1024), existing_type=sa.VARCHAR(length=1024),
type_=sa.String(length=255), type_=sa.String(length=255),
existing_nullable=False) existing_nullable=False)
if 'size' in columns:
batch_op.alter_column('size', batch_op.alter_column('size',
existing_type=sa.BIGINT(), existing_type=sa.BIGINT(),
type_=sa.Integer(), type_=sa.Integer(),
existing_nullable=True) existing_nullable=True)
if 'uploaded_by' in columns:
batch_op.alter_column('uploaded_by', batch_op.alter_column('uploaded_by',
existing_type=sa.INTEGER(), existing_type=sa.INTEGER(),
nullable=True) nullable=True)
if 'uploaded_at' in columns:
batch_op.alter_column('uploaded_at', batch_op.alter_column('uploaded_at',
existing_type=postgresql.TIMESTAMP(), existing_type=postgresql.TIMESTAMP(),
nullable=True) nullable=True)
@@ -41,21 +52,30 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('room_file')]
with op.batch_alter_table('room_file', schema=None) as batch_op: with op.batch_alter_table('room_file', schema=None) as batch_op:
if 'uploaded_at' in columns:
batch_op.alter_column('uploaded_at', batch_op.alter_column('uploaded_at',
existing_type=postgresql.TIMESTAMP(), existing_type=postgresql.TIMESTAMP(),
nullable=False) nullable=False)
if 'uploaded_by' in columns:
batch_op.alter_column('uploaded_by', batch_op.alter_column('uploaded_by',
existing_type=sa.INTEGER(), existing_type=sa.INTEGER(),
nullable=False) nullable=False)
if 'size' in columns:
batch_op.alter_column('size', batch_op.alter_column('size',
existing_type=sa.Integer(), existing_type=sa.Integer(),
type_=sa.BIGINT(), type_=sa.BIGINT(),
existing_nullable=True) existing_nullable=True)
if 'path' in columns:
batch_op.alter_column('path', batch_op.alter_column('path',
existing_type=sa.String(length=255), existing_type=sa.String(length=255),
type_=sa.VARCHAR(length=1024), type_=sa.VARCHAR(length=1024),
existing_nullable=False) existing_nullable=False)
if 'starred' in columns:
batch_op.drop_column('starred') batch_op.drop_column('starred')
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -19,16 +19,30 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('site_settings')]
with op.batch_alter_table('site_settings', schema=None) as batch_op: with op.batch_alter_table('site_settings', schema=None) as batch_op:
if 'company_website' not in columns:
batch_op.add_column(sa.Column('company_website', sa.String(length=200), nullable=True)) batch_op.add_column(sa.Column('company_website', sa.String(length=200), nullable=True))
if 'company_email' not in columns:
batch_op.add_column(sa.Column('company_email', sa.String(length=100), nullable=True)) batch_op.add_column(sa.Column('company_email', sa.String(length=100), nullable=True))
if 'company_phone' not in columns:
batch_op.add_column(sa.Column('company_phone', sa.String(length=20), nullable=True)) batch_op.add_column(sa.Column('company_phone', sa.String(length=20), nullable=True))
if 'company_address' not in columns:
batch_op.add_column(sa.Column('company_address', sa.String(length=200), nullable=True)) batch_op.add_column(sa.Column('company_address', sa.String(length=200), nullable=True))
if 'company_city' not in columns:
batch_op.add_column(sa.Column('company_city', sa.String(length=100), nullable=True)) batch_op.add_column(sa.Column('company_city', sa.String(length=100), nullable=True))
if 'company_state' not in columns:
batch_op.add_column(sa.Column('company_state', sa.String(length=100), nullable=True)) batch_op.add_column(sa.Column('company_state', sa.String(length=100), nullable=True))
if 'company_zip' not in columns:
batch_op.add_column(sa.Column('company_zip', sa.String(length=20), nullable=True)) batch_op.add_column(sa.Column('company_zip', sa.String(length=20), nullable=True))
if 'company_country' not in columns:
batch_op.add_column(sa.Column('company_country', sa.String(length=100), nullable=True)) batch_op.add_column(sa.Column('company_country', sa.String(length=100), nullable=True))
if 'company_description' not in columns:
batch_op.add_column(sa.Column('company_description', sa.Text(), nullable=True)) batch_op.add_column(sa.Column('company_description', sa.Text(), nullable=True))
if 'company_industry' not in columns:
batch_op.add_column(sa.Column('company_industry', sa.String(length=100), nullable=True)) batch_op.add_column(sa.Column('company_industry', sa.String(length=100), nullable=True))
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -10,6 +10,7 @@ import sqlalchemy as sa
from sqlalchemy import inspect from sqlalchemy import inspect
from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = '9faab7ef6036' revision = '9faab7ef6036'
down_revision = 'ca9026520dad' down_revision = 'ca9026520dad'

View File

@@ -17,8 +17,14 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('room_file')]
with op.batch_alter_table('room_file', schema=None) as batch_op: with op.batch_alter_table('room_file', schema=None) as batch_op:
if 'deleted_by' not in columns:
batch_op.add_column(sa.Column('deleted_by', sa.Integer(), nullable=True)) batch_op.add_column(sa.Column('deleted_by', sa.Integer(), nullable=True))
if 'deleted_at' not in columns:
batch_op.add_column(sa.Column('deleted_at', sa.DateTime(), nullable=True)) batch_op.add_column(sa.Column('deleted_at', sa.DateTime(), nullable=True))
batch_op.create_foreign_key('fk_room_file_deleted_by_user', 'user', ['deleted_by'], ['id']) batch_op.create_foreign_key('fk_room_file_deleted_by_user', 'user', ['deleted_by'], ['id'])

View File

@@ -17,6 +17,11 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('room_file')]
if 'deleted' not in columns:
with op.batch_alter_table('room_file', schema=None) as batch_op: with op.batch_alter_table('room_file', schema=None) as batch_op:
batch_op.add_column(sa.Column('deleted', sa.Boolean(), nullable=False, server_default='false')) batch_op.add_column(sa.Column('deleted', sa.Boolean(), nullable=False, server_default='false'))

View File

@@ -18,6 +18,12 @@ depends_on = None
def upgrade(): def upgrade():
# Check if the column exists before adding it
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')]
if 'preferred_view' not in columns:
# Add preferred_view as nullable first # Add preferred_view as nullable first
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.add_column(sa.Column('preferred_view', sa.String(length=10), nullable=True)) batch_op.add_column(sa.Column('preferred_view', sa.String(length=10), nullable=True))

View File

@@ -19,9 +19,16 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('room_member_permissions')]
with op.batch_alter_table('room_member_permissions', schema=None) as batch_op: with op.batch_alter_table('room_member_permissions', schema=None) as batch_op:
if 'can_download' not in columns:
batch_op.add_column(sa.Column('can_download', sa.Boolean(), nullable=False, server_default=sa.false())) batch_op.add_column(sa.Column('can_download', sa.Boolean(), nullable=False, server_default=sa.false()))
if 'can_rename' not in columns:
batch_op.add_column(sa.Column('can_rename', sa.Boolean(), nullable=False, server_default=sa.false())) batch_op.add_column(sa.Column('can_rename', sa.Boolean(), nullable=False, server_default=sa.false()))
if 'can_move' not in columns:
batch_op.add_column(sa.Column('can_move', sa.Boolean(), nullable=False, server_default=sa.false())) batch_op.add_column(sa.Column('can_move', sa.Boolean(), nullable=False, server_default=sa.false()))
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -19,11 +19,14 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.drop_table('contact')
conn = op.get_bind() conn = op.get_bind()
inspector = inspect(conn) inspector = inspect(conn)
tables = inspector.get_table_names()
columns = [col['name'] for col in inspector.get_columns('user')] columns = [col['name'] for col in inspector.get_columns('user')]
if 'contact' in tables:
op.drop_table('contact')
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
if 'profile_picture' not in columns: if 'profile_picture' not in columns:
batch_op.add_column(sa.Column('profile_picture', sa.String(length=255), nullable=True)) batch_op.add_column(sa.Column('profile_picture', sa.String(length=255), nullable=True))
@@ -33,7 +36,12 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')]
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
if 'profile_picture' in columns:
batch_op.drop_column('profile_picture') batch_op.drop_column('profile_picture')
op.create_table('contact', op.create_table('contact',

View File

@@ -32,7 +32,12 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')]
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
if 'last_name' in columns:
batch_op.drop_column('last_name') batch_op.drop_column('last_name')
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -19,6 +19,11 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')]
if 'password_hash' in columns:
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.alter_column('password_hash', batch_op.alter_column('password_hash',
existing_type=sa.VARCHAR(length=128), existing_type=sa.VARCHAR(length=128),
@@ -30,6 +35,11 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')]
if 'password_hash' in columns:
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.alter_column('password_hash', batch_op.alter_column('password_hash',
existing_type=sa.String(length=256), existing_type=sa.String(length=256),

View File

@@ -19,6 +19,11 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
tables = inspector.get_table_names()
if 'contact' not in tables:
op.create_table('contact', op.create_table('contact',
sa.Column('id', sa.Integer(), nullable=False), sa.Column('id', sa.Integer(), nullable=False),
sa.Column('first_name', sa.String(length=100), nullable=False), sa.Column('first_name', sa.String(length=100), nullable=False),
@@ -38,8 +43,6 @@ def upgrade():
) )
# Check if columns exist before adding them # Check if columns exist before adding them
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')] columns = [col['name'] for col in inspector.get_columns('user')]
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
@@ -53,8 +56,14 @@ def upgrade():
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('user')]
with op.batch_alter_table('user', schema=None) as batch_op: with op.batch_alter_table('user', schema=None) as batch_op:
if 'created_at' in columns:
batch_op.drop_column('created_at') batch_op.drop_column('created_at')
if 'is_admin' in columns:
batch_op.drop_column('is_admin') batch_op.drop_column('is_admin')
op.drop_table('contact') op.drop_table('contact')

View File

@@ -19,6 +19,11 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
inspector = inspect(conn)
columns = [col['name'] for col in inspector.get_columns('site_settings')]
if 'company_logo' not in columns:
with op.batch_alter_table('site_settings', schema=None) as batch_op: with op.batch_alter_table('site_settings', schema=None) as batch_op:
batch_op.add_column(sa.Column('company_logo', sa.String(length=255), nullable=True)) batch_op.add_column(sa.Column('company_logo', sa.String(length=255), nullable=True))