Coverage for /usr/local/lib/python3.12/site-packages/prefect/server/database/_migrations/versions/sqlite/2024_12_04_144924_a49711513ad4_sync_orm_models_and_migrations.py: 60%

47 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 11:21 +0000

1"""Sync ORM models and migrations 

2 

3Revision ID: a49711513ad4 

4Revises: 5952a5498b51 

5Create Date: 2024-12-04 14:49:24.099491 

6 

7""" 

8 

9import sqlalchemy as sa 1a

10from alembic import op 1a

11from sqlalchemy.dialects import sqlite 1a

12 

13# revision identifiers, used by Alembic. 

14revision = "a49711513ad4" 1a

15down_revision = "5952a5498b51" 1a

16branch_labels = None 1a

17depends_on = None 1a

18 

19 

20def upgrade(): 1a

21 with op.batch_alter_table("artifact_collection", schema=None) as batch_op: 1a

22 # table added in 027c123512befd2bd00a0ef28bd44215e77bece6 but index was 

23 # never created in a migration. 

24 batch_op.create_index( 1a

25 batch_op.f("ix_artifact_collection__updated"), ["updated"], unique=False 

26 ) 

27 # index created on the wrong table in ca9f93463a4c38fce8be972d91e808b5935e5d9c 

28 batch_op.drop_index("ix_artifact__key_created_desc") 1a

29 

30 with op.batch_alter_table("artifact", schema=None) as batch_op: 1a

31 # index created on the wrong table in ca9f93463a4c38fce8be972d91e808b5935e5d9c 

32 batch_op.create_index( 1a

33 "ix_artifact__key_created_desc", 

34 ["key", sa.text("created DESC")], 

35 unique=False, 

36 postgresql_include=["id", "updated", "type", "task_run_id", "flow_run_id"], 

37 ) 

38 

39 with op.batch_alter_table("block_document", schema=None) as batch_op: 1a

40 # Renamed index to remain consistent with PostgreSQL 

41 batch_op.drop_index("ix_block_document__block_type_name_name") 1a

42 batch_op.create_index( 1a

43 "ix_block_document__block_type_name__name", 

44 ["block_type_name", "name"], 

45 unique=False, 

46 ) 

47 

48 with op.batch_alter_table("deployment", schema=None) as batch_op: 1a

49 # columns removed in c53b00bfa1f6850ab43e168c92c627350c090647 

50 batch_op.drop_column("schedule") 1a

51 batch_op.drop_column("is_schedule_active") 1a

52 

53 # column removed in 5784c637e7e11a8e88e2b3146e54e9b6c97d50ef 

54 batch_op.drop_column("flow_data") 1a

55 

56 # column removed in eaa7a5063c73718dff56ce4aeb66e53fcafe60e5 

57 batch_op.drop_column("manifest_path") 1a

58 

59 with op.batch_alter_table("deployment_schedule", schema=None) as batch_op: 1a

60 # columns removed from orm models in 0b62de684447c6955e04c722c276edac4002fd40 

61 batch_op.drop_column("catchup") 1a

62 batch_op.drop_column("max_active_runs") 1a

63 

64 with op.batch_alter_table("flow_run", schema=None) as batch_op: 1a

65 # Column is no longer a FK since d10c7471a69403bcf88f401091497a2dc8963885 

66 batch_op.drop_index("ix_flow_run__deployment_id") 1a

67 # Index accidentally dropped in 519a2ed6e31e2b60136e1a1a163a9cd0a8d3d5c4 

68 batch_op.create_index( 1a

69 "ix_flow_run__scheduler_deployment_id_auto_scheduled_next_schedu", 

70 ["deployment_id", "auto_scheduled", "next_scheduled_start_time"], 

71 unique=False, 

72 postgresql_where=sa.text("state_type = 'SCHEDULED'::state_type"), 

73 sqlite_where=sa.text("state_type = 'SCHEDULED'"), 

74 ) 

75 

76 

77def downgrade(): 1a

78 with op.batch_alter_table("flow_run", schema=None) as batch_op: 

79 batch_op.drop_index( 

80 "ix_flow_run__scheduler_deployment_id_auto_scheduled_next_schedu", 

81 postgresql_where=sa.text("state_type = 'SCHEDULED'::state_type"), 

82 sqlite_where=sa.text("state_type = 'SCHEDULED'"), 

83 ) 

84 batch_op.create_index( 

85 "ix_flow_run__deployment_id", ["deployment_id"], unique=False 

86 ) 

87 

88 with op.batch_alter_table("deployment_schedule", schema=None) as batch_op: 

89 batch_op.add_column(sa.Column("max_active_runs", sa.INTEGER(), nullable=True)) 

90 batch_op.add_column( 

91 sa.Column( 

92 "catchup", sa.BOOLEAN(), server_default=sa.text("'0'"), nullable=False 

93 ) 

94 ) 

95 

96 with op.batch_alter_table("deployment", schema=None) as batch_op: 

97 batch_op.add_column( 

98 sa.Column( 

99 "is_schedule_active", 

100 sa.BOOLEAN(), 

101 server_default=sa.text("'1'"), 

102 nullable=False, 

103 ) 

104 ) 

105 batch_op.add_column(sa.Column("flow_data", sqlite.JSON(), nullable=True)) 

106 batch_op.add_column(sa.Column("schedule", sqlite.JSON(), nullable=True)) 

107 batch_op.add_column(sa.Column("manifest_path", sa.VARCHAR(), nullable=True)) 

108 

109 with op.batch_alter_table("block_document", schema=None) as batch_op: 

110 batch_op.drop_index("ix_block_document__block_type_name__name") 

111 batch_op.create_index( 

112 "ix_block_document__block_type_name_name", 

113 ["block_type_name", "name"], 

114 unique=False, 

115 ) 

116 

117 with op.batch_alter_table("artifact", schema=None) as batch_op: 

118 batch_op.drop_index( 

119 "ix_artifact__key_created_desc", 

120 postgresql_include=["id", "updated", "type", "task_run_id", "flow_run_id"], 

121 ) 

122 

123 with op.batch_alter_table("artifact_collection", schema=None) as batch_op: 

124 batch_op.create_index( 

125 "ix_artifact__key_created_desc", ["key", "created"], unique=False 

126 ) 

127 batch_op.drop_index(batch_op.f("ix_artifact_collection__updated"))