Skip to content

Commit

Permalink
allow Path objects to be used as directory parameter
Browse files Browse the repository at this point in the history
Closes #318.
  • Loading branch information
nioncode committed Mar 13, 2020
1 parent 1b6355c commit c4f2ddb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions flask_migrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get_config(self, directory=None, x_arg=None, opts=None):
if directory is None:
directory = self.directory
config = Config(os.path.join(directory, 'alembic.ini'))
config.set_main_option('script_location', directory)
config.set_main_option('script_location', str(directory))
if config.cmd_opts is None:
config.cmd_opts = argparse.Namespace()
for opt in opts or []:
Expand Down Expand Up @@ -124,7 +124,7 @@ def init(directory=None, multidb=False):
if directory is None:
directory = current_app.extensions['migrate'].directory
config = Config()
config.set_main_option('script_location', directory)
config.set_main_option('script_location', str(directory))
config.config_file_name = os.path.join(directory, 'alembic.ini')
config = current_app.extensions['migrate'].\
migrate.call_configure_callbacks(config)
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 c4f2ddb

Please sign in to comment.