bprinty / Flask-Continuum

Model provenance and versioning via SQLAlchemy-Continuum
https://flask-continuum.readthedocs.io/en/latest/
MIT License
6 stars 3 forks source link

using flask-migrate #10

Closed carloshanson closed 4 years ago

carloshanson commented 4 years ago

I just added Flask-Continuum to a project using flask-migrate and received the following:

flask db migrate -m 'Add continuum.'
[...]
INFO  [alembic.env] No changes in schema detected.

Using the sqlalchemy-continuum documentation, I added the following:

from sqlalchemy import orm
[...]
orm.configure_mappers()

This allowed a successful migration:

flask db migrate -m 'Add continuum.'
[...]
INFO  [alembic.autogenerate.compare] Detected added table 'transaction'
[...]

The following is my test app. I used the flask-migrate documentation as the starting point, then added Flask-Continuum.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_continuum import Continuum, VersioningMixin
from flask_migrate import Migrate
from sqlalchemy import orm

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'

db = SQLAlchemy(app)
continuum = Continuum(app, db)
migrate = Migrate(app, db)

class User(VersioningMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(123))                                               

orm.configure_mappers()
bprinty commented 4 years ago

Thanks for filing a ticket!

Normally, orm.configure_mappers() is called when an application context is pushed by flask and a database connection is made. However, alembic (what Flask-Migrate is using) isn't pushing the application context when it generates migrations. So, I had to make a change to:

  1. Allow users to be able to configure mappers manually via continuum.configure (similar to your fix above).
  2. Allow users to pass the migrate object into Continuum so this package can automatically configure the mappers before alembic commands are issued.

These changes are in the new 0.1.6 release on pypi, and associated documentation can be found here:

https://flask-continuum.readthedocs.io/en/latest/usage.html#migrations