question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to switch to native versioning with existing version tables?

See original GitHub issue

Hi there, I have a big project which has been using sqlalchemy-continuum for a long time. I’m trying to switch to use native versioning so we can do bulk imports and still get versions.

I set the native_versioning option:

make_versioned(
    options={'native_versioning': True}
)

And then ran an alembic migration calling sync_trigger for all the version tables:

conn = op.get_bind()
sync_trigger(conn, 'user_version')
sync_trigger(conn, 'inventory_item_version')
...

I can see in the database that _audit triggers are created for all the tables. However, my tests now always return a length of 0 for list(model.versions) after updating a model. Am I missing a step?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
evryscommented, Mar 19, 2022

I ended up getting it to work by recreating the transaction table with native_versioning enabled and then copying all the old transactions across. I also needed to enable the hstore extension, and fork sqlalchemy-continuum to change a boolean so it no longer assumed the presence of PropertyModTrackerPlugin.

I should make a PR adding some documentation about it and fixing the mod tracker thing. In the meantime this is the migration I used, if someone else encounters this issue:

conn = op.get_bind()

# Enable hstore extension (used by versioning triggers)
op.execute("CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public")

# Rename our existing transaction table out of the way
op.execute("ALTER TABLE transaction RENAME CONSTRAINT transaction_pkey TO old_transaction_pkey")
op.execute("ALTER TABLE transaction RENAME CONSTRAINT transaction_user_id_fkey TO old_transaction_user_id_fkey")
op.execute("ALTER INDEX ix_transaction_user_id RENAME TO old_ix_transaction_user_id")
op.execute("ALTER TABLE transaction RENAME TO old_transaction;")
op.execute("ALTER SEQUENCE transaction_id_seq RENAME TO old_transaction_id_seq;")

# Make a new transaction table (this adds a particular trigger needed for
# native_versioning to work)
Transaction = db.Model._decl_class_registry.get('Transaction')
Transaction.__table__.create(bind=conn)

# Copy our data across
op.execute("INSERT INTO transaction SELECT * FROM old_transaction;")
op.execute("DROP TABLE old_transaction;")

# Restore the old id sequence
op.execute("DROP SEQUENCE transaction_id_seq;")
op.execute("ALTER SEQUENCE old_transaction_id_seq RENAME TO transaction_id_seq;")

# Create all the versioning update triggers
sync_trigger(conn, 'user_version')
sync_trigger(conn, 'inventory_item_version')
...
0reactions
guzzijonescommented, May 26, 2022

Postgres database server version 14 also requires quotes around the version_table_name_format if your table name has capital letters.

sync_trigger(ob.get_bind(), 'mytable_version',
    use_property_mod_tracking=False,
   version_table_name_format="\"%s_version\"")
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add versioning to a existing sql server 2016 table with ...
Below is the table DDL which I use to create the table -Product_Rates_test first. USE TestDB. SET ANSI_NULLS ON GO. SET QUOTED_IDENTIFIER ON...
Read more >
Versioning vocabulary—ArcMap | Documentation
The adds table stores all records inserted to or updated in a versioned dataset ... versioned (the default option) or with the option...
Read more >
Implementing version control using Amazon DynamoDB
Table ('VersionControl') # Add the new state data item ... To model historical versions of data and easily retrieve the most recent version...
Read more >
System-Versioned Tables - MariaDB Knowledge Base
System-versioned tables record the history of all changes to table data. ... An existing table can be altered to enable system versioning for...
Read more >
Database versioning without history tables - Stack Overflow
An update generally does nothing to the master table (unless a static value is actually changed) and a new version of the new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found