Skip to content

Commit

Permalink
Allow Path objects to be used as directory parameter (#319)
Browse files Browse the repository at this point in the history
Closes #318.
  • Loading branch information
nioncode authored Mar 14, 2020
1 parent 1b6355c commit 399cb28
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions flask_migrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class Migrate(object):
def __init__(self, app=None, db=None, directory='migrations', **kwargs):
self.configure_callbacks = []
self.db = db
self.directory = directory
self.directory = str(directory)
self.alembic_ctx_kwargs = kwargs
if app is not None and db is not None:
self.init_app(app, db, directory)

def init_app(self, app, db=None, directory=None, **kwargs):
self.db = db or self.db
self.directory = directory or self.directory
self.directory = str(directory or self.directory)
self.alembic_ctx_kwargs.update(kwargs)
if not hasattr(app, 'extensions'):
app.extensions = {}
Expand All @@ -69,6 +69,7 @@ def call_configure_callbacks(self, config):
def get_config(self, directory=None, x_arg=None, opts=None):
if directory is None:
directory = self.directory
directory = str(directory)
config = Config(os.path.join(directory, 'alembic.ini'))
config.set_main_option('script_location', directory)
if config.cmd_opts is None:
Expand Down
30 changes: 30 additions & 0 deletions tests/app_custom_directory_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from pathlib import Path

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

db = SQLAlchemy(app)
migrate = Migrate(app, db, directory=Path('temp_folder/temp_migrations'))

manager = Manager(app)
manager.add_command('db', MigrateCommand)


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


@manager.command
def add():
db.session.add(User(name='test'))
db.session.commit()


if __name__ == '__main__':
manager.run()
10 changes: 10 additions & 0 deletions tests/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def test_custom_directory(self):
(o, e, s) = run_cmd(sys.executable + ' app_custom_directory.py add')
self.assertTrue(s == 0)

def test_custom_directory_path(self):
(o, e, s) = run_cmd(sys.executable + ' app_custom_directory_path.py db init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd(sys.executable + ' app_custom_directory_path.py db migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd(sys.executable + ' app_custom_directory_path.py db upgrade')
self.assertTrue(s == 0)
(o, e, s) = run_cmd(sys.executable + ' app_custom_directory_path.py add')
self.assertTrue(s == 0)

def test_compare_type(self):
(o, e, s) = run_cmd(sys.executable + ' app_compare_type1.py db init')
self.assertTrue(s == 0)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_migrate_flaskcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def test_custom_directory(self):
db.session.add(User(name='test'))
db.session.commit()

def test_custom_directory_path(self):
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db upgrade')
self.assertTrue(s == 0)

from .app_custom_directory_path import db, User
db.session.add(User(name='test'))
db.session.commit()

def test_compare_type(self):
(o, e, s) = run_cmd('app_compare_type1.py', 'flask db init')
self.assertTrue(s == 0)
Expand Down

0 comments on commit 399cb28

Please sign in to comment.