From 10016a427d5fede9309a8369624a73d2e1873067 Mon Sep 17 00:00:00 2001
From: PGijsbers
Date: Tue, 29 Oct 2024 16:49:27 +0200
Subject: [PATCH] Add migration to extend distribution content_url to LONG
---
alembic/README.md | 18 +++++++-
.../279e17c48ea1_extend_description_length.py | 42 +++++++++++++++++++
2 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 alembic/alembic/versions/279e17c48ea1_extend_description_length.py
diff --git a/alembic/README.md b/alembic/README.md
index c0960285..386d5533 100644
--- a/alembic/README.md
+++ b/alembic/README.md
@@ -17,7 +17,7 @@ With the sqlserver container running, you can migrate to the latest schema with:
```commandline
docker run -v $(pwd)/alembic:/alembic:ro -v $(pwd)/src:/app -it --network aiod_default aiod-migration
```
-Make sure that the specifid `--network` is the docker network that has the `sqlserver` container.
+Make sure that the specified `--network` is the docker network that has the `sqlserver` container.
The alembic directory is mounted to ensure the latest migrations are available,
the src directory is mounted so the migration scripts can use defined classes and variable from the project.
@@ -28,5 +28,21 @@ the src directory is mounted so the migration scripts can use defined classes an
Following the usage commands above, on a new release we should run alembic to ensure the latest schema changes are applied.
The default entrypoint of the container specifies to upgrade the database to the latest schema.
+## Adding a Revision
+
+Build the docker image above, and start a container of it with shell as entry:
+
+```bash
+docker run -v $(pwd)/alembic:/alembic -v $(pwd)/src:/app -it --network aiod_default --entrypoint=/bin/bash aiod-migration
+```
+
+Then follow regular `alembic` steps:
+```bash
+alembic revision -m "revision message"
+```
+Then edit the generated file (note that it should also exist on your host machine, so you might prefer to edit it there).
+
+Note that working from a docker container is not strictly necessary, but it helps set up the PYTHONPATH correctly, so that you can import from the `src` directory.
+
## TODO
- set up support for auto-generating migration scripts: https://alembic.sqlalchemy.org/en/latest/autogenerate.html
diff --git a/alembic/alembic/versions/279e17c48ea1_extend_description_length.py b/alembic/alembic/versions/279e17c48ea1_extend_description_length.py
new file mode 100644
index 00000000..a4984954
--- /dev/null
+++ b/alembic/alembic/versions/279e17c48ea1_extend_description_length.py
@@ -0,0 +1,42 @@
+"""Extend description length
+
+Revision ID: 279e17c48ea1
+Revises: 0a23b40cc09c
+Create Date: 2024-10-29 14:38:30.684251
+
+"""
+from typing import Sequence, Union
+
+from alembic import op
+
+from sqlalchemy import String
+
+from database.model.field_length import LONG
+
+# revision identifiers, used by Alembic.
+revision: str = "279e17c48ea1"
+down_revision: Union[str, None] = "0a23b40cc09c"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # All models that have associated distributions
+ for table in [
+ "dataset",
+ "case_study",
+ "publication",
+ "computational_asset",
+ "ml_model",
+ "experiment",
+ ]:
+ op.alter_column(
+ f"distribution_{table}",
+ "content_url",
+ type_=String(LONG),
+ )
+
+
+def downgrade() -> None:
+ # from NORMAL
+ pass