Skip to content

Commit

Permalink
Add migration script for schema 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abudlong committed Jan 15, 2025
1 parent 8fe7346 commit ed1a170
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/lsst.dax.apdb_migrate/migrations/sql/schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,15 @@ No additional parameters or packages are needed for this script.
An example of migration::

$ apdb-migrate-sql upgrade -s SCHEMA_NAME $APDB_URL schema_4.0.0

Upgrade from 4.0.0 to 5.0.0
===========================

Migration script: `schema_5.0.0.py <https://github.com/lsst-dm/dax_apdb_migrate/blob/main/migrations/sql/schema/schema_5.0.0.py>`_

This migration adds ``is_negative`` column to ``DiaSource`` table, initially set to ``NULL``.
No additional parameters or packages are needed for this script.

An example of migration::

$ apdb-migrate-sql upgrade -s SCHEMA_NAME $APDB_URL schema_5.0.0
66 changes: 66 additions & 0 deletions migrations/sql/schema/schema_5.0.0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Migration script for schema 5.0.0.
Revision ID: schema_5.0.0
Revises: schema_4.0.0
Create Date: 2025-01-15 12:06:42.740977
"""

import logging

import sqlalchemy
from lsst.dax.apdb_migrate.sql.context import Context

# revision identifiers, used by Alembic.
revision = "schema_5.0.0"
down_revision = "schema_4.0.0"
branch_labels = None
depends_on = None

_LOG = logging.getLogger(__name__)


def upgrade() -> None:
"""Upgrade 'schema' tree from 4.0.0 to 5.0.0 (ticket DM-48437).
Summary of changes:
- Add empty dipoleFitAttempted column.
"""
ctx = Context()

# Alter table schema.
_LOG.info("Adding column to DiaSource table.")
try:
summary = ctx.get_table("DiaSource")
with ctx.batch_alter_table("DiaSource", copy_from=summary) as batch_op:
# add empty column.
batch_op.add_column(
sqlalchemy.Column(
"is_negative", sqlalchemy.types.Boolean, nullable=True
)
)
except sqlalchemy.exc.NoSuchTableError:
pass
# Update metadata version.
tree, _, version = revision.partition("_")
ctx.apdb_meta.update_tree_version(tree, version)


def downgrade() -> None:
"""Downgrade 'schema' tree from 5.0.0 to 4.0.0 (ticket DM-48437).
Summary of changes:
- Remove empty dipoleFitAttempted column.
"""
ctx = Context()

# Alter table schema.
_LOG.info("Dropping column to DiaSource table.")
try:
summary = ctx.get_table("DiaSource")
with ctx.batch_alter_table("DiaSource", copy_from=summary) as batch_op:
# drop pixelScale column.
batch_op.drop_column("is_negative")
except sqlalchemy.exc.NoSuchTableError:
pass
# Update metadata version.
tree, _, version = down_revision.partition("_")
ctx.apdb_meta.update_tree_version(tree, version)

0 comments on commit ed1a170

Please sign in to comment.