1010from alembic import op
1111import sqlalchemy as sa
1212
13-
1413# revision identifiers, used by Alembic.
1514revision = 'e074a214491e'
1615down_revision = 'a8d8866bd608'
1716branch_labels = None
1817depends_on = None
1918
19+ def index_exists (connection , table_name , index_name ):
20+ sql = """
21+ SELECT COUNT(1)
22+ FROM INFORMATION_SCHEMA.STATISTICS
23+ WHERE table_schema=DATABASE() AND table_name=:table_name AND index_name=:index_name
24+ """
25+ res = connection .execute (sa .text (sql ), {'table_name' : table_name , 'index_name' : index_name })
26+ return res .scalar () > 0
2027
2128def upgrade ():
22- # ### commands auto generated by Alembic - please adjust! ###
23- with op .batch_alter_table ('documento' , schema = None ) as batch_op :
24- batch_op .drop_index (batch_op .f ('idx_documento_data_vencimento' ))
25- # batch_op.drop_index(batch_op.f('idx_documento_entidade_tipo')) # NÃO REMOVA OU COMENTE SE DER ERRO!
26- batch_op .drop_index (batch_op .f ('idx_documento_tipo' ))
27-
28- with op .batch_alter_table ('endividamento' , schema = None ) as batch_op :
29- batch_op .drop_index (batch_op .f ('idx_endividamento_banco' ))
30- batch_op .drop_index (batch_op .f ('idx_endividamento_created_at' ))
31- batch_op .drop_index (batch_op .f ('idx_endividamento_data_vencimento' ))
32-
33- with op .batch_alter_table ('historico_notificacao' , schema = None ) as batch_op :
34- batch_op .drop_index (batch_op .f ('idx_historico_notificacao_data' ))
35-
36- with op .batch_alter_table ('notificacao_endividamento' , schema = None ) as batch_op :
37- batch_op .drop_index (batch_op .f ('idx_notificacao_endividamento_ativo' ))
38-
39- with op .batch_alter_table ('parcela' , schema = None ) as batch_op :
40- batch_op .drop_index (batch_op .f ('idx_parcela_data_vencimento' ))
41- # batch_op.drop_index(batch_op.f('idx_parcela_endividamento_id')) # NÃO REMOVA! USADO EM FK
42- batch_op .drop_index (batch_op .f ('idx_parcela_pago' ))
43-
44- with op .batch_alter_table ('pessoa' , schema = None ) as batch_op :
45- batch_op .drop_index (batch_op .f ('idx_pessoa_cpf_cnpj' ))
46- batch_op .drop_index (batch_op .f ('idx_pessoa_nome' ))
47-
48- # ### end Alembic commands ###
49-
29+ conn = op .get_bind ()
30+
31+ # DOCUMENTO
32+ if index_exists (conn , 'documento' , 'idx_documento_data_vencimento' ):
33+ with op .batch_alter_table ('documento' , schema = None ) as batch_op :
34+ batch_op .drop_index (batch_op .f ('idx_documento_data_vencimento' ))
35+ # batch_op.drop_index(batch_op.f('idx_documento_entidade_tipo')) # NÃO REMOVA OU COMENTE SE DER ERRO!
36+ if index_exists (conn , 'documento' , 'idx_documento_tipo' ):
37+ with op .batch_alter_table ('documento' , schema = None ) as batch_op :
38+ batch_op .drop_index (batch_op .f ('idx_documento_tipo' ))
39+
40+ # ENDIVIDAMENTO
41+ if index_exists (conn , 'endividamento' , 'idx_endividamento_banco' ):
42+ with op .batch_alter_table ('endividamento' , schema = None ) as batch_op :
43+ batch_op .drop_index (batch_op .f ('idx_endividamento_banco' ))
44+ if index_exists (conn , 'endividamento' , 'idx_endividamento_created_at' ):
45+ with op .batch_alter_table ('endividamento' , schema = None ) as batch_op :
46+ batch_op .drop_index (batch_op .f ('idx_endividamento_created_at' ))
47+ if index_exists (conn , 'endividamento' , 'idx_endividamento_data_vencimento' ):
48+ with op .batch_alter_table ('endividamento' , schema = None ) as batch_op :
49+ batch_op .drop_index (batch_op .f ('idx_endividamento_data_vencimento' ))
50+
51+ # HISTORICO_NOTIFICACAO
52+ if index_exists (conn , 'historico_notificacao' , 'idx_historico_notificacao_data' ):
53+ with op .batch_alter_table ('historico_notificacao' , schema = None ) as batch_op :
54+ batch_op .drop_index (batch_op .f ('idx_historico_notificacao_data' ))
55+
56+ # NOTIFICACAO_ENDIVIDAMENTO
57+ if index_exists (conn , 'notificacao_endividamento' , 'idx_notificacao_endividamento_ativo' ):
58+ with op .batch_alter_table ('notificacao_endividamento' , schema = None ) as batch_op :
59+ batch_op .drop_index (batch_op .f ('idx_notificacao_endividamento_ativo' ))
60+
61+ # PARCELA
62+ if index_exists (conn , 'parcela' , 'idx_parcela_data_vencimento' ):
63+ with op .batch_alter_table ('parcela' , schema = None ) as batch_op :
64+ batch_op .drop_index (batch_op .f ('idx_parcela_data_vencimento' ))
65+ # NÃO REMOVA: índice de FK!
66+ # if index_exists(conn, 'parcela', 'idx_parcela_endividamento_id'):
67+ # with op.batch_alter_table('parcela', schema=None) as batch_op:
68+ # batch_op.drop_index(batch_op.f('idx_parcela_endividamento_id'))
69+ if index_exists (conn , 'parcela' , 'idx_parcela_pago' ):
70+ with op .batch_alter_table ('parcela' , schema = None ) as batch_op :
71+ batch_op .drop_index (batch_op .f ('idx_parcela_pago' ))
72+
73+ # PESSOA
74+ if index_exists (conn , 'pessoa' , 'idx_pessoa_cpf_cnpj' ):
75+ with op .batch_alter_table ('pessoa' , schema = None ) as batch_op :
76+ batch_op .drop_index (batch_op .f ('idx_pessoa_cpf_cnpj' ))
77+ if index_exists (conn , 'pessoa' , 'idx_pessoa_nome' ):
78+ with op .batch_alter_table ('pessoa' , schema = None ) as batch_op :
79+ batch_op .drop_index (batch_op .f ('idx_pessoa_nome' ))
5080
5181def downgrade ():
52- # ### commands auto generated by Alembic - please adjust! ###
5382 with op .batch_alter_table ('pessoa' , schema = None ) as batch_op :
5483 batch_op .create_index (batch_op .f ('idx_pessoa_nome' ), ['nome' ], unique = False )
5584 batch_op .create_index (batch_op .f ('idx_pessoa_cpf_cnpj' ), ['cpf_cnpj' ], unique = False )
@@ -74,5 +103,4 @@ def downgrade():
74103 batch_op .create_index (batch_op .f ('idx_documento_tipo' ), ['tipo' ], unique = False )
75104 batch_op .create_index (batch_op .f ('idx_documento_entidade_tipo' ), ['tipo' ], unique = False )
76105 batch_op .create_index (batch_op .f ('idx_documento_data_vencimento' ), ['data_vencimento' ], unique = False )
77-
78- # ### end Alembic commands ###
106+
0 commit comments