🔀 Merge branch 'almet/move-migrations' into 'main'

Move migrations inside the `argos/server/migrations` directory.

See merge request framasoft/framaspace/argos!43
This commit is contained in:
Luc Didry 2024-04-25 11:40:27 +00:00
commit dea9f40f7d
10 changed files with 18 additions and 11 deletions

View file

@ -5,7 +5,7 @@ ORANGE=\033[0;33m
BLUE=\033[0;34m BLUE=\033[0;34m
NC=\033[0m # No Color NC=\033[0m # No Color
.PHONY: test djlint pylint .PHONY: test lint djlint pylint ruff
venv: ## Create the venv venv: ## Create the venv
python3 -m venv venv python3 -m venv venv
@ -28,9 +28,7 @@ djlint: venv ## Format the templates
venv/bin/djlint --ignore=H030,H031,H006 --profile jinja --lint argos/server/templates/*html venv/bin/djlint --ignore=H030,H031,H006 --profile jinja --lint argos/server/templates/*html
pylint: venv ## Runs pylint on the code pylint: venv ## Runs pylint on the code
venv/bin/pylint argos venv/bin/pylint argos
pylint-alembic: venv ## Runs pylint on alembic migration files lint: djlint pylint ruff
venv/bin/pylint --disable invalid-name,no-member alembic/versions/*.py
lint: djlint pylint pylint-alembic ruff
help: help:
@python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) @python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

View file

@ -209,7 +209,8 @@ async def migrate(config):
settings = get_app_settings() settings = get_app_settings()
alembic_cfg = Config("alembic.ini") current_dir = os.path.dirname(__file__)
alembic_cfg = Config(os.path.join(current_dir, "server/migrations/alembic.ini"))
alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url) alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url)
command.upgrade(alembic_cfg, "head") command.upgrade(alembic_cfg, "head")

View file

@ -1,10 +1,12 @@
[alembic] [alembic]
script_location = alembic script_location = %(here)s
prepend_sys_path = . prepend_sys_path = .
version_path_separator = os version_path_separator = os
sqlalchemy.url = sqlite:////tmp/argos.db sqlalchemy.url = sqlite:////tmp/argos.db
version_locations = %(here)s/versions
# Logging configuration # Logging configuration
[loggers] [loggers]
keys = root,sqlalchemy,alembic keys = root,sqlalchemy,alembic

View file

@ -18,7 +18,9 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
bind = op.get_bind()
with op.batch_alter_table("results", schema=None) as batch_op: with op.batch_alter_table("results", schema=None) as batch_op:
if bind.engine.name != "sqlite":
batch_op.drop_constraint("results_task_id_fkey", type_="foreignkey") batch_op.drop_constraint("results_task_id_fkey", type_="foreignkey")
batch_op.create_foreign_key( batch_op.create_foreign_key(
"results_task_id_fkey", "tasks", ["task_id"], ["id"], ondelete="CASCADE" "results_task_id_fkey", "tasks", ["task_id"], ["id"], ondelete="CASCADE"
@ -26,7 +28,9 @@ def upgrade() -> None:
def downgrade() -> None: def downgrade() -> None:
bind = op.get_bind()
with op.batch_alter_table("results", schema=None) as batch_op: with op.batch_alter_table("results", schema=None) as batch_op:
if bind.engine.name != "sqlite":
batch_op.drop_constraint("results_task_id_fkey", type_="foreignkey") batch_op.drop_constraint("results_task_id_fkey", type_="foreignkey")
batch_op.create_foreign_key( batch_op.create_foreign_key(
"results_task_id_fkey", "tasks", ["task_id"], ["id"] "results_task_id_fkey", "tasks", ["task_id"], ["id"]

View file

@ -19,6 +19,8 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
bind = op.get_bind()
if bind.engine.name != "sqlite":
op.execute("ALTER TYPE severity ADD VALUE 'unknown'") op.execute("ALTER TYPE severity ADD VALUE 'unknown'")
op.add_column( op.add_column(
"tasks", "tasks",

View file

@ -7,7 +7,7 @@ First, do your changes in the code, change the model, add new tables, etc. Once
you're done, you can create a new migration. you're done, you can create a new migration.
```bash ```bash
venv/bin/alembic revision --autogenerate -m "migration reason" venv/bin/alembic -c argos/server/migrations/alembic.ini revision --autogenerate -m "migration reason"
``` ```
Edit the created file to remove comments and adapt it to make sure the migration is complete (Alembic is not powerful enough to cover all the corner cases). Edit the created file to remove comments and adapt it to make sure the migration is complete (Alembic is not powerful enough to cover all the corner cases).