Skip to content

Commit

Permalink
Fixes #5605 alembic migration errors on Focal
Browse files Browse the repository at this point in the history
Due to SQLite3 upgrade in Focal, our previous migration scripts
were failing.

From SQLite website https://sqlite.org/lang_altertable.html:

``` Compatibility Note: The behavior of ALTER TABLE when renaming a table was
enhanced in versions 3.25.0 (2018-09-15) and 3.26.0 (2018-12-01) in order to
carry the rename operation forward into triggers and views that reference the
renamed table. This is considered an improvement. Applications that depend on
the older (and arguably buggy) behavior can use the PRAGMA legacy_alter_table=ON
statement or the SQLITE_DBCONFIG_LEGACY_ALTER_TABLE configuration parameter on
sqlite3_db_config() interface to make ALTER TABLE RENAME behave as it did prior
to version 3.25.0.
```
  • Loading branch information
kushaldas committed Nov 2, 2020
1 parent 8c86e8e commit ba68bfb
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def upgrade():
def downgrade():
# sqlite has no `drop column` command, so we recreate the original table
# then load it from a temp table

conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
op.rename_table("journalists", "journalists_tmp")

op.create_table(
Expand All @@ -43,7 +44,6 @@ def downgrade():
sa.UniqueConstraint("username"),
)

conn = op.get_bind()
conn.execute(
"""
INSERT INTO journalists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@


def upgrade():
conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
# Schema migration
op.rename_table("sources", "sources_tmp")

# Add UUID column.
op.add_column("sources_tmp", sa.Column("uuid", sa.String(length=36)))

# Add UUIDs to sources_tmp table.
conn = op.get_bind()
sources = conn.execute(sa.text("SELECT * FROM sources_tmp")).fetchall()

for source in sources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@


def upgrade():
conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
# Save existing journalist table.
op.rename_table('journalists', 'journalists_tmp')

# Add nonce column.
op.add_column('journalists_tmp', sa.Column('session_nonce', sa.Integer()))

# Populate nonce column.
conn = op.get_bind()
journalists = conn.execute(
sa.text("SELECT * FROM journalists_tmp")).fetchall()

Expand Down Expand Up @@ -57,7 +58,6 @@ def upgrade():
sa.UniqueConstraint('uuid')
)

conn = op.get_bind()
conn.execute('''
INSERT INTO journalists
SELECT id, uuid, username, first_name, last_name, pw_salt, pw_hash,
Expand Down
3 changes: 2 additions & 1 deletion securedrop/alembic/versions/6db892e17271_add_reply_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@


def upgrade():
conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
# Schema migration
op.rename_table("replies", "replies_tmp")

# Add new column.
op.add_column("replies_tmp", sa.Column("uuid", sa.String(length=36)))

# Populate new column in replies_tmp table.
conn = op.get_bind()
replies = conn.execute(sa.text("SELECT * FROM replies_tmp")).fetchall()

for reply in replies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@


def upgrade():
conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
# Schema migration
op.rename_table("replies", "replies_tmp")

# Add new column.
op.add_column("replies_tmp", sa.Column("deleted_by_source", sa.Boolean()))

# Populate deleted_by_source column in replies_tmp table.
conn = op.get_bind()
replies = conn.execute(sa.text("SELECT * FROM replies_tmp")).fetchall()

for reply in replies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@


def upgrade():
conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
# Save existing journalist table.
op.rename_table("journalists", "journalists_tmp")

# Add UUID column.
op.add_column("journalists_tmp", sa.Column("uuid", sa.String(length=36)))

# Add UUIDs to journalists_tmp table.
conn = op.get_bind()
journalists = conn.execute(sa.text("SELECT * FROM journalists_tmp")).fetchall()

for journalist in journalists:
Expand Down Expand Up @@ -57,7 +58,6 @@ def upgrade():
sa.UniqueConstraint("uuid"),
)

conn = op.get_bind()
conn.execute(
"""
INSERT INTO journalists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@


def upgrade():
conn = op.get_bind()
conn.execute("PRAGMA legacy_alter_table=ON")
# Schema migration
op.rename_table("submissions", "submissions_tmp")

# Add UUID column.
op.add_column("submissions_tmp", sa.Column("uuid", sa.String(length=36)))

# Add UUIDs to submissions_tmp table.
conn = op.get_bind()
submissions = conn.execute(sa.text("SELECT * FROM submissions_tmp")).fetchall()

for submission in submissions:
Expand Down

0 comments on commit ba68bfb

Please sign in to comment.