Skip to content

Commit

Permalink
🗃️ Init alembic
Browse files Browse the repository at this point in the history
  • Loading branch information
agn-7 committed Nov 8, 2023
1 parent 88010df commit 2e42f76
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
51 changes: 51 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = alembic

[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
hooks=black
black.type=console_scripts
black.entrypoint=black
black.options=

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
81 changes: 81 additions & 0 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config, pool, create_engine

from alembic import context # type: ignore
from ifsguid.config import settings
from ifsguid.models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata # type: ignore

# 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.


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""

context.configure(
url=settings.SQLALCHEMY_DATABASE_URI.unicode_string(),
target_metadata=target_metadata,
literal_binds=True,
compare_types=True,
dialect_opts={"paramstyle": "named"},
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
configuration = config.get_section(config.config_ini_section)
configuration["sqlalchemy.url"] = f"{settings.SQLALCHEMY_DATABASE_URI}"
connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata, compare_type=True
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
65 changes: 65 additions & 0 deletions alembic/versions/b77bb559f678_init_message_and_interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Init message and interaction
Revision ID: b77bb559f678
Revises:
Create Date: 2023-11-08 04:17:55.802076
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "b77bb559f678"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"interaction",
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.Column("settings", sa.JSON(), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("id"),
)
op.create_index(
op.f("ix_interaction_created_at"), "interaction", ["created_at"], unique=False
)
op.create_index(
op.f("ix_interaction_updated_at"), "interaction", ["updated_at"], unique=False
)
op.create_table(
"message",
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("role", sa.String(), nullable=True),
sa.Column("content", sa.String(), nullable=True),
sa.Column("interaction_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.ForeignKeyConstraint(
["interaction_id"],
["interaction.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("id"),
)
op.create_index(
op.f("ix_message_created_at"), "message", ["created_at"], unique=False
)
op.create_index(op.f("ix_message_role"), "message", ["role"], unique=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_message_role"), table_name="message")
op.drop_index(op.f("ix_message_created_at"), table_name="message")
op.drop_table("message")
op.drop_index(op.f("ix_interaction_updated_at"), table_name="interaction")
op.drop_index(op.f("ix_interaction_created_at"), table_name="interaction")
op.drop_table("interaction")
# ### end Alembic commands ###

0 comments on commit 2e42f76

Please sign in to comment.