Skip to content

Commit

Permalink
fix: fix_mysql_collations migration more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintShit committed Feb 16, 2024
1 parent c03e59d commit c0984cb
Showing 1 changed file with 51 additions and 58 deletions.
109 changes: 51 additions & 58 deletions app/db/migrations/versions/dd725e4d3628_fix_mysql_collations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@

def upgrade() -> None:
bind = op.get_bind()
metadata = sa.MetaData(bind=bind.engine)
metadata.reflect()

if bind.engine.name == 'mysql':

constraints = []
for table_name, table in metadata.tables.items():
for constraint in table.foreign_key_constraints:
if (
isinstance(constraint, sa.sql.schema.ForeignKeyConstraint)
and constraint.referred_table.name == 'inbounds'
):
for element in constraint.elements:
if element.column.name == 'tag':
constraints.append(constraint)

op.alter_column('nodes', 'name', type_=sa.String(length=256, collation='utf8mb4_bin'), nullable=True)
op.alter_column('users', 'username', type_=sa.String(length=34, collation='utf8mb4_bin'), nullable=True)

op.drop_constraint(
'template_inbounds_association_ibfk_1',
'template_inbounds_association',
type_='foreignkey'
)
op.drop_constraint(
'hosts_ibfk_1',
'hosts',
type_='foreignkey'
)
op.drop_constraint(
'exclude_inbounds_association_ibfk_1',
'exclude_inbounds_association',
type_='foreignkey')
for cons in constraints:
op.drop_constraint(
cons.name,
cons.table.name,
type_='foreignkey'
)

op.alter_column(
'inbounds', 'tag',
Expand All @@ -60,44 +65,41 @@ def upgrade() -> None:
nullable=False
)

op.create_foreign_key(
'template_inbounds_association_ibfk_1',
'template_inbounds_association', 'inbounds',
['inbound_tag'], ['tag'],
)
op.create_foreign_key(
'hosts_ibfk_1',
'hosts', 'inbounds',
['inbound_tag'], ['tag'],
)
op.create_foreign_key(
'exclude_inbounds_association_ibfk_1',
'exclude_inbounds_association', 'inbounds',
['inbound_tag'], ['tag'],
)
for cons in constraints:
op.create_foreign_key(
cons.name,
cons.table.name, cons.referred_table.name,
[c.name for c in cons.columns], ['tag'],
)


def downgrade() -> None:
bind = op.get_bind()
metadata = sa.MetaData(bind=bind.engine)
metadata.reflect()

if bind.engine.name == 'mysql':

constraints = []
for table_name, table in metadata.tables.items():
for constraint in table.foreign_key_constraints:
if (
isinstance(constraint, sa.sql.schema.ForeignKeyConstraint)
and constraint.referred_table.name == 'inbounds'
):
for element in constraint.elements:
if element.column.name == 'tag':
constraints.append(constraint)

op.alter_column('nodes', 'name', type_=sa.String(length=256, collation='utf8mb4_general_ci'), nullable=True)
op.alter_column('users', 'username', type_=sa.String(length=34, collation='utf8mb4_general_ci'), nullable=True)

op.drop_constraint(
'template_inbounds_association_ibfk_1',
'template_inbounds_association',
type_='foreignkey'
)
op.drop_constraint(
'hosts_ibfk_1',
'hosts',
type_='foreignkey'
)
op.drop_constraint(
'exclude_inbounds_association_ibfk_1',
'exclude_inbounds_association',
type_='foreignkey')
for cons in constraints:
op.drop_constraint(
cons.name,
cons.table.name,
type_='foreignkey'
)

op.alter_column(
'inbounds', 'tag',
Expand All @@ -120,18 +122,9 @@ def downgrade() -> None:
nullable=False
)

op.create_foreign_key(
'template_inbounds_association_ibfk_1',
'template_inbounds_association', 'inbounds',
['inbound_tag'], ['tag'],
)
op.create_foreign_key(
'hosts_ibfk_1',
'hosts', 'inbounds',
['inbound_tag'], ['tag'],
)
op.create_foreign_key(
'exclude_inbounds_association_ibfk_1',
'exclude_inbounds_association', 'inbounds',
['inbound_tag'], ['tag'],
)
for cons in constraints:
op.create_foreign_key(
cons.name,
cons.table.name, cons.referred_table.name,
[c.name for c in cons.columns], ['tag'],
)

0 comments on commit c0984cb

Please sign in to comment.