kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
582 stars 125 forks source link

Migration function when porting continuum to existing projects #28

Open kvesteri opened 11 years ago

kvesteri commented 11 years ago

When user has an existing project and starts using SA-Continuum the following problems are exposed:

  1. Continuum transaction history is not aware of old object insertions
  2. Since transaction history is not aware of object insertions all relation queries do not return expected results.

For example lets say we have articles and users (each article has one user as owner). The articles and users are already in the database when the developer migrates the whole project to use Continuum. Then user updates article A. When user calls:

article.versions[-1].owner

It returns None, because there is no history yet for this user object.

Solution:

Lets make a migration function which generates history records for all versioned table rows.

dtheodor commented 10 years ago

I've written a script that creates a new history object for all existing versioned object. All these new entries have the operation create and are associated with the transaction with id 1. Is this what you are looking for here?

anthony-arnold commented 5 years ago

@dtheodor Five years later; do you have this script? This is what I'm looking for.

ghost commented 4 years ago

@anthony-arnold this seems to work for me

versioning_manager = VersioningManager(transaction_cls=AuditTransaction)
uow = versioning_manager.unit_of_work(DBSession)
uow.create_transaction(DBSession)
uow.operations.add_insert(obj)
uow.make_versions(DBSession)
dkarp0 commented 4 years ago

Thanks @dvschramm that worked for me also