Skip to content

Commit

Permalink
Check if view exists before populating collection_id, if creating dat…
Browse files Browse the repository at this point in the history
…abase from scratch
  • Loading branch information
jpmckinney committed Jan 8, 2020
1 parent 6deb2ce commit 597c901
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ def upgrade():
op.create_index('release_collection_id_idx', 'release', ['collection_id'])
op.create_index('compiled_release_collection_id_idx', 'compiled_release', ['collection_id'])

op.execute(
"UPDATE record SET collection_id = record_with_collection.collection_id "
"FROM record_with_collection WHERE record.id = record_with_collection.id")
op.execute(
"UPDATE release SET collection_id = release_with_collection.collection_id "
"FROM release_with_collection WHERE release.id = release_with_collection.id")
op.execute(
"UPDATE compiled_release SET collection_id = compiled_release_with_collection.collection_id "
"FROM compiled_release_with_collection WHERE compiled_release.id = compiled_release_with_collection.id")
conn = op.get_bind()

res = conn.execute("SELECT 1 FROM information_schema.tables WHERE table_name = 'record_with_collection'")
if res.scalar():
op.execute(
"UPDATE record SET collection_id = record_with_collection.collection_id "
"FROM record_with_collection WHERE record.id = record_with_collection.id")

res = conn.execute("SELECT 1 FROM information_schema.tables WHERE table_name = 'release_with_collection'")
if res.scalar():
op.execute(
"UPDATE release SET collection_id = release_with_collection.collection_id "
"FROM release_with_collection WHERE release.id = release_with_collection.id")

res = conn.execute("SELECT 1 FROM information_schema.tables WHERE table_name = 'compiled_release_with_collection'")
if res.scalar():
op.execute(
"UPDATE compiled_release SET collection_id = compiled_release_with_collection.collection_id "
"FROM compiled_release_with_collection WHERE compiled_release.id = compiled_release_with_collection.id")

op.alter_column('record', 'collection_id', nullable=False)
op.alter_column('release', 'collection_id', nullable=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Create Date: 2019-02-06 14:19:41.539672
"""
from alembic import op
from sqlalchemy.sql import text

# revision identifiers, used by Alembic.
revision = 'aef60ba6c0bc'
Expand All @@ -18,5 +16,6 @@
def upgrade():
pass


def downgrade():
pass

0 comments on commit 597c901

Please sign in to comment.