3

With this setup:

-Development environment
-Flask
-SQLAlchemy
-Postgres
-Possibility Alembic

If I have a database with some tables populated with random data. As far as I know the Flask-Migrate, that will use Alembic, will not preserve the data, only keep the models and database synchronized.

But what is the difference between the use of Alembic or just delete > create all tables?

Something like:

db.create_all()

The second question:

What happens to the data when something change in models? The data will be lost? Or the Alembic can preserve the previous populated data?

Well, my idea is just populate the database with some data, and then avoid any lost of data when the models change. Alembic is the solution?

Or I need to import the data, from a .sql file, for example, when I change the models and database?

user2990084
  • 2,699
  • 9
  • 31
  • 46

1 Answers1

16

I am the Flask-Migrate author.

You are not correct. Flask-Migrate (through Alembic) will always preserve the data that you have in your database. That is the whole point of working with database migrations, you do not want to lose your data.

It seems you already have a database with data in it and you want to start using migrations. You have two options to incorporate Flask-Migrate into your project:

  1. Only track migrations going forward, i.e. leave your initial database schema outside of migration tracking.

    For this you really have nothing special to do. Just do manage.py db init to create the migrations repository and when you need to migrate your database do so normally with manage.py db migrate. The disadvantage of this method is that Flask-Migrate/Alembic do not have the initial schema of the database, so it is not possible to recreate a database from scratch.

  2. Implement an initial migration that brings your database to your current state, then continue tracking future migrations normally.

    This requires a little bit of trick. Here you want Alembic to record an initial migration that defines your current schema. Since Alembic creates migrations by comparing your models to your database, the trick is to replace your real database with an empty database and then generate a migration. After the initial migration is recorded, you restore your database, and from then on you can continue migrating your database normally.

I hope this helps. Let me know if you have any more questions.

Community
  • 1
  • 1
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thanks for the answer and corrections. Your book about flask is really nice too. Thanks – user2990084 Oct 13 '14 at 14:24
  • Miguel, the migrations didn't recognize something like this `from app.users.models import User`? If i put the model directly in the file manage.py, for example, all works great, but if i import, the model is ignored. To debug, a print of the User show the sqlalchemy object, so it is correctly imported. – user2990084 Oct 15 '14 at 23:19
  • Make sure you import your models before you initialize SQLAlchemy. – Miguel Grinberg Oct 15 '14 at 23:25
  • ok, I found the error. I am generating a new db = SQLAlchemy(app), and I just need to have `from app import db`. Then, i can avoid the import of the models because they will be recognized immediatly. A Big thanks! – user2990084 Oct 15 '14 at 23:36
  • In this case, if I don't care about the data, I could just recreate the database through `db.drop_all()` and `db.create_all()`, is that right? I can write a `flask init` command to recreate the database and generate random data, it's quite easy (better than `flask db migrate` etc.). Is it a bad practice? Say in other words, what is the advantage to migrate database in developing instead of preserving the data. – Grey Li Jul 07 '17 at 15:43
  • 1
    @GreyLi if you don't care about the data then you don't need database migrations. Just regenerate your entire database any time you need to change something. That's a lot easier, if you are lucky enough to not need to preserve existing data. – Miguel Grinberg Jul 07 '17 at 21:36