1 """Add BlockSchema capabilities
2
3 Revision ID: 33439667aeea
4 Revises: 888a0bb0df7b
5 Create Date: 2022-05-19 16:58:08.802305
6
7 """
8
9 import sqlalchemy as sa 1 ctx 1a
10 from alembic import op 1 ctx 1a
11
12 import prefect 1 ctx 1a
13
14 # revision identifiers, used by Alembic.
15 revision = "33439667aeea" 1 ctx 1a
16 down_revision = "888a0bb0df7b" 1 ctx 1a
17 branch_labels = None 1 ctx 1a
18 depends_on = None 1 ctx 1a
19
20
21 def upgrade ( ) : 1 ctx 1a
22 # ### commands auto generated by Alembic - please adjust! ###
23 with op . batch_alter_table ( "block_document" , schema = None ) as batch_op : 1 ctx 1a
24 batch_op . drop_constraint ( "fk_block__block_schema_id__block_schema" ) 1 ctx 1a
25 with op . batch_alter_table ( "block_schema" , schema = None ) as batch_op : 1 ctx 1a
26 batch_op . add_column ( 1 ctx 1a
27 sa . Column (
28 "capabilities" ,
29 prefect . server . utilities . database . JSON ( astext_type = sa . Text ( ) ) ,
30 server_default = "[]" ,
31 nullable = False ,
32 ) ,
33 )
34
35 connection = op . get_bind ( ) 1 ctx 1a
36 meta_data = sa . MetaData ( ) 1 ctx 1a
37 meta_data . reflect ( connection ) 1 ctx 1a
38 BLOCK_SCHEMA = meta_data . tables [ "block_schema" ] 1 ctx 1a
39
40 results = connection . execute ( sa . select ( BLOCK_SCHEMA . c . id , BLOCK_SCHEMA . c . type ) ) 1 ctx 1a
41
42 for id , type in results : 42 ↛ 43 line 42 didn't jump to line 43 because the loop on line 42 never started 1 ctx 1a
43 if type == "STORAGE" :
44 connection . execute (
45 sa . update ( BLOCK_SCHEMA )
46 . where ( BLOCK_SCHEMA . c . id == id )
47 . values ( capabilities = [ "writeable" , "readable" , "storage" ] )
48 )
49
50 with op . batch_alter_table ( "block_schema" , schema = None ) as batch_op : 1 ctx 1a
51 batch_op . drop_index ( "ix_block_schema__type" ) 1 ctx 1a
52 batch_op . drop_column ( "type" ) 1 ctx 1a
53
54 with op . batch_alter_table ( 1 ctx 1a
55 "block_document" ,
56 schema = None ,
57 ) as batch_op :
58 batch_op . create_foreign_key ( 1 ctx 1a
59 batch_op . f ( "fk_block__block_schema_id__block_schema" ) ,
60 "block_schema" ,
61 [ "block_schema_id" ] ,
62 [ "id" ] ,
63 ondelete = "cascade" ,
64 )
65 # ### end Alembic commands ###
66
67
68 def downgrade ( ) : 1 ctx 1a
69 # ### commands auto generated by Alembic - please adjust! ###
70 with op . batch_alter_table ( "block_schema" , schema = None ) as batch_op :
71 batch_op . add_column ( sa . Column ( "type" , sa . VARCHAR ( ) , nullable = True ) )
72 batch_op . create_index ( "ix_block_schema__type" , [ "type" ] , unique = False )
73
74 connection = op . get_bind ( )
75 meta_data = sa . MetaData ( )
76 meta_data . reflect ( connection )
77 BLOCK_SCHEMA = meta_data . tables [ "block_schema" ]
78
79 results = connection . execute (
80 sa . select ( BLOCK_SCHEMA . c . id , BLOCK_SCHEMA . c . capabilities )
81 )
82
83 for id , capabilities in results :
84 if "storage" in capabilities :
85 connection . execute (
86 sa . update ( BLOCK_SCHEMA )
87 . where ( BLOCK_SCHEMA . c . id == id )
88 . values ( type = "STORAGE" )
89 )
90
91 with op . batch_alter_table ( "block_schema" , schema = None ) as batch_op :
92 batch_op . drop_column ( "capabilities" )
93 # ### end Alembic commands ###