Skip to content

Commit

Permalink
Merge pull request #48 from CAVEconnectome/fix_migrations
Browse files Browse the repository at this point in the history
feat: check if tables or columns already exist
  • Loading branch information
dlbrittain authored Jun 13, 2024
2 parents 90cc599 + c9333a6 commit 6d8be14
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = '309cf493a1e2'
Expand All @@ -17,10 +19,20 @@
depends_on = None


def _table_has_column(table, column):
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)

insp = reflection.Inspector.from_engine(engine)
return any(column in col["name"] for col in insp.get_columns(table))



def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('annotation_table_metadata', sa.Column('notice_text', sa.Text(), nullable=True))
# ### end Alembic commands ###
if not _table_has_column('annotation_table_metadata', 'notice_text'):
op.add_column('annotation_table_metadata', sa.Column('notice_text', sa.Text(), nullable=True))


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,43 @@
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = "5a1d7c0ad006"
down_revision = "7c79eff751b4"
branch_labels = None
depends_on = None

def _table_has_column(table, column):
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)

insp = reflection.Inspector.from_engine(engine)
return any(column in col["name"] for col in insp.get_columns(table))

def upgrade():
status_enum = postgresql.ENUM(
"AVAILABLE", "RUNNING", "FAILED", "EXPIRED", name="version_status"
)
status_enum.create(op.get_bind())
op.add_column(
"analysisversion",
sa.Column(
"status",
postgresql.ENUM(
"AVAILABLE", "RUNNING", "FAILED", "EXPIRED", name="version_status"
),
nullable=True,
if not _table_has_column("analysisversion", "status"):
op.add_column(
"analysisversion",
sa.Column(
"status",
postgresql.ENUM(
"AVAILABLE", "RUNNING", "FAILED", "EXPIRED", name="version_status"
),
nullable=True,

),
)
op.execute("UPDATE analysisversion SET status = 'EXPIRED'")
op.alter_column('analysisversion', 'status', nullable=False)
),
)
op.execute("UPDATE analysisversion SET status = 'EXPIRED'")
op.alter_column('analysisversion', 'status', nullable=False)


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = '6e7f580ff680'
Expand All @@ -17,8 +19,18 @@
depends_on = None


def _table_has_column(table, column):
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)

insp = reflection.Inspector.from_engine(engine)
return any(column in col["name"] for col in insp.get_columns(table))

def upgrade():
op.add_column('version_error', sa.Column('exception', sa.String(), nullable=True))
if not _table_has_column("version_error", "exception"):
op.add_column('version_error', sa.Column('exception', sa.String(), nullable=True))


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ def _table_has_column(table, column):


def upgrade():
with op.batch_alter_table("analysisversion", schema=None) as batch_op:
op.add_column(
"analysisversion",
sa.Column("parent_version", sa.Integer(), nullable=True),
)
op.create_foreign_key(
None, "analysisversion", "analysisversion", ["parent_version"], ["id"]
)
if not _table_has_column("analysisversion", "parent_version"):

with op.batch_alter_table("analysisversion", schema=None) as batch_op:
op.add_column(
"analysisversion",
sa.Column("parent_version", sa.Integer(), nullable=True),
)
op.create_foreign_key(
None, "analysisversion", "analysisversion", ["parent_version"], ["id"]
)


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,46 @@
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = "814d72d74e3b"
down_revision = "975a79461cab"
branch_labels = None
depends_on = None

def get_tables():
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)
inspector = reflection.Inspector.from_engine(engine)
return inspector.get_table_names()


def upgrade():
op.create_table(
"version_error",
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column(
"error",
postgresql.JSON(astext_type=sa.Text()),
autoincrement=False,
nullable=True,
),
sa.Column(
"analysisversion_id", sa.INTEGER(), autoincrement=False, nullable=True
),
sa.ForeignKeyConstraint(
["analysisversion_id"],
["analysisversion.id"],
name="version_error_analysisversion_id_fkey",
),
sa.PrimaryKeyConstraint("id", name="version_error_pkey"),
)
tables = get_tables()
if "version_error" not in tables:
op.create_table(
"version_error",
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column(
"error",
postgresql.JSON(astext_type=sa.Text()),
autoincrement=False,
nullable=True,
),
sa.Column(
"analysisversion_id", sa.INTEGER(), autoincrement=False, nullable=True
),
sa.ForeignKeyConstraint(
["analysisversion_id"],
["analysisversion.id"],
name="version_error_analysisversion_id_fkey",
),
sa.PrimaryKeyConstraint("id", name="version_error_pkey"),
)


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,64 @@
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = "8fdc843fc202"
down_revision = "6e7f580ff680"
branch_labels = None
depends_on = None

def _table_has_column(table, column):
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)

insp = reflection.Inspector.from_engine(engine)
return any(column in col["name"] for col in insp.get_columns(table))


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
permission_enum = postgresql.ENUM(
"PRIVATE", "GROUP", "PUBLIC", name="readwrite_permission"
)
permission_enum.create(op.get_bind())

op.add_column(
"annotation_table_metadata",
sa.Column(
"write_permission",
postgresql.ENUM("PRIVATE", "GROUP", "PUBLIC", name="readwrite_permission"),
nullable=True,
),
)
op.add_column(
"annotation_table_metadata",
sa.Column(
"read_permission",
postgresql.ENUM("PRIVATE", "GROUP", "PUBLIC", name="readwrite_permission"),
nullable=True,
),
)
op.add_column(
"annotation_table_metadata",
sa.Column("last_modified", sa.DateTime(), nullable=True),
)
# ### end Alembic commands ###
op.execute("UPDATE annotation_table_metadata SET read_permission = 'PUBLIC'")
op.execute("UPDATE annotation_table_metadata SET write_permission = 'PRIVATE'")
op.execute("UPDATE annotation_table_metadata SET last_modified = current_timestamp")
permission_enum.create(op.get_bind())

if not _table_has_column("annotation_table_metadata", "write_permission"):
op.add_column(
"annotation_table_metadata",
sa.Column(
"write_permission",
postgresql.ENUM("PRIVATE", "GROUP", "PUBLIC", name="readwrite_permission"),
nullable=True,
),
)
op.execute("UPDATE annotation_table_metadata SET write_permission = 'PRIVATE'")
op.alter_column("annotation_table_metadata", "write_permission", nullable=False)

op.alter_column("annotation_table_metadata", "write_permission", nullable=False)
op.alter_column("annotation_table_metadata", "read_permission", nullable=False)
op.alter_column("annotation_table_metadata", "last_modified", nullable=False)
if not _table_has_column("annotation_table_metadata", "read_permission"):
op.add_column(
"annotation_table_metadata",
sa.Column(
"read_permission",
postgresql.ENUM("PRIVATE", "GROUP", "PUBLIC", name="readwrite_permission"),
nullable=True,
),
)
op.execute("UPDATE annotation_table_metadata SET read_permission = 'PUBLIC'")
op.alter_column("annotation_table_metadata", "read_permission", nullable=False)

if not _table_has_column("annotation_table_metadata", "last_modified"):
op.add_column(
"annotation_table_metadata",
sa.Column("last_modified", sa.DateTime(), nullable=True),
)

op.execute("UPDATE annotation_table_metadata SET last_modified = current_timestamp")
op.alter_column("annotation_table_metadata", "last_modified", nullable=False)

def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("annotation_table_metadata", "last_modified")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,34 @@
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = "975a79461cab"
down_revision = "5a1d7c0ad006"
branch_labels = None
depends_on = None

def _table_has_column(table, column):
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)

def upgrade():
insp = reflection.Inspector.from_engine(engine)
return any(column in col["name"] for col in insp.get_columns(table))

op.add_column(
"analysisversion",
sa.Column("is_merged", sa.Boolean(), nullable=True, default=True),
)

op.execute("UPDATE analysisversion SET is_merged = True")
op.alter_column('analysisversion', 'is_merged', nullable=False)
def upgrade():
if not _table_has_column("analysisversion", "is_merged"):
op.add_column(
"analysisversion",
sa.Column("is_merged", sa.Boolean(), nullable=True, default=True),
)

op.execute("UPDATE analysisversion SET is_merged = True")
op.alter_column('analysisversion', 'is_merged', nullable=False)

def downgrade():
op.drop_column("analysisversion", "is_merged")
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,40 @@
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql
from sqlalchemy.engine import reflection
from sqlalchemy import engine_from_config

# revision identifiers, used by Alembic.
revision = "fac66b439033"
down_revision = "309cf493a1e2"
branch_labels = None
depends_on = None

def get_tables():
config = op.get_context().config
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix="sqlalchemy."
)
inspector = reflection.Inspector.from_engine(engine)
return inspector.get_table_names()


def upgrade():
op.create_table(
"analysisviews",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("table_name", sa.String(length=100), nullable=False),
sa.Column("description", sa.Text(), nullable=False),
sa.Column("datastack_name", sa.String(length=100), nullable=False),
sa.Column("voxel_resolution_x", sa.Float(), nullable=False),
sa.Column("voxel_resolution_y", sa.Float(), nullable=False),
sa.Column("voxel_resolution_z", sa.Float(), nullable=False),
sa.Column("notice_text", sa.Text(), nullable=True),
sa.Column("live_compatible", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
tables = get_tables()
if "analysisviews" not in tables:
op.create_table(
"analysisviews",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("table_name", sa.String(length=100), nullable=False),
sa.Column("description", sa.Text(), nullable=False),
sa.Column("datastack_name", sa.String(length=100), nullable=False),
sa.Column("voxel_resolution_x", sa.Float(), nullable=False),
sa.Column("voxel_resolution_y", sa.Float(), nullable=False),
sa.Column("voxel_resolution_z", sa.Float(), nullable=False),
sa.Column("notice_text", sa.Text(), nullable=True),
sa.Column("live_compatible", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)


def downgrade():
Expand Down

0 comments on commit 6d8be14

Please sign in to comment.