Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask : changing location of 'migrations' folder

I have my Flask project hierarchy a

project
├── controllers
└── models
    └── schema.py

When I run python schema.py db init, a migrations folder is added under project instead of under models. I have a __init__.py under all 3 folders (not showing here for brevity). I want the migrations folder generated under models. How do I do it?

like image 670
Aayush Karki Avatar asked Jun 15 '17 08:06

Aayush Karki


People also ask

How do I create a migration repository in flask?

With the above application you can create a migration repository with the following command: This will add a migrations folder to your application. The contents of this folder need to be added to version control along with your other source files. $ flask db migrate -m "Initial migration."

What is the difference between migrate and migratecommand in flask?

The classes which are exposed are Migrate and MigrateCommand. Migrate class itself covers all the functionality and utility of the extension whereas MigrateCommand is used when there is a need of exposing the database migration commands through the usage of the Flask script extension.

How to migrate from flask to Python?

Installation of the Flask migrate module in Python environment: Initializing the migrate instance: Adding the MigrateCommand into the list of commands in the manager: In conclusion, in this article, we have learned about the migrate configuration and application in Flask.

What is the flask-migrate extension?

Flask-Migrate is an extension that configures Alembic in the proper way to work with your Flask and Flask-SQLAlchemy application. In terms of the actual database migrations, everything is handled by Alembic so you get exactly the same functionality. This is an example application that handles database migrations through Flask-Migrate:


3 Answers

Well.. Like Oluwafemi said, you can pass the -d (--directory) flag to your manager script in the cli command

python schema.py db init --directory models/migrations

The problem with this solution is that you will have to specify the migration path each time you will enter a db command, otherwise you will get the following error:

alembic.util.exc.CommandError: Path doesn't exist: 'migrations'.  Please use the 'init' command to create a new scripts folder.

A better way to config your migration path is by passing the directory argument to the Migrate object in your manage.py (or in your case, the 'schema.py').

For example:

import os
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from .application import app, db


MIGRATION_DIR = os.path.join('models', 'migrations')


migrate = Migrate(app, db, directory=MIGRATION_DIR)
manager = Manager(app)

manager.add_command('db', MigrateCommand)


if __name__ == '__main__':
    manager.run()

Besides, I suggest you move all the command interface functionality from 'schema.py' to a diffrent python script in your application root path (like, 'manager.py') and create the migrations folder next to your models and not inside it. The models folder should not contains anything else than the Data Model Objects!

Hope it was helpful..

like image 97
AlonP Avatar answered Oct 24 '22 20:10

AlonP


You need to pass the directory option to the init command. This can be the path to the migrations directory. It is set to migrations by default.

python schema.py db init --directory models/migrations

Reference:

  • API Reference Section https://flask-migrate.readthedocs.io/en/latest/
like image 41
Oluwafemi Sule Avatar answered Oct 24 '22 21:10

Oluwafemi Sule


Or you can update the location of scripts location in alembic.ini

# path to migration scripts
script_location = alembic
like image 39
Arvind Chinniah Avatar answered Oct 24 '22 21:10

Arvind Chinniah