🐛 fix(adds_tables.py): create "user" and "apikey" tables only if they don't exist to prevent duplicate table creation

✨ feat(adds_tables.py): add unique index on "username" column in "user" table for faster username lookups
✨ feat(adds_tables.py): add unique index on "api_key" column in "apikey" table for faster API key lookups
✨ feat(adds_tables.py): add index on "name" column in "apikey" table for faster name lookups
✨ feat(adds_tables.py): add index on "user_id" column in "apikey" table for faster user ID lookups
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-28 11:17:12 -03:00
commit 2d22737c31

View file

@ -39,6 +39,7 @@ def upgrade() -> None:
for fk in inspector.get_foreign_keys("flow") for fk in inspector.get_foreign_keys("flow")
] ]
if "user" not in existing_tables:
op.create_table( op.create_table(
"user", "user",
sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False), sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
@ -53,8 +54,11 @@ def upgrade() -> None:
sa.UniqueConstraint("id"), sa.UniqueConstraint("id"),
) )
with op.batch_alter_table("user", schema=None) as batch_op: with op.batch_alter_table("user", schema=None) as batch_op:
batch_op.create_index(batch_op.f("ix_user_username"), ["username"], unique=True) batch_op.create_index(
batch_op.f("ix_user_username"), ["username"], unique=True
)
if "apikey" not in existing_tables:
op.create_table( op.create_table(
"apikey", "apikey",
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=True), sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
@ -73,7 +77,9 @@ def upgrade() -> None:
sa.UniqueConstraint("id"), sa.UniqueConstraint("id"),
) )
with op.batch_alter_table("apikey", schema=None) as batch_op: with op.batch_alter_table("apikey", schema=None) as batch_op:
batch_op.create_index(batch_op.f("ix_apikey_api_key"), ["api_key"], unique=True) batch_op.create_index(
batch_op.f("ix_apikey_api_key"), ["api_key"], unique=True
)
batch_op.create_index(batch_op.f("ix_apikey_name"), ["name"], unique=False) batch_op.create_index(batch_op.f("ix_apikey_name"), ["name"], unique=False)
batch_op.create_index( batch_op.create_index(
batch_op.f("ix_apikey_user_id"), ["user_id"], unique=False batch_op.f("ix_apikey_user_id"), ["user_id"], unique=False