From c78a8d9326f01cc22524272f2dabeebac79d9fd7 Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Sun, 26 Nov 2023 23:08:19 +0100 Subject: [PATCH] alembic: add deletion_status field --- .../3c1ae3770e92_add_missing_columns.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 invenio_records_lom/alembic/3c1ae3770e92_add_missing_columns.py diff --git a/invenio_records_lom/alembic/3c1ae3770e92_add_missing_columns.py b/invenio_records_lom/alembic/3c1ae3770e92_add_missing_columns.py new file mode 100644 index 0000000..ea8e543 --- /dev/null +++ b/invenio_records_lom/alembic/3c1ae3770e92_add_missing_columns.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2023 Graz University of Technology. +# +# invenio-records-lom is free software; you can redistribute it and/or modify it +# under the terms of the MIT License; see LICENSE file for more details. + +"""Create invenio records lom tables.""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "3c1ae3770e92" +down_revision = "1dc5abf0e34b" +branch_labels = () +depends_on = None + + +def upgrade(): + """Upgrade database.""" + # step 1: create the columns, but make them nullable for now + op.add_column( + "lom_records_metadata", + sa.Column("deletion_status", sa.String(length=1), nullable=True), + ) + op.add_column( + "lom_records_metadata_version", + sa.Column( + "deletion_status", + sa.String(length=1), + nullable=True, + ), + ) + + # step 2: set default values for existing rows + default_value = "P" + metadata_table = sa.sql.table( + "lom_records_metadata", sa.sql.column("deletion_status") + ) + metadata_version_table = sa.sql.table( + "lom_records_metadata_version", sa.sql.column("deletion_status") + ) + op.execute(metadata_table.update().values(deletion_status=default_value)) + op.execute(metadata_version_table.update().values(deletion_status=default_value)) + + # step 3: make the original table not nullable + op.alter_column("lom_records_metadata", "deletion_status", nullable=False) + + +def downgrade(): + """Downgrade database.""" + op.drop_column("lom_records_metadata_version", "deletion_status") + op.drop_column("lom_records_metadata", "deletion_status")