Advertisement
Mochinov

Untitled

Apr 8th, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. """КRecreating tables min batches
  2.  
  3. Revision ID: 8ea062dfb9d0
  4. Revises: 23130213b059
  5. Create Date: 2024-04-08 14:38:27.571208
  6.  
  7. """
  8. from alembic import op
  9. from clickhouse_sqlalchemy import engines
  10. from clickhouse_sqlalchemy.types import common
  11. import sqlalchemy as sa
  12.  
  13.  
  14.  
  15. # revision identifiers, used by Alembic.
  16. revision = '8ea062dfb9d0'
  17. down_revision = '23130213b059'
  18. branch_labels = None
  19. depends_on = None
  20.  
  21.  
  22. def upgrade():
  23. conn = op.get_bind()
  24.  
  25. op.create_table(
  26. 'tetris_min_batches_data_a',
  27. sa.Column("tetris_scenario_id", common.UInt32(), nullable=False),
  28. sa.Column('plant_id', common.UInt32(), nullable=False),
  29. sa.Column('material_id', common.UInt64(), nullable=False),
  30. sa.Column('line_id', common.String(), nullable=False),
  31. sa.Column('min_batch_kg', common.Float64(), nullable=False),
  32. sa.Column("validation_error", common.UInt8(), nullable=False),
  33. engines.ReplacingMergeTree(
  34. primary_key=['tetris_scenario_id', 'material_id', 'plant_id', 'line_id'],
  35. partition_by=['tetris_scenario_id'],
  36. order_by=['tetris_scenario_id', 'material_id', 'plant_id', 'line_id']
  37. )
  38. )
  39.  
  40. tetris_min_batches_table = sa.sql.table(
  41. "tetris_min_batches_data_a",
  42. sa.Column("tetris_scenario_id", common.UInt32(), nullable=False),
  43. sa.Column('plant_id', common.UInt32(), nullable=False),
  44. sa.Column('material_id', common.UInt64(), nullable=False),
  45. sa.Column('line_id', common.String(), nullable=False),
  46. sa.Column('min_batch_kg', common.Float64(), nullable=False),
  47. sa.Column("validation_error", common.UInt8(), nullable=False),
  48. )
  49.  
  50.  
  51.  
  52. res = conn.execute(
  53. "Select tetris_scenario_id, material_id, plant_id, min_batch_kg, validation_error "
  54. f"FROM tetris_min_batches_data"
  55. )
  56. results = res.fetchall()
  57. records = []
  58. for result in results:
  59. records.append(
  60. {
  61. "tetris_scenario_id": result[0],
  62. "material_id": result[1],
  63. "plant_id": result[2],
  64. "min_batch_kg": result[3],
  65. "validation_error": result[5],
  66. }
  67. )
  68.  
  69. op.bulk_insert(tetris_min_batches_table, records)
  70.  
  71. op.drop_table("tetris_min_batches_data")
  72. op.execute("rename table tetris_min_batches_data_a to tetris_min_batches_data")
  73. op.execute("OPTIMIZE TABLE tetris_min_batches_data FINAL")
  74.  
  75.  
  76. def downgrade():
  77. pass
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement