6a39f3d8e55c_add_project_table.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Add project table
  2. Revision ID: 6a39f3d8e55c
  3. Revises: c0fbf31ca0db
  4. Create Date: 2024-10-01 14:02:35.241684
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. from sqlalchemy.sql import table, column, select
  9. revision = "6a39f3d8e55c"
  10. down_revision = "c0fbf31ca0db"
  11. branch_labels = None
  12. depends_on = None
  13. def upgrade():
  14. # Creating the 'project' table
  15. print("Creating project table")
  16. project_table = op.create_table(
  17. "project",
  18. sa.Column("id", sa.Text(), primary_key=True),
  19. sa.Column("user_id", sa.Text(), nullable=False),
  20. sa.Column("name", sa.Text(), nullable=False),
  21. sa.Column("description", sa.Text(), nullable=True),
  22. sa.Column("data", sa.JSON(), nullable=True),
  23. sa.Column("meta", sa.JSON(), nullable=True),
  24. sa.Column("created_at", sa.BigInteger(), nullable=False),
  25. sa.Column("updated_at", sa.BigInteger(), nullable=True),
  26. )
  27. print("Migrating data from document table to project table")
  28. # Representation of the existing 'document' table
  29. document_table = table(
  30. "document",
  31. column("collection_name", sa.String()),
  32. column("user_id", sa.String()),
  33. column("name", sa.String()),
  34. column("title", sa.Text()),
  35. column("timestamp", sa.BigInteger()),
  36. )
  37. # Select all from existing document table
  38. documents = op.get_bind().execute(
  39. select(
  40. document_table.c.collection_name,
  41. document_table.c.user_id,
  42. document_table.c.name,
  43. document_table.c.title,
  44. document_table.c.timestamp,
  45. )
  46. )
  47. # Insert data into project table from document table
  48. for doc in documents:
  49. op.get_bind().execute(
  50. project_table.insert().values(
  51. id=doc.collection_name,
  52. user_id=doc.user_id,
  53. description=doc.name,
  54. meta={
  55. "legacy": True,
  56. },
  57. name=doc.title,
  58. created_at=doc.timestamp,
  59. updated_at=doc.timestamp, # using created_at for both created_at and updated_at in project
  60. )
  61. )
  62. def downgrade():
  63. op.drop_table("project")