This commit is contained in:
Timothy Jaeryang Baek committed 2026-05-14 14:05:28 +09:00
1 parent 9a8969ca93
commit 9717ada92f
3 files changed
+32 -10

No files matched your search

@@ -16,6 +16,18 @@ depends_on = None
def upgrade(): def upgrade():
conn = op.get_bind()
inspector = sa.inspect(conn)
columns = {c['name']: c for c in inspector.get_columns('folder')}
created_at_col = columns.get('created_at')
if not created_at_col:
return
# Only convert if still DateTime — skip if already BigInteger
if isinstance(created_at_col['type'], sa.BigInteger):
return
# Perform safe alterations using batch operation # Perform safe alterations using batch operation
with op.batch_alter_table('folder', schema=None) as batch_op: with op.batch_alter_table('folder', schema=None) as batch_op:
# Step 1: Remove server defaults for created_at and updated_at # Step 1: Remove server defaults for created_at and updated_at
@@ -19,6 +19,11 @@ depends_on = None
def upgrade(): def upgrade():
conn = op.get_bind()
inspector = sa.inspect(conn)
existing_tables = set(inspector.get_table_names())
if 'knowledge' not in existing_tables:
# Creating the 'knowledge' table # Creating the 'knowledge' table
print('Creating knowledge table') print('Creating knowledge table')
knowledge_table = op.create_table( knowledge_table = op.create_table(
@@ -46,7 +51,7 @@ def upgrade():
) )
# Select all from existing document table # Select all from existing document table
documents = op.get_bind().execute( documents = conn.execute(
select( select(
document_table.c.collection_name, document_table.c.collection_name,
document_table.c.user_id, document_table.c.user_id,
@@ -59,7 +64,7 @@ def upgrade():
# Insert data into knowledge table from document table # Insert data into knowledge table from document table
for doc in documents: for doc in documents:
op.get_bind().execute( conn.execute(
knowledge_table.insert().values( knowledge_table.insert().values(
id=doc.collection_name, id=doc.collection_name,
user_id=doc.user_id, user_id=doc.user_id,
@@ -71,7 +76,7 @@ def upgrade():
}, },
name=doc.title, name=doc.title,
created_at=doc.timestamp, created_at=doc.timestamp,
updated_at=doc.timestamp, # using created_at for both created_at and updated_at in project updated_at=doc.timestamp,
) )
) )
@@ -80,12 +80,17 @@ def upgrade() -> None:
if table_name not in existing_tables: if table_name not in existing_tables:
continue continue
# Check if access_control and id columns exist (may already be dropped on re-run,
# or table may have been rebuilt without id during intermediate migration states)
insp = sa.inspect(conn)
insp.clear_cache() # Ensure fresh metadata after prior migrations that rebuild tables
table_cols = {c['name'] for c in insp.get_columns(table_name)}
if 'access_control' not in table_cols or 'id' not in table_cols:
continue
# Query all rows # Query all rows
try:
result = conn.execute(sa.text(f'SELECT id, access_control FROM "{table_name}"')) result = conn.execute(sa.text(f'SELECT id, access_control FROM "{table_name}"'))
rows = result.fetchall() rows = result.fetchall()
except Exception:
continue
for row in rows: for row in rows:
resource_id = row[0] resource_id = row[0]
@@ -207,15 +212,15 @@ def upgrade() -> None:
except Exception: except Exception:
pass pass
# Drop access_control columns from resource tables # Drop access_control columns from resource tables (only if column still exists)
inspector = sa.inspect(conn)
for table_name, _ in resource_tables: for table_name, _ in resource_tables:
if table_name not in existing_tables: if table_name not in existing_tables:
continue continue
try: cols = {c['name'] for c in inspector.get_columns(table_name)}
if 'access_control' in cols:
with op.batch_alter_table(table_name) as batch: with op.batch_alter_table(table_name) as batch:
batch.drop_column('access_control') batch.drop_column('access_control')
except Exception:
pass
def downgrade() -> None: def downgrade() -> None: