Skip to content

Commit

Permalink
Move migrations folder and add first migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mslwang committed Oct 3, 2024
1 parent 42c8c76 commit 470b85e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
4 changes: 2 additions & 2 deletions backend/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
File renamed without changes.
16 changes: 11 additions & 5 deletions backend/migrations/env.py → backend/app/migrations/env.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit 470b85e

Please sign in to comment.