Skip to content

Commit

Permalink
Merge pull request #1288 from shangyian/alembic-migrations-release
Browse files Browse the repository at this point in the history
Include alembic migrations as part of datajunction-server release
  • Loading branch information
shangyian authored Jan 24, 2025
2 parents 2f0a28f + 5a9ce33 commit f69a5f9
Show file tree
Hide file tree
Showing 25 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
# Run tests
export MODULE=${{ matrix.library == 'server' && 'datajunction_server' || matrix.library == 'client' && 'datajunction' || matrix.library == 'djqs' && 'djqs' || matrix.library == 'djrs' && 'datajunction_reflection'}}
pdm run pytest ${{ (matrix.library == 'server' || matrix.library == 'client') && '-n auto' || '' }} --cov-fail-under=100 --cov=$MODULE --cov-report term-missing -vv tests/ --doctest-modules $MODULE --without-integration --without-slow-integration
pdm run pytest ${{ (matrix.library == 'server' || matrix.library == 'client') && '-n auto' || '' }} --cov-fail-under=100 --cov=$MODULE --cov-report term-missing -vv tests/ --doctest-modules $MODULE --without-integration --without-slow-integration --ignore=datajunction_server/alembic/env.py
build-javascript:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions datajunction-server/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ omit =
*/datajunction_server/api/access/authentication/google.py
*/datajunction_server/api/djsql.py
*/datajunction_server/construction/dj_query.py
*/datajunction_server/alembic/*

[paths]
source =
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"""
# pylint: disable=no-member, unused-import, no-name-in-module, import-error

import os
from logging.config import fileConfig

import alembic
from sqlalchemy import create_engine

from alembic import context
from datajunction_server.database import (
AttributeType,
Catalog,
Expand All @@ -29,11 +30,14 @@
from datajunction_server.database.base import Base
from datajunction_server.database.column import Column

DEFAULT_URI = "postgresql+psycopg://dj:dj@postgres_metadata:5432/dj"
DEFAULT_URI = os.getenv(
"DATABASE_URI",
"postgresql+psycopg://dj:dj@postgres_metadata:5432/dj",
)

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

# Interpret the config file for Python logging.
# This line sets up loggers basically.
Expand Down Expand Up @@ -64,17 +68,17 @@ def run_migrations_offline():
script output.
"""
x_args = context.get_x_argument(as_dictionary=True)
context.configure(
x_args = alembic.context.get_x_argument(as_dictionary=True)
alembic.context.configure(
url=x_args.get("uri") or DEFAULT_URI,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True,
)

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


def run_migrations_online():
Expand All @@ -84,21 +88,21 @@ def run_migrations_online():
and associate a connection with the context.
"""
x_args = context.get_x_argument(as_dictionary=True)
x_args = alembic.context.get_x_argument(as_dictionary=True)
connectable = create_engine(x_args.get("uri") or DEFAULT_URI)

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

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


if context.is_offline_mode():
if alembic.context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import sqlalchemy as sa
import sqlalchemy_utils

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "c74b11566d82"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "20f060b02772"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "a8e22109be24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "d61fb7e48cc3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "de7ec1c82fe0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "9b1227ff17f4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "70904373eab3"
Expand Down
2 changes: 1 addition & 1 deletion datajunction-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["datajunction_server"]

include = ["alembic/**", "alembic.ini"]

[tool.pdm]
[tool.pdm.build]
Expand Down
10 changes: 5 additions & 5 deletions datajunction-server/tests/migrations_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Verify alembic migrations."""
import pytest
from sqlalchemy import create_engine
from sqlalchemy.engine.base import Connection
from testcontainers.postgres import PostgresContainer

from alembic.autogenerate import compare_metadata
from alembic.config import Config
from alembic.runtime.environment import EnvironmentContext
from alembic.runtime.migration import MigrationContext
from alembic.script import ScriptDirectory
from sqlalchemy import create_engine
from sqlalchemy.engine.base import Connection
from testcontainers.postgres import PostgresContainer

from datajunction_server.database.base import Base


Expand All @@ -34,7 +34,7 @@ def test_migrations_are_current(connection): # pylint: disable=redefined-outer-
target_metadata = Base.metadata

config = Config("alembic.ini")
config.set_main_option("script_location", "alembic")
config.set_main_option("script_location", "datajunction_server/alembic")
script = ScriptDirectory.from_config(config)

context = EnvironmentContext(
Expand Down
Empty file.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ services:
context: ./datajunction-server
volumes:
- ./datajunction-server:/code
working_dir: /code/datajunction_server
command: /bin/bash -c "alembic -x uri="postgresql+psycopg://dj:dj@postgres_metadata:5432/dj" upgrade head"
restart: on-failure
depends_on:
Expand Down

0 comments on commit f69a5f9

Please sign in to comment.