diff --git a/backend/alembic.ini b/backend/alembic.ini index 18f8896..cd66bf8 100644 --- a/backend/alembic.ini +++ b/backend/alembic.ini @@ -3,7 +3,7 @@ [alembic] # path to migration scripts # Use forward slashes (/) also on windows to provide an os agnostic path -script_location = migrations +script_location = app/migrations # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s # Uncomment the line below if you want the files to be prepended with date and time @@ -61,7 +61,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = driver://user:pass@localhost/dbname +# sqlalchemy.url = [post_write_hooks] diff --git a/backend/migrations/README b/backend/app/migrations/README similarity index 100% rename from backend/migrations/README rename to backend/app/migrations/README diff --git a/backend/migrations/env.py b/backend/app/migrations/env.py similarity index 83% rename from backend/migrations/env.py rename to backend/app/migrations/env.py index 36112a3..17feb44 100644 --- a/backend/migrations/env.py +++ b/backend/app/migrations/env.py @@ -1,9 +1,13 @@ from logging.config import fileConfig +import os from sqlalchemy import engine_from_config from sqlalchemy import pool from alembic import context +from dotenv import load_dotenv + +load_dotenv() # this is the Alembic Config object, which provides # access to the values within the .ini file in use. @@ -18,12 +22,15 @@ # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata -target_metadata = None +from app.models import Base + +target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, # can be acquired: # my_important_option = config.get_main_option("my_important_option") # ... etc. +config.set_main_option("sqlalchemy.url", os.environ["POSTGRES_DATABASE_URL"]) def run_migrations_offline() -> None: @@ -57,16 +64,15 @@ def run_migrations_online() -> None: and associate a connection with the context. """ + alembic_config = config.get_section(config.config_ini_section, {}) connectable = engine_from_config( - config.get_section(config.config_ini_section, {}), + alembic_config, prefix="sqlalchemy.", poolclass=pool.NullPool, ) with connectable.connect() as connection: - context.configure( - connection=connection, target_metadata=target_metadata - ) + context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() diff --git a/backend/migrations/script.py.mako b/backend/app/migrations/script.py.mako similarity index 100% rename from backend/migrations/script.py.mako rename to backend/app/migrations/script.py.mako diff --git a/backend/app/migrations/versions/4ba3479cb8df_create_user_table_and_roles.py b/backend/app/migrations/versions/4ba3479cb8df_create_user_table_and_roles.py new file mode 100644 index 0000000..c21a380 --- /dev/null +++ b/backend/app/migrations/versions/4ba3479cb8df_create_user_table_and_roles.py @@ -0,0 +1,51 @@ +"""create user table and roles + +Revision ID: 4ba3479cb8df +Revises: +Create Date: 2024-10-03 00:41:13.800838 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "4ba3479cb8df" +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "roles", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sa.String(length=80), nullable=False), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "users", + sa.Column("id", sa.String(), nullable=False), + sa.Column("first_name", sa.String(length=80), nullable=True), + sa.Column("last_name", sa.String(length=80), nullable=True), + sa.Column("email", sa.String(length=120), nullable=False), + sa.Column("role_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["role_id"], + ["roles.id"], + ), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("email"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("users") + op.drop_table("roles") + # ### end Alembic commands ###