Skip to content

Commit

Permalink
Fix migrations, add naming convention to help sqlite DB handle ALTER …
Browse files Browse the repository at this point in the history
…commands
  • Loading branch information
Giacomo Licari committed Mar 9, 2024
1 parent fa0d434 commit 6c461f4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
11 changes: 10 additions & 1 deletion api/api/services/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
from datetime import datetime

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData

from api.const import (DEFAULT_ERC20_MAX_AMOUNT_PER_DAY,
DEFAULT_NATIVE_MAX_AMOUNT_PER_DAY, FaucetRequestType)

db = SQLAlchemy()
flask_db_convention = {
"ix": 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
db_metadata = MetaData(naming_convention=flask_db_convention)
db = SQLAlchemy(metadata=db_metadata)


class Database:
Expand Down
45 changes: 45 additions & 0 deletions api/migrations/versions/4cacf36b2356_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""empty message
Revision ID: 4cacf36b2356
Revises: 022497197c7a
Create Date: 2024-03-09 11:37:03.009350
"""
from alembic import op
import sqlalchemy as sa

from api.services.database import flask_db_convention


# revision identifiers, used by Alembic.
revision = '4cacf36b2356'
down_revision = '022497197c7a'
branch_labels = None
depends_on = None

def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('access_keys', schema=None, naming_convention=flask_db_convention) as batch_op:
batch_op.create_unique_constraint(batch_op.f('uq_access_keys_secret_access_key'), ['secret_access_key'])

with op.batch_alter_table('access_keys_config', schema=None, naming_convention=flask_db_convention) as batch_op:
batch_op.create_unique_constraint(batch_op.f('uq_access_keys_config_access_key_id'), ['access_key_id', 'chain_id'])

with op.batch_alter_table('transactions', schema=None, naming_convention=flask_db_convention) as batch_op:
batch_op.create_unique_constraint(batch_op.f('uq_transactions_hash'), ['hash'])

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('transactions', schema=None, naming_convention=flask_db_convention) as batch_op:
batch_op.drop_constraint(batch_op.f('uq_transactions_hash'), type_='unique')

with op.batch_alter_table('access_keys_config', schema=None, naming_convention=flask_db_convention) as batch_op:
batch_op.drop_constraint(batch_op.f('uq_access_keys_config_access_key_id'), type_='unique')

with op.batch_alter_table('access_keys', schema=None, naming_convention=flask_db_convention) as batch_op:
batch_op.drop_constraint(batch_op.f('uq_access_keys_secret_access_key'), type_='unique')

# ### end Alembic commands ###
43 changes: 0 additions & 43 deletions api/migrations/versions/b5d4ca37347c_.py

This file was deleted.

0 comments on commit 6c461f4

Please sign in to comment.