|
| 1 | +"""Create legacy NMA_MajorChemistry table. |
| 2 | +
|
| 3 | +Revision ID: a7b8c9d0e1f2 |
| 4 | +Revises: f3b4c5d6e7f8 |
| 5 | +Create Date: 2026-03-01 02:00:00.000000 |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Sequence, Union |
| 9 | + |
| 10 | +import sqlalchemy as sa |
| 11 | +from alembic import op |
| 12 | +from sqlalchemy import inspect |
| 13 | +from sqlalchemy.dialects import postgresql |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision: str = "a7b8c9d0e1f2" |
| 17 | +down_revision: Union[str, Sequence[str], None] = "f1a2b3c4d5e6" |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | + |
| 22 | +def upgrade() -> None: |
| 23 | + """Create the legacy major chemistry table.""" |
| 24 | + bind = op.get_bind() |
| 25 | + inspector = inspect(bind) |
| 26 | + if not inspector.has_table("NMA_MajorChemistry"): |
| 27 | + op.create_table( |
| 28 | + "NMA_MajorChemistry", |
| 29 | + sa.Column( |
| 30 | + "SamplePtID", |
| 31 | + postgresql.UUID(as_uuid=True), |
| 32 | + sa.ForeignKey( |
| 33 | + "NMA_Chemistry_SampleInfo.SamplePtID", ondelete="CASCADE" |
| 34 | + ), |
| 35 | + nullable=False, |
| 36 | + ), |
| 37 | + sa.Column("SamplePointID", sa.String(length=10), nullable=True), |
| 38 | + sa.Column("Analyte", sa.String(length=50), nullable=True), |
| 39 | + sa.Column("Symbol", sa.String(length=50), nullable=True), |
| 40 | + sa.Column( |
| 41 | + "SampleValue", sa.Float(), nullable=True, server_default=sa.text("0") |
| 42 | + ), |
| 43 | + sa.Column("Units", sa.String(length=50), nullable=True), |
| 44 | + sa.Column("Uncertainty", sa.Float(), nullable=True), |
| 45 | + sa.Column("AnalysisMethod", sa.String(length=255), nullable=True), |
| 46 | + sa.Column("AnalysisDate", sa.DateTime(), nullable=True), |
| 47 | + sa.Column("Notes", sa.String(length=255), nullable=True), |
| 48 | + sa.Column( |
| 49 | + "Volume", sa.Integer(), nullable=True, server_default=sa.text("0") |
| 50 | + ), |
| 51 | + sa.Column("VolumeUnit", sa.String(length=50), nullable=True), |
| 52 | + sa.Column("OBJECTID", sa.Integer(), nullable=True, unique=True), |
| 53 | + sa.Column( |
| 54 | + "GlobalID", |
| 55 | + postgresql.UUID(as_uuid=True), |
| 56 | + nullable=False, |
| 57 | + primary_key=True, |
| 58 | + ), |
| 59 | + sa.Column("AnalysesAgency", sa.String(length=50), nullable=True), |
| 60 | + sa.Column("WCLab_ID", sa.String(length=25), nullable=True), |
| 61 | + ) |
| 62 | + op.create_index( |
| 63 | + "MajorChemistry$AnalysesAgency", |
| 64 | + "NMA_MajorChemistry", |
| 65 | + ["AnalysesAgency"], |
| 66 | + ) |
| 67 | + op.create_index( |
| 68 | + "MajorChemistry$Analyte", |
| 69 | + "NMA_MajorChemistry", |
| 70 | + ["Analyte"], |
| 71 | + ) |
| 72 | + op.create_index( |
| 73 | + "MajorChemistry$Chemistry SampleInfoMajorChemistry", |
| 74 | + "NMA_MajorChemistry", |
| 75 | + ["SamplePtID"], |
| 76 | + ) |
| 77 | + op.create_index( |
| 78 | + "MajorChemistry$SamplePointID", |
| 79 | + "NMA_MajorChemistry", |
| 80 | + ["SamplePointID"], |
| 81 | + ) |
| 82 | + op.create_index( |
| 83 | + "MajorChemistry$SamplePointIDAnalyte", |
| 84 | + "NMA_MajorChemistry", |
| 85 | + ["SamplePointID", "Analyte"], |
| 86 | + ) |
| 87 | + op.create_index( |
| 88 | + "MajorChemistry$SamplePtID", |
| 89 | + "NMA_MajorChemistry", |
| 90 | + ["SamplePtID"], |
| 91 | + ) |
| 92 | + op.create_index( |
| 93 | + "MajorChemistry$WCLab_ID", |
| 94 | + "NMA_MajorChemistry", |
| 95 | + ["WCLab_ID"], |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def downgrade() -> None: |
| 100 | + """Drop the legacy major chemistry table.""" |
| 101 | + bind = op.get_bind() |
| 102 | + inspector = inspect(bind) |
| 103 | + if inspector.has_table("NMA_MajorChemistry"): |
| 104 | + op.drop_table("NMA_MajorChemistry") |
0 commit comments