🔀 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
NC=\033[0m # No Color
.PHONY: test djlint pylint
.PHONY: test lint djlint pylint ruff
venv: ## Create the 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
pylint: venv ## Runs pylint on the code
venv/bin/pylint argos
pylint-alembic: venv ## Runs pylint on alembic migration files
venv/bin/pylint --disable invalid-name,no-member alembic/versions/*.py
lint: djlint pylint pylint-alembic ruff
lint: djlint pylint ruff
help:
@python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

View file

@ -209,7 +209,8 @@ async def migrate(config):
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)
command.upgrade(alembic_cfg, "head")

View file

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

View file

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

View file

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

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.
```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).