This commit is contained in:
Timothy Jaeryang Baek committed 2026-07-01 02:53:42 -05:00
1 parent 4b08d65597
commit c416c6cad6
1 file changed
+9 -7
@@ -72,7 +72,6 @@ def _convert_column_to_json(table: str, column: str):
dialect = conn.dialect.name dialect = conn.dialect.name
t = sa.table(table, sa.column('id', sa.Text), sa.column(column, sa.Text)) t = sa.table(table, sa.column('id', sa.Text), sa.column(column, sa.Text))
t_json = sa.column(f'{column}_json', sa.JSON)
# SQLite cannot ALTER COLUMN → must recreate column # SQLite cannot ALTER COLUMN → must recreate column
if dialect == 'sqlite': if dialect == 'sqlite':
@@ -90,9 +89,11 @@ def _convert_column_to_json(table: str, column: str):
parsed = None parsed = None
conn.execute( conn.execute(
sa.update(sa.table(table, sa.column('id'), t_json)) sa.update(
sa.table(table, sa.column('id'), sa.column(f'{column}_json', sa.JSON))
)
.where(sa.column('id') == uid) .where(sa.column('id') == uid)
.values({f'{column}_json': json.dumps(parsed) if parsed else None}) .values({f'{column}_json': parsed})
) )
op.drop_column(table, column) op.drop_column(table, column)
@@ -112,8 +113,7 @@ def _convert_column_to_text(table: str, column: str):
conn = op.get_bind() conn = op.get_bind()
dialect = conn.dialect.name dialect = conn.dialect.name
t = sa.table(table, sa.column('id', sa.Text), sa.column(column)) t = sa.table(table, sa.column('id', sa.Text), sa.column(column, sa.JSON))
t_text = sa.column(f'{column}_text', sa.Text)
if dialect == 'sqlite': if dialect == 'sqlite':
op.add_column(table, sa.Column(f'{column}_text', sa.Text(), nullable=True)) op.add_column(table, sa.Column(f'{column}_text', sa.Text(), nullable=True))
@@ -122,9 +122,11 @@ def _convert_column_to_text(table: str, column: str):
for uid, raw in rows: for uid, raw in rows:
conn.execute( conn.execute(
sa.update(sa.table(table, sa.column('id'), t_text)) sa.update(
sa.table(table, sa.column('id'), sa.column(f'{column}_text', sa.Text))
)
.where(sa.column('id') == uid) .where(sa.column('id') == uid)
.values({f'{column}_text': json.dumps(raw) if raw else None}) .values({f'{column}_text': json.dumps(raw) if raw is not None else None})
) )
op.drop_column(table, column) op.drop_column(table, column)