Skip to content

Commit

Permalink
Merge pull request #74 from python-discord/jb3/sqlalchemy-2
Browse files Browse the repository at this point in the history
SQLAlchemy 2
  • Loading branch information
jb3 authored Sep 4, 2023
2 parents 5d37417 + c6829ab commit 48459ff
Show file tree
Hide file tree
Showing 37 changed files with 827 additions and 693 deletions.
65 changes: 31 additions & 34 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import asyncio
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool
from sqlalchemy import Connection, pool
from sqlalchemy.ext.asyncio import async_engine_from_config

from alembic import context

import sys
sys.path.append(".")

from metricity.database import build_db_uri
from metricity.models import db
from metricity.models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -21,20 +18,37 @@

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = db
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', build_db_uri())
config.set_main_option("sqlalchemy.url", build_db_uri())

def do_run_migrations(connection: Connection) -> None:
"""Run migrations."""
context.configure(connection=connection, target_metadata=target_metadata)

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

async def run_async_migrations() -> None:
"""Run migrations asynchronously using the asyncpg driver."""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)

await connectable.dispose()

def run_migrations_offline():
"""Run migrations in 'offline' mode.
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
Expand All @@ -57,26 +71,9 @@ def run_migrations_offline():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

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

with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())


if context.is_offline_mode():
Expand Down
10 changes: 6 additions & 4 deletions alembic/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}
def upgrade() -> None:
"""Apply the current migration."""
${upgrades}


def downgrade():
${downgrades if downgrades else "pass"}
def downgrade() -> None:
"""Revert the current migration."""
${downgrades}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
New NOT NULL constraints on messages and users table.
Revision ID: 03655ce2097b
Revises: 563a15b2a76e
Create Date: 2023-09-04 20:17:03.543328
"""
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "03655ce2097b"
down_revision = "563a15b2a76e"
branch_labels = None
depends_on = None


def upgrade() -> None:
"""Apply the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column("messages", "channel_id",
existing_type=sa.VARCHAR(),
nullable=False)
op.alter_column("messages", "author_id",
existing_type=sa.VARCHAR(),
nullable=False)
op.alter_column("messages", "is_deleted",
existing_type=sa.BOOLEAN(),
nullable=False)
op.alter_column("users", "bot",
existing_type=sa.BOOLEAN(),
nullable=False)
op.alter_column("users", "in_guild",
existing_type=sa.BOOLEAN(),
nullable=False)
op.alter_column("users", "pending",
existing_type=sa.BOOLEAN(),
nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
"""Revert the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column("users", "pending",
existing_type=sa.BOOLEAN(),
nullable=True)
op.alter_column("users", "in_guild",
existing_type=sa.BOOLEAN(),
nullable=True)
op.alter_column("users", "bot",
existing_type=sa.BOOLEAN(),
nullable=True)
op.alter_column("messages", "is_deleted",
existing_type=sa.BOOLEAN(),
nullable=True)
op.alter_column("messages", "author_id",
existing_type=sa.VARCHAR(),
nullable=True)
op.alter_column("messages", "channel_id",
existing_type=sa.VARCHAR(),
nullable=True)
# ### end Alembic commands ###
19 changes: 11 additions & 8 deletions alembic/versions/25f3b8fb9961_add_pending_column_to_user.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
"""add pending column to user
"""
add pending column to user.
Revision ID: 25f3b8fb9961
Revises: a259ab5efcec
Create Date: 2020-12-21 17:42:04.566930
"""
from alembic import op
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = '25f3b8fb9961'
down_revision = 'a259ab5efcec'
revision = "25f3b8fb9961"
down_revision = "a259ab5efcec"
branch_labels = None
depends_on = None


def upgrade():
def upgrade() -> None:
"""Apply the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('pending', sa.Boolean(), nullable=True))
op.add_column("users", sa.Column("pending", sa.Boolean(), nullable=True))
# ### end Alembic commands ###


def downgrade():
def downgrade() -> None:
"""Revert the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'pending')
op.drop_column("users", "pending")
# ### end Alembic commands ###
81 changes: 42 additions & 39 deletions alembic/versions/2743389eb63e_add_all_tables_with_string_keys.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,66 @@
"""add all tables with string keys
"""
add all tables with string keys.
Revision ID: 2743389eb63e
Revises: 2e383ecae493
Create Date: 2020-08-25 16:35:38.833315
"""
from alembic import op
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = '2743389eb63e'
down_revision = '2e383ecae493'
revision = "2743389eb63e"
down_revision = "2e383ecae493"
branch_labels = None
depends_on = None


def upgrade():
def upgrade() -> None:
"""Apply the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('categories',
sa.Column('id', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id')
op.create_table("categories",
sa.Column("id", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_table('users',
sa.Column('id', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('avatar_hash', sa.String(), nullable=True),
sa.Column('joined_at', sa.DateTime(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('is_staff', sa.Boolean(), nullable=False),
sa.Column('opt_out', sa.Boolean(), nullable=True),
sa.Column('bot', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id')
op.create_table("users",
sa.Column("id", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("avatar_hash", sa.String(), nullable=True),
sa.Column("joined_at", sa.DateTime(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("is_staff", sa.Boolean(), nullable=False),
sa.Column("opt_out", sa.Boolean(), nullable=True),
sa.Column("bot", sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_table('channels',
sa.Column('id', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('category_id', sa.String(), nullable=True),
sa.Column('is_staff', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
sa.PrimaryKeyConstraint('id')
op.create_table("channels",
sa.Column("id", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("category_id", sa.String(), nullable=True),
sa.Column("is_staff", sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(["category_id"], ["categories.id"] ),
sa.PrimaryKeyConstraint("id"),
)
op.create_table('messages',
sa.Column('id', sa.String(), nullable=False),
sa.Column('channel_id', sa.String(), nullable=True),
sa.Column('author_id', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['author_id'], ['users.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['channel_id'], ['channels.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
op.create_table("messages",
sa.Column("id", sa.String(), nullable=False),
sa.Column("channel_id", sa.String(), nullable=True),
sa.Column("author_id", sa.String(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(["author_id"], ["users.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["channel_id"], ["channels.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade():
def downgrade() -> None:
"""Revert the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('messages')
op.drop_table('channels')
op.drop_table('users')
op.drop_table('categories')
op.drop_table("messages")
op.drop_table("channels")
op.drop_table("users")
op.drop_table("categories")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
"""remove all tables for conversion to string keys
"""
remove all tables for conversion to string keys.
Revision ID: 2e383ecae493
Revises: b1fdfe71fcb7
Create Date: 2020-08-25 16:31:05.025135
"""
from alembic import op
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = '2e383ecae493'
down_revision = 'b1fdfe71fcb7'
revision = "2e383ecae493"
down_revision = "b1fdfe71fcb7"
branch_labels = None
depends_on = None


def upgrade():
op.drop_table('messages')
op.drop_table('users')
op.drop_table('channels')
op.drop_table('categories')
def upgrade() -> None:
"""Apply the current migration."""
op.drop_table("messages")
op.drop_table("users")
op.drop_table("channels")
op.drop_table("categories")


def downgrade():
pass
def downgrade() -> None:
"""Revert the current migration."""
Loading

0 comments on commit 48459ff

Please sign in to comment.