7aa055089aea_initial_migration_create_all_tables.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """Initial migration - create all tables
  2. Revision ID: 7aa055089aea
  3. Revises:
  4. Create Date: 2026-01-02 00:45:00.414032
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. # revision identifiers, used by Alembic.
  9. revision = '7aa055089aea'
  10. down_revision = None
  11. branch_labels = None
  12. depends_on = None
  13. def upgrade():
  14. # ### commands auto generated by Alembic - please adjust! ###
  15. op.create_table('aws_credentials',
  16. sa.Column('id', sa.Integer(), nullable=False),
  17. sa.Column('name', sa.String(length=100), nullable=False),
  18. sa.Column('credential_type', sa.Enum('assume_role', 'access_key', name='credential_type'), nullable=False),
  19. sa.Column('account_id', sa.String(length=12), nullable=False),
  20. sa.Column('role_arn', sa.String(length=255), nullable=True),
  21. sa.Column('external_id', sa.String(length=255), nullable=True),
  22. sa.Column('access_key_id', sa.String(length=255), nullable=True),
  23. sa.Column('secret_access_key_encrypted', sa.Text(), nullable=True),
  24. sa.Column('created_at', sa.DateTime(), nullable=True),
  25. sa.Column('is_active', sa.Boolean(), nullable=True),
  26. sa.PrimaryKeyConstraint('id')
  27. )
  28. with op.batch_alter_table('aws_credentials', schema=None) as batch_op:
  29. batch_op.create_index(batch_op.f('ix_aws_credentials_account_id'), ['account_id'], unique=False)
  30. op.create_table('base_assume_role_config',
  31. sa.Column('id', sa.Integer(), nullable=False),
  32. sa.Column('access_key_id', sa.String(length=255), nullable=False),
  33. sa.Column('secret_access_key_encrypted', sa.Text(), nullable=False),
  34. sa.Column('updated_at', sa.DateTime(), nullable=True),
  35. sa.PrimaryKeyConstraint('id')
  36. )
  37. op.create_table('users',
  38. sa.Column('id', sa.Integer(), nullable=False),
  39. sa.Column('username', sa.String(length=50), nullable=False),
  40. sa.Column('email', sa.String(length=100), nullable=False),
  41. sa.Column('password_hash', sa.String(length=255), nullable=False),
  42. sa.Column('role', sa.Enum('admin', 'power_user', 'user', name='user_role'), nullable=True),
  43. sa.Column('created_at', sa.DateTime(), nullable=True),
  44. sa.Column('is_active', sa.Boolean(), nullable=True),
  45. sa.PrimaryKeyConstraint('id')
  46. )
  47. with op.batch_alter_table('users', schema=None) as batch_op:
  48. batch_op.create_index(batch_op.f('ix_users_email'), ['email'], unique=True)
  49. batch_op.create_index(batch_op.f('ix_users_username'), ['username'], unique=True)
  50. op.create_table('tasks',
  51. sa.Column('id', sa.Integer(), nullable=False),
  52. sa.Column('name', sa.String(length=200), nullable=False),
  53. sa.Column('status', sa.Enum('pending', 'running', 'completed', 'failed', name='task_status'), nullable=True),
  54. sa.Column('progress', sa.Integer(), nullable=True),
  55. sa.Column('created_by', sa.Integer(), nullable=False),
  56. sa.Column('created_at', sa.DateTime(), nullable=True),
  57. sa.Column('started_at', sa.DateTime(), nullable=True),
  58. sa.Column('completed_at', sa.DateTime(), nullable=True),
  59. sa.Column('celery_task_id', sa.String(length=100), nullable=True),
  60. sa.Column('credential_ids', sa.Text(), nullable=True),
  61. sa.Column('regions', sa.Text(), nullable=True),
  62. sa.Column('project_metadata', sa.Text(), nullable=True),
  63. sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
  64. sa.PrimaryKeyConstraint('id')
  65. )
  66. with op.batch_alter_table('tasks', schema=None) as batch_op:
  67. batch_op.create_index(batch_op.f('ix_tasks_celery_task_id'), ['celery_task_id'], unique=False)
  68. batch_op.create_index(batch_op.f('ix_tasks_created_at'), ['created_at'], unique=False)
  69. batch_op.create_index(batch_op.f('ix_tasks_status'), ['status'], unique=False)
  70. op.create_table('user_credentials',
  71. sa.Column('id', sa.Integer(), nullable=False),
  72. sa.Column('user_id', sa.Integer(), nullable=False),
  73. sa.Column('credential_id', sa.Integer(), nullable=False),
  74. sa.Column('assigned_at', sa.DateTime(), nullable=True),
  75. sa.ForeignKeyConstraint(['credential_id'], ['aws_credentials.id'], ),
  76. sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
  77. sa.PrimaryKeyConstraint('id'),
  78. sa.UniqueConstraint('user_id', 'credential_id', name='unique_user_credential')
  79. )
  80. op.create_table('reports',
  81. sa.Column('id', sa.Integer(), nullable=False),
  82. sa.Column('task_id', sa.Integer(), nullable=False),
  83. sa.Column('file_name', sa.String(length=255), nullable=False),
  84. sa.Column('file_path', sa.String(length=500), nullable=False),
  85. sa.Column('file_size', sa.Integer(), nullable=True),
  86. sa.Column('created_at', sa.DateTime(), nullable=True),
  87. sa.ForeignKeyConstraint(['task_id'], ['tasks.id'], ),
  88. sa.PrimaryKeyConstraint('id'),
  89. sa.UniqueConstraint('task_id')
  90. )
  91. with op.batch_alter_table('reports', schema=None) as batch_op:
  92. batch_op.create_index(batch_op.f('ix_reports_created_at'), ['created_at'], unique=False)
  93. op.create_table('task_logs',
  94. sa.Column('id', sa.Integer(), nullable=False),
  95. sa.Column('task_id', sa.Integer(), nullable=False),
  96. sa.Column('level', sa.Enum('info', 'warning', 'error', name='log_level'), nullable=True),
  97. sa.Column('message', sa.Text(), nullable=False),
  98. sa.Column('details', sa.Text(), nullable=True),
  99. sa.Column('created_at', sa.DateTime(), nullable=True),
  100. sa.ForeignKeyConstraint(['task_id'], ['tasks.id'], ),
  101. sa.PrimaryKeyConstraint('id')
  102. )
  103. with op.batch_alter_table('task_logs', schema=None) as batch_op:
  104. batch_op.create_index(batch_op.f('ix_task_logs_created_at'), ['created_at'], unique=False)
  105. batch_op.create_index(batch_op.f('ix_task_logs_task_id'), ['task_id'], unique=False)
  106. # ### end Alembic commands ###
  107. def downgrade():
  108. # ### commands auto generated by Alembic - please adjust! ###
  109. with op.batch_alter_table('task_logs', schema=None) as batch_op:
  110. batch_op.drop_index(batch_op.f('ix_task_logs_task_id'))
  111. batch_op.drop_index(batch_op.f('ix_task_logs_created_at'))
  112. op.drop_table('task_logs')
  113. with op.batch_alter_table('reports', schema=None) as batch_op:
  114. batch_op.drop_index(batch_op.f('ix_reports_created_at'))
  115. op.drop_table('reports')
  116. op.drop_table('user_credentials')
  117. with op.batch_alter_table('tasks', schema=None) as batch_op:
  118. batch_op.drop_index(batch_op.f('ix_tasks_status'))
  119. batch_op.drop_index(batch_op.f('ix_tasks_created_at'))
  120. batch_op.drop_index(batch_op.f('ix_tasks_celery_task_id'))
  121. op.drop_table('tasks')
  122. with op.batch_alter_table('users', schema=None) as batch_op:
  123. batch_op.drop_index(batch_op.f('ix_users_username'))
  124. batch_op.drop_index(batch_op.f('ix_users_email'))
  125. op.drop_table('users')
  126. op.drop_table('base_assume_role_config')
  127. with op.batch_alter_table('aws_credentials', schema=None) as batch_op:
  128. batch_op.drop_index(batch_op.f('ix_aws_credentials_account_id'))
  129. op.drop_table('aws_credentials')
  130. # ### end Alembic commands ###