Skip to content

Commit

Permalink
Added drop trigger to alembic ugprade to prevent issue with trigger t… (
Browse files Browse the repository at this point in the history
#176)

* Added drop trigger to alembic ugprade to prevent issue with trigger trying to write to big_id after name is changed.

* Added fix for sqlite testing.

* Additional tweaks to make sure downgrade and upgrade can always be envoked.
  • Loading branch information
tjacovich committed Nov 20, 2023
1 parent 0c783e2 commit dc8d126
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions adsmp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from adsputils import get_date
from datetime import datetime
from dateutil.tz import tzutc
from sqlalchemy import Column, Integer, String, Text, TIMESTAMP, Boolean, DateTime
from sqlalchemy import Column, Integer, BigInteger, String, Text, TIMESTAMP, Boolean, DateTime
from sqlalchemy import types
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import Enum
Expand Down Expand Up @@ -118,7 +118,7 @@ def toJSON(self, for_solr=False, load_only=None):

class ChangeLog(Base):
__tablename__ = 'change_log'
id = Column(Integer, primary_key=True)
id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True)
created = Column(UTCDateTime, default=get_date)
key = Column(String(255))
type = Column(String(255))
Expand Down
17 changes: 10 additions & 7 deletions alembic/versions/2d2af8a9c996_upgrade_change_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def upgrade():
op.execute('ALTER TABLE change_log ADD COLUMN big_id BIGINT;')
op.execute('CREATE FUNCTION set_new_id() RETURNS TRIGGER AS\n'\
op.execute('CREATE OR REPLACE FUNCTION set_new_id() RETURNS TRIGGER AS\n'\
'$BODY$\n'\
'BEGIN\n'\
'\t NEW.big_id := NEW.id;\n'\
Expand All @@ -38,18 +38,21 @@ def upgrade():
op.execute("ALTER TABLE change_log RENAME COLUMN big_id TO id;")
op.drop_column('change_log', 'old_id')
op.execute('ALTER SEQUENCE change_log_id_seq as bigint MAXVALUE 9223372036854775807')
op.execute('DROP TRIGGER IF EXISTS set_new_id_trigger ON change_log')
# ### end Alembic commands ###


def downgrade():
op.add_column('change_log', sa.Column('smallid', sa.Integer(), unique=True))
op.add_column('change_log', sa.Column('small_id', sa.Integer(), unique=True))
op.execute('DELETE FROM change_log WHERE id > 2147483647')
op.execute('UPDATE change_log SET smallid=id')
op.alter_column('change_log', 'smallid', nullable=False)
op.execute('UPDATE change_log SET small_id=id')
op.alter_column('change_log', 'small_id', nullable=False)
op.drop_constraint('change_log_pkey', 'change_log', type_='primary')
op.create_primary_key("change_log_pkey", "change_log", ["smallid", ])
op.create_primary_key("change_log_pkey", "change_log", ["small_id", ])
op.execute('ALTER SEQUENCE change_log_id_seq OWNED BY change_log.small_id;')
op.execute("ALTER TABLE change_log ALTER COLUMN small_id SET DEFAULT nextval('change_log_id_seq');")
op.alter_column('change_log', 'id', nullable=False, new_column_name='old_id')
op.alter_column('change_log', 'smallid', nullable=False, new_column_name='id')
op.alter_column('change_log', 'small_id', nullable=False, new_column_name='id')
op.drop_column('change_log', 'old_id')
op.execute('ALTER SEQUENCE change_log_id_seq as integer MAXVALUE 2147483647')
op.execute('ALTER SEQUENCE change_log_id_seq as int MAXVALUE 2147483647')

0 comments on commit dc8d126

Please sign in to comment.