diff --git a/api/api/services/database.py b/api/api/services/database.py index a667776..87c8ae9 100644 --- a/api/api/services/database.py +++ b/api/api/services/database.py @@ -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: diff --git a/api/migrations/versions/4cacf36b2356_.py b/api/migrations/versions/4cacf36b2356_.py new file mode 100644 index 0000000..5a6e4f8 --- /dev/null +++ b/api/migrations/versions/4cacf36b2356_.py @@ -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 ### diff --git a/api/migrations/versions/b5d4ca37347c_.py b/api/migrations/versions/b5d4ca37347c_.py deleted file mode 100644 index ebaebf6..0000000 --- a/api/migrations/versions/b5d4ca37347c_.py +++ /dev/null @@ -1,43 +0,0 @@ -"""empty message - -Revision ID: b5d4ca37347c -Revises: 022497197c7a -Create Date: 2024-03-08 17:17:14.960915 - -""" -import sqlalchemy as sa -from alembic import op - -# revision identifiers, used by Alembic. -revision = 'b5d4ca37347c' -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) as batch_op: - batch_op.create_unique_constraint(None, ['secret_access_key']) - - with op.batch_alter_table('access_keys_config', schema=None) as batch_op: - batch_op.create_unique_constraint(None, ['access_key_id', 'chain_id']) - - with op.batch_alter_table('transactions', schema=None) as batch_op: - batch_op.create_unique_constraint(None, ['hash']) - - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('transactions', schema=None) as batch_op: - batch_op.drop_constraint(None, type_='unique') - - with op.batch_alter_table('access_keys_config', schema=None) as batch_op: - batch_op.drop_constraint(None, type_='unique') - - with op.batch_alter_table('access_keys', schema=None) as batch_op: - batch_op.drop_constraint(None, type_='unique') - - # ### end Alembic commands ###