refac
This commit is contained in:
1 parent
9a8969ca93
commit
9717ada92f
3 files changed
+81
-59
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,62 +19,67 @@ depends_on = None
|
|||||||
|
|
||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
# Creating the 'knowledge' table
|
conn = op.get_bind()
|
||||||
print('Creating knowledge table')
|
inspector = sa.inspect(conn)
|
||||||
knowledge_table = op.create_table(
|
existing_tables = set(inspector.get_table_names())
|
||||||
'knowledge',
|
|
||||||
sa.Column('id', sa.Text(), primary_key=True),
|
|
||||||
sa.Column('user_id', sa.Text(), nullable=False),
|
|
||||||
sa.Column('name', sa.Text(), nullable=False),
|
|
||||||
sa.Column('description', sa.Text(), nullable=True),
|
|
||||||
sa.Column('data', sa.JSON(), nullable=True),
|
|
||||||
sa.Column('meta', sa.JSON(), nullable=True),
|
|
||||||
sa.Column('created_at', sa.BigInteger(), nullable=False),
|
|
||||||
sa.Column('updated_at', sa.BigInteger(), nullable=True),
|
|
||||||
)
|
|
||||||
|
|
||||||
print('Migrating data from document table to knowledge table')
|
if 'knowledge' not in existing_tables:
|
||||||
# Representation of the existing 'document' table
|
# Creating the 'knowledge' table
|
||||||
document_table = table(
|
print('Creating knowledge table')
|
||||||
'document',
|
knowledge_table = op.create_table(
|
||||||
column('collection_name', sa.String()),
|
'knowledge',
|
||||||
column('user_id', sa.String()),
|
sa.Column('id', sa.Text(), primary_key=True),
|
||||||
column('name', sa.String()),
|
sa.Column('user_id', sa.Text(), nullable=False),
|
||||||
column('title', sa.Text()),
|
sa.Column('name', sa.Text(), nullable=False),
|
||||||
column('content', sa.Text()),
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
column('timestamp', sa.BigInteger()),
|
sa.Column('data', sa.JSON(), nullable=True),
|
||||||
)
|
sa.Column('meta', sa.JSON(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.BigInteger(), nullable=False),
|
||||||
# Select all from existing document table
|
sa.Column('updated_at', sa.BigInteger(), nullable=True),
|
||||||
documents = op.get_bind().execute(
|
|
||||||
select(
|
|
||||||
document_table.c.collection_name,
|
|
||||||
document_table.c.user_id,
|
|
||||||
document_table.c.name,
|
|
||||||
document_table.c.title,
|
|
||||||
document_table.c.content,
|
|
||||||
document_table.c.timestamp,
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
# Insert data into knowledge table from document table
|
print('Migrating data from document table to knowledge table')
|
||||||
for doc in documents:
|
# Representation of the existing 'document' table
|
||||||
op.get_bind().execute(
|
document_table = table(
|
||||||
knowledge_table.insert().values(
|
'document',
|
||||||
id=doc.collection_name,
|
column('collection_name', sa.String()),
|
||||||
user_id=doc.user_id,
|
column('user_id', sa.String()),
|
||||||
description=doc.name,
|
column('name', sa.String()),
|
||||||
meta={
|
column('title', sa.Text()),
|
||||||
'legacy': True,
|
column('content', sa.Text()),
|
||||||
'document': True,
|
column('timestamp', sa.BigInteger()),
|
||||||
'tags': json.loads(doc.content or '{}').get('tags', []),
|
)
|
||||||
},
|
|
||||||
name=doc.title,
|
# Select all from existing document table
|
||||||
created_at=doc.timestamp,
|
documents = conn.execute(
|
||||||
updated_at=doc.timestamp, # using created_at for both created_at and updated_at in project
|
select(
|
||||||
|
document_table.c.collection_name,
|
||||||
|
document_table.c.user_id,
|
||||||
|
document_table.c.name,
|
||||||
|
document_table.c.title,
|
||||||
|
document_table.c.content,
|
||||||
|
document_table.c.timestamp,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Insert data into knowledge table from document table
|
||||||
|
for doc in documents:
|
||||||
|
conn.execute(
|
||||||
|
knowledge_table.insert().values(
|
||||||
|
id=doc.collection_name,
|
||||||
|
user_id=doc.user_id,
|
||||||
|
description=doc.name,
|
||||||
|
meta={
|
||||||
|
'legacy': True,
|
||||||
|
'document': True,
|
||||||
|
'tags': json.loads(doc.content or '{}').get('tags', []),
|
||||||
|
},
|
||||||
|
name=doc.title,
|
||||||
|
created_at=doc.timestamp,
|
||||||
|
updated_at=doc.timestamp,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
op.drop_table('knowledge')
|
op.drop_table('knowledge')
|
||||||
@@ -80,13 +80,18 @@ def upgrade() -> None:
|
|||||||
if table_name not in existing_tables:
|
if table_name not in existing_tables:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Query all rows
|
# Check if access_control and id columns exist (may already be dropped on re-run,
|
||||||
try:
|
# or table may have been rebuilt without id during intermediate migration states)
|
||||||
result = conn.execute(sa.text(f'SELECT id, access_control FROM "{table_name}"'))
|
insp = sa.inspect(conn)
|
||||||
rows = result.fetchall()
|
insp.clear_cache() # Ensure fresh metadata after prior migrations that rebuild tables
|
||||||
except Exception:
|
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
|
continue
|
||||||
|
|
||||||
|
# Query all rows
|
||||||
|
result = conn.execute(sa.text(f'SELECT id, access_control FROM "{table_name}"'))
|
||||||
|
rows = result.fetchall()
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
resource_id = row[0]
|
resource_id = row[0]
|
||||||
access_control_json = row[1]
|
access_control_json = row[1]
|
||||||
@@ -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:
|
||||||
|
|||||||
Reference in new issue
Block a user