Skip to content

Commit

Permalink
Move alembic config into mealie package for easier distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
chishm committed Oct 7, 2024
1 parent a2beab6 commit 0a1cf31
Show file tree
Hide file tree
Showing 44 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ tasks:
py:migrate:
desc: generates a new database migration file e.g. task py:migrate -- "add new column"
cmds:
- poetry run alembic revision --autogenerate -m "{{ .CLI_ARGS }}"
- poetry run alembic --config mealie/alembic/alembic.ini revision --autogenerate -m "{{ .CLI_ARGS }}"
- task: py:format

ui:build:
Expand Down
4 changes: 0 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ COPY --from=crfpp /usr/local/bin/crf_test /usr/local/bin/crf_test
COPY ./mealie $MEALIE_HOME/mealie
COPY ./poetry.lock ./pyproject.toml $MEALIE_HOME/

# Alembic
COPY ./alembic $MEALIE_HOME/alembic
COPY ./alembic.ini $MEALIE_HOME/

# venv already has runtime deps installed we get a quicker install
WORKDIR $MEALIE_HOME
RUN . $VENV_PATH/bin/activate && poetry install -E pgsql --only main
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion alembic.ini → mealie/alembic/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[alembic]
# path to migration scripts
script_location = alembic
script_location = %(here)s

# template used to generate migration files
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d.%%(minute).2d.%%(second).2d_%%(rev)s_%%(slug)s
Expand Down
2 changes: 1 addition & 1 deletion alembic/env.py → mealie/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any

import sqlalchemy as sa
from alembic import context

import mealie.db.models._all_models # noqa: F401
from alembic import context
from mealie.core.config import get_app_settings
from mealie.db.models._model_base import SqlAlchemyBase

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from uuid import UUID

import sqlalchemy as sa
from alembic import op
from sqlalchemy import orm

import mealie.db.migration_types
from alembic import op
from mealie.core.root_logger import get_logger

logger = get_logger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from uuid import uuid4

import sqlalchemy as sa
from alembic import op
from sqlalchemy import orm

import mealie.db.migration_types
from alembic import op

# revision identifiers, used by Alembic.
revision = "d7c6efd2de42"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"""

import sqlalchemy as sa
from alembic import op

import mealie.db.migration_types
from alembic import op

# revision identifiers, used by Alembic.
revision = "7788478a0338"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"""

import sqlalchemy as sa
from sqlalchemy import orm

from alembic import op
from sqlalchemy import orm

# revision identifiers, used by Alembic.
revision = "32d69327997b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
from uuid import uuid4

import sqlalchemy as sa
from alembic import op
from slugify import slugify
from sqlalchemy import orm

import mealie.db.migration_types
from alembic import op
from mealie.core.config import get_app_settings

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from textwrap import dedent

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 @@ -7,9 +7,9 @@
"""

import sqlalchemy as sa
from alembic import op

import mealie.db.migration_types
from alembic import op

# revision identifiers, used by Alembic.
revision = "1fe4bd37ccc8"
Expand Down
8 changes: 4 additions & 4 deletions mealie/db/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from pathlib import Path
from time import sleep

from sqlalchemy import engine, orm, text

from alembic import command, config, script
from alembic.config import Config
from alembic.runtime import migration
from sqlalchemy import engine, orm, text

from mealie.core import root_logger
from mealie.core.config import get_app_settings
from mealie.db.db_setup import session_context
Expand All @@ -22,7 +22,7 @@
from mealie.services.group_services.group_service import GroupService
from mealie.services.household_services.household_service import HouseholdService

PROJECT_DIR = Path(__file__).parent.parent.parent
ALEMBIC_DIR = Path(__file__).parent.parent / "alembic"

logger = root_logger.get_logger()

Expand Down Expand Up @@ -101,7 +101,7 @@ def main():
if max_retry == 0:
raise ConnectionError("Database connection failed - exiting application.")

alembic_cfg_path = os.getenv("ALEMBIC_CONFIG_FILE", default=str(PROJECT_DIR / "alembic.ini"))
alembic_cfg_path = os.getenv("ALEMBIC_CONFIG_FILE", default=str(ALEMBIC_DIR / "alembic.ini"))

if not os.path.isfile(alembic_cfg_path):
raise Exception("Provided alembic config path doesn't exist")
Expand Down
13 changes: 4 additions & 9 deletions mealie/services/backups_v2/alchemy_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
import os
import uuid
from os import path
from pathlib import Path
from typing import Any

from alembic import command
from alembic.config import Config
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from sqlalchemy import ForeignKey, ForeignKeyConstraint, MetaData, Table, create_engine, insert, text
from sqlalchemy.engine import base
from sqlalchemy.orm import sessionmaker

from alembic import command
from alembic.config import Config
from mealie.db import init_db
from mealie.db.init_db import ALEMBIC_DIR
from mealie.db.models._model_utils.guid import GUID
from mealie.services._base_service import BaseService

PROJECT_DIR = Path(__file__).parent.parent.parent.parent


class AlchemyExporter(BaseService):
connection_str: str
Expand Down Expand Up @@ -152,15 +150,12 @@ def restore(self, db_dump: dict) -> None:
alembic_data = db_dump["alembic_version"]
alembic_version = alembic_data[0]["version_num"]

alembic_cfg_path = os.getenv("ALEMBIC_CONFIG_FILE", default=str(PROJECT_DIR / "alembic.ini"))
alembic_cfg_path = os.getenv("ALEMBIC_CONFIG_FILE", default=str(ALEMBIC_DIR / "alembic.ini"))

if not path.isfile(alembic_cfg_path):
raise Exception("Provided alembic config path doesn't exist")

alembic_cfg = Config(alembic_cfg_path)
# alembic's file resolver wants to use the "mealie" subdirectory when called from within the server package
# Just override this to use the correct migrations path
alembic_cfg.set_main_option("script_location", path.join(PROJECT_DIR, "alembic"))
command.upgrade(alembic_cfg, alembic_version)

del db_dump["alembic_version"]
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ select = [

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402", "E501"]
"alembic/versions/2022*" = ["E501"]
"alembic/versions/2023*" = ["E501"]
"mealie/alembic/versions/2022*" = ["E501", "I001"]
"mealie/alembic/versions/2023*" = ["E501", "I001"]
"dev/scripts/all_recipes_stress_test.py" = ["E501"]
"ldap_provider.py" = ["UP032"]
"tests/conftest.py" = ["E402"]
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/alembic_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import pathlib
from functools import lru_cache

from mealie.db.init_db import PROJECT_DIR
from mealie.db.init_db import ALEMBIC_DIR

ALEMBIC_MIGRATIONS = PROJECT_DIR / "alembic" / "versions"
ALEMBIC_MIGRATIONS = ALEMBIC_DIR / "versions"


def import_file(module_name: str, file_path: pathlib.Path):
Expand Down

0 comments on commit 0a1cf31

Please sign in to comment.