From a1787cf18fb4d5ec7369280afe1a59349f7544b8 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 15 May 2021 15:39:02 +0100 Subject: [PATCH] Remove support for Flask-Script (Fixes #403) --- flask_migrate/__init__.py | 168 ------------------------- tests/app.py | 11 +- tests/app_compare_type1.py | 8 +- tests/app_compare_type2.py | 8 +- tests/app_custom_directory.py | 11 +- tests/app_custom_directory_path.py | 11 +- tests/app_multidb.py | 11 +- tests/test_migrate.py | 64 +++++----- tests/test_migrate_flaskcli.py | 102 --------------- tests/test_multidb_migrate.py | 25 ++-- tests/test_multidb_migrate_flaskcli.py | 91 -------------- tox.ini | 1 - 12 files changed, 63 insertions(+), 448 deletions(-) mode change 100644 => 100755 tests/app_custom_directory_path.py delete mode 100644 tests/test_migrate_flaskcli.py delete mode 100644 tests/test_multidb_migrate_flaskcli.py diff --git a/flask_migrate/__init__.py b/flask_migrate/__init__.py index 45877d0..8b367a7 100644 --- a/flask_migrate/__init__.py +++ b/flask_migrate/__init__.py @@ -4,10 +4,6 @@ import os import sys from flask import current_app -try: - from flask_script import Manager -except ImportError: - Manager = None from alembic import __version__ as __alembic_version__ from alembic.config import Config as AlembicConfig from alembic import command @@ -100,25 +96,6 @@ def wrapped(*args, **kwargs): return wrapped -if Manager is not None: - MigrateCommand = Manager(usage='Perform database migrations') -else: - class FakeCommand(object): - def option(self, *args, **kwargs): - def decorator(f): - return f - return decorator - - MigrateCommand = FakeCommand() - - -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) -@MigrateCommand.option('--multidb', dest='multidb', action='store_true', - default=False, - help=("Multiple databases migraton (default is " - "False)")) @catch_errors def init(directory=None, multidb=False): """Creates a new migration repository""" @@ -135,35 +112,6 @@ def init(directory=None, multidb=False): command.init(config, directory, 'flask') -@MigrateCommand.option('--rev-id', dest='rev_id', default=None, - help=('Specify a hardcoded revision id instead of ' - 'generating one')) -@MigrateCommand.option('--version-path', dest='version_path', default=None, - help=('Specify specific path from config for version ' - 'file')) -@MigrateCommand.option('--branch-label', dest='branch_label', default=None, - help=('Specify a branch label to apply to the new ' - 'revision')) -@MigrateCommand.option('--splice', dest='splice', action='store_true', - default=False, - help=('Allow a non-head revision as the "head" to ' - 'splice onto')) -@MigrateCommand.option('--head', dest='head', default='head', - help=('Specify head revision or @head to ' - 'base new revision on')) -@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False, - help=("Don't emit SQL to database - dump to standard " - "output instead")) -@MigrateCommand.option('--autogenerate', dest='autogenerate', - action='store_true', default=False, - help=('Populate revision script with candidate ' - 'migration operations, based on comparison of ' - 'database to model')) -@MigrateCommand.option('-m', '--message', dest='message', default=None, - help='Revision message') -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def revision(directory=None, message=None, autogenerate=False, sql=False, head='head', splice=False, branch_label=None, version_path=None, @@ -175,32 +123,6 @@ def revision(directory=None, message=None, autogenerate=False, sql=False, version_path=version_path, rev_id=rev_id) -@MigrateCommand.option('--rev-id', dest='rev_id', default=None, - help=('Specify a hardcoded revision id instead of ' - 'generating one')) -@MigrateCommand.option('--version-path', dest='version_path', default=None, - help=('Specify specific path from config for version ' - 'file')) -@MigrateCommand.option('--branch-label', dest='branch_label', default=None, - help=('Specify a branch label to apply to the new ' - 'revision')) -@MigrateCommand.option('--splice', dest='splice', action='store_true', - default=False, - help=('Allow a non-head revision as the "head" to ' - 'splice onto')) -@MigrateCommand.option('--head', dest='head', default='head', - help=('Specify head revision or @head to ' - 'base new revision on')) -@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False, - help=("Don't emit SQL to database - dump to standard " - "output instead")) -@MigrateCommand.option('-m', '--message', dest='message', default=None) -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) -@MigrateCommand.option('-x', '--x-arg', dest='x_arg', default=None, - action='append', help=("Additional arguments consumed " - "by custom env.py scripts")) @catch_errors def migrate(directory=None, message=None, sql=False, head='head', splice=False, branch_label=None, version_path=None, rev_id=None, x_arg=None): @@ -212,11 +134,6 @@ def migrate(directory=None, message=None, sql=False, head='head', splice=False, version_path=version_path, rev_id=rev_id) -@MigrateCommand.option('revision', nargs='?', default='head', - help="revision identifier") -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def edit(directory=None, revision='current'): """Edit current revision.""" @@ -228,18 +145,6 @@ def edit(directory=None, revision='current'): raise RuntimeError('Alembic 0.8.0 or greater is required') -@MigrateCommand.option('--rev-id', dest='rev_id', default=None, - help=('Specify a hardcoded revision id instead of ' - 'generating one')) -@MigrateCommand.option('--branch-label', dest='branch_label', default=None, - help=('Specify a branch label to apply to the new ' - 'revision')) -@MigrateCommand.option('-m', '--message', dest='message', default=None) -@MigrateCommand.option('revisions', nargs='+', - help='one or more revisions, or "heads" for all heads') -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def merge(directory=None, revisions='', message=None, branch_label=None, rev_id=None): @@ -249,20 +154,6 @@ def merge(directory=None, revisions='', message=None, branch_label=None, branch_label=branch_label, rev_id=rev_id) -@MigrateCommand.option('--tag', dest='tag', default=None, - help=("Arbitrary 'tag' name - can be used by custom " - "env.py scripts")) -@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False, - help=("Don't emit SQL to database - dump to standard " - "output instead")) -@MigrateCommand.option('revision', nargs='?', default='head', - help="revision identifier") -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) -@MigrateCommand.option('-x', '--x-arg', dest='x_arg', default=None, - action='append', help=("Additional arguments consumed " - "by custom env.py scripts")) @catch_errors def upgrade(directory=None, revision='head', sql=False, tag=None, x_arg=None): """Upgrade to a later version""" @@ -271,20 +162,6 @@ def upgrade(directory=None, revision='head', sql=False, tag=None, x_arg=None): command.upgrade(config, revision, sql=sql, tag=tag) -@MigrateCommand.option('--tag', dest='tag', default=None, - help=("Arbitrary 'tag' name - can be used by custom " - "env.py scripts")) -@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False, - help=("Don't emit SQL to database - dump to standard " - "output instead")) -@MigrateCommand.option('revision', nargs='?', default="-1", - help="revision identifier") -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) -@MigrateCommand.option('-x', '--x-arg', dest='x_arg', default=None, - action='append', help=("Additional arguments consumed " - "by custom env.py scripts")) @catch_errors def downgrade(directory=None, revision='-1', sql=False, tag=None, x_arg=None): """Revert to a previous version""" @@ -295,11 +172,6 @@ def downgrade(directory=None, revision='-1', sql=False, tag=None, x_arg=None): command.downgrade(config, revision, sql=sql, tag=tag) -@MigrateCommand.option('revision', nargs='?', default="head", - help="revision identifier") -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def show(directory=None, revision='head'): """Show the revision denoted by the given symbol.""" @@ -307,18 +179,6 @@ def show(directory=None, revision='head'): command.show(config, revision) -@MigrateCommand.option('-i', '--indicate-current', dest='indicate_current', - action='store_true', default=False, - help=('Indicate current version (Alembic 0.9.9 or ' - 'greater is required)')) -@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true', - default=False, help='Use more verbose output') -@MigrateCommand.option('-r', '--rev-range', dest='rev_range', default=None, - help=('Specify a revision range; format is ' - '[start]:[end]')) -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def history(directory=None, rev_range=None, verbose=False, indicate_current=False): @@ -331,14 +191,6 @@ def history(directory=None, rev_range=None, verbose=False, command.history(config, rev_range, verbose=verbose) -@MigrateCommand.option('--resolve-dependencies', dest='resolve_dependencies', - action='store_true', default=False, - help='Treat dependency versions as down revisions') -@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true', - default=False, help='Use more verbose output') -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def heads(directory=None, verbose=False, resolve_dependencies=False): """Show current available heads in the script directory""" @@ -347,11 +199,6 @@ def heads(directory=None, verbose=False, resolve_dependencies=False): resolve_dependencies=resolve_dependencies) -@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true', - default=False, help='Use more verbose output') -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def branches(directory=None, verbose=False): """Show current branch points""" @@ -359,11 +206,6 @@ def branches(directory=None, verbose=False): command.branches(config, verbose=verbose) -@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true', - default=False, help='Use more verbose output') -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def current(directory=None, verbose=False): """Display the current revision for each database.""" @@ -371,16 +213,6 @@ def current(directory=None, verbose=False): command.current(config, verbose=verbose) -@MigrateCommand.option('--tag', dest='tag', default=None, - help=("Arbitrary 'tag' name - can be used by custom " - "env.py scripts")) -@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False, - help=("Don't emit SQL to database - dump to standard " - "output instead")) -@MigrateCommand.option('revision', default=None, help="revision identifier") -@MigrateCommand.option('-d', '--directory', dest='directory', default=None, - help=("Migration script directory (default is " - "'migrations')")) @catch_errors def stamp(directory=None, revision='head', sql=False, tag=None): """'stamp' the revision table with the given revision; don't run any diff --git a/tests/app.py b/tests/app.py index c11349a..e2e2aa2 100755 --- a/tests/app.py +++ b/tests/app.py @@ -1,8 +1,7 @@ #!/bin/env python from flask import Flask from flask_sqlalchemy import SQLAlchemy -from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand +from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' @@ -11,20 +10,18 @@ db = SQLAlchemy(app) migrate = Migrate(app, db) -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 +@app.cli.command() def add(): + """Add test user.""" db.session.add(User(name='test')) db.session.commit() if __name__ == '__main__': - manager.run() + app.run() diff --git a/tests/app_compare_type1.py b/tests/app_compare_type1.py index 2cd963f..fcd0e87 100755 --- a/tests/app_compare_type1.py +++ b/tests/app_compare_type1.py @@ -1,7 +1,6 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand +from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' @@ -10,9 +9,6 @@ db = SQLAlchemy(app) migrate = Migrate(app, db, compare_type=True) -manager = Manager(app) -manager.add_command('db', MigrateCommand) - class User(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -20,4 +16,4 @@ class User(db.Model): if __name__ == '__main__': - manager.run() + app.run() diff --git a/tests/app_compare_type2.py b/tests/app_compare_type2.py index 4dd2d0c..95c33ae 100755 --- a/tests/app_compare_type2.py +++ b/tests/app_compare_type2.py @@ -1,7 +1,6 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand +from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' @@ -10,9 +9,6 @@ db = SQLAlchemy(app) migrate = Migrate(app, db, compare_type=True) -manager = Manager(app) -manager.add_command('db', MigrateCommand) - class User(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -20,4 +16,4 @@ class User(db.Model): if __name__ == '__main__': - manager.run() + app.run() diff --git a/tests/app_custom_directory.py b/tests/app_custom_directory.py index 39fdd9f..af085b4 100755 --- a/tests/app_custom_directory.py +++ b/tests/app_custom_directory.py @@ -1,7 +1,6 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand +from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' @@ -10,20 +9,18 @@ db = SQLAlchemy(app) migrate = Migrate(app, db, directory='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 +@app.cli.command() def add(): + """Add test user.""" db.session.add(User(name='test')) db.session.commit() if __name__ == '__main__': - manager.run() + app.run() diff --git a/tests/app_custom_directory_path.py b/tests/app_custom_directory_path.py old mode 100644 new mode 100755 index 083cad4..f55e389 --- a/tests/app_custom_directory_path.py +++ b/tests/app_custom_directory_path.py @@ -1,7 +1,6 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand +from flask_migrate import Migrate from pathlib import Path app = Flask(__name__) @@ -11,20 +10,18 @@ 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 +@app.cli.command() def add(): + """Add test user.""" db.session.add(User(name='test')) db.session.commit() if __name__ == '__main__': - manager.run() + app.run() diff --git a/tests/app_multidb.py b/tests/app_multidb.py index ac9acc3..01809a8 100755 --- a/tests/app_multidb.py +++ b/tests/app_multidb.py @@ -1,8 +1,7 @@ #!/bin/env python from flask import Flask from flask_sqlalchemy import SQLAlchemy -from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand +from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app1.db' @@ -27,16 +26,14 @@ class Group(db.Model): migrate = Migrate(app, db) -manager = Manager(app) -manager.add_command('db', MigrateCommand) - -@manager.command +@app.cli.command() def add(): + """Add test users.""" db.session.add(User(name='test')) db.session.add(Group(name='group')) db.session.commit() if __name__ == '__main__': - manager.run() + app.run() diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 74721c8..58645a9 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -1,17 +1,17 @@ import os -import shlex import shutil -import subprocess -import sys import unittest +import subprocess +import shlex -def run_cmd(cmd): +def run_cmd(app, cmd): """Run a command and return a tuple with (stdout, stderr, exit_code)""" - print('\n$ ' + cmd) + os.environ['FLASK_APP'] = app process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() + print('\n$ ' + cmd) print(stdout.decode('utf-8')) print(stderr.decode('utf-8')) return stdout, stderr, process.wait() @@ -54,53 +54,49 @@ def test_alembic_version(self): self.assertTrue(isinstance(v, int)) def test_migrate_upgrade(self): - (o, e, s) = run_cmd(sys.executable + ' app.py db init') + (o, e, s) = run_cmd('app.py', 'flask db init') self.assertTrue(s == 0) - (o, e, s) = run_cmd(sys.executable + ' app.py db migrate') + (o, e, s) = run_cmd('app.py', 'flask db migrate') self.assertTrue(s == 0) - (o, e, s) = run_cmd(sys.executable + ' app.py db upgrade') - self.assertTrue(s == 0) - (o, e, s) = run_cmd(sys.executable + ' app.py add') + (o, e, s) = run_cmd('app.py', 'flask db upgrade') self.assertTrue(s == 0) + from .app import db, User + db.session.add(User(name='test')) + db.session.commit() + def test_custom_directory(self): - (o, e, s) = run_cmd( - sys.executable + ' app_custom_directory.py db init') - self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_custom_directory.py db migrate') + (o, e, s) = run_cmd('app_custom_directory.py', 'flask db init') self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_custom_directory.py db upgrade') + (o, e, s) = run_cmd('app_custom_directory.py', 'flask db migrate') self.assertTrue(s == 0) - (o, e, s) = run_cmd(sys.executable + ' app_custom_directory.py add') + (o, e, s) = run_cmd('app_custom_directory.py', 'flask db upgrade') self.assertTrue(s == 0) + from .app_custom_directory import db, User + db.session.add(User(name='test')) + db.session.commit() + 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') + (o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db init') self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_custom_directory_path.py db upgrade') + (o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db migrate') self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_custom_directory_path.py add') + (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(sys.executable + ' app_compare_type1.py db init') + (o, e, s) = run_cmd('app_compare_type1.py', 'flask db init') self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_compare_type1.py db migrate') + (o, e, s) = run_cmd('app_compare_type1.py', 'flask db migrate') self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_compare_type1.py db upgrade') + (o, e, s) = run_cmd('app_compare_type1.py', 'flask db upgrade') self.assertTrue(s == 0) - (o, e, s) = run_cmd( - sys.executable + ' app_compare_type2.py db migrate') + (o, e, s) = run_cmd('app_compare_type2.py', 'flask db migrate') self.assertTrue(s == 0) self.assertTrue(b'Detected type change from VARCHAR(length=128) ' b'to String(length=10)' in e) diff --git a/tests/test_migrate_flaskcli.py b/tests/test_migrate_flaskcli.py deleted file mode 100644 index 58645a9..0000000 --- a/tests/test_migrate_flaskcli.py +++ /dev/null @@ -1,102 +0,0 @@ -import os -import shutil -import unittest -import subprocess -import shlex - - -def run_cmd(app, cmd): - """Run a command and return a tuple with (stdout, stderr, exit_code)""" - os.environ['FLASK_APP'] = app - process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - (stdout, stderr) = process.communicate() - print('\n$ ' + cmd) - print(stdout.decode('utf-8')) - print(stderr.decode('utf-8')) - return stdout, stderr, process.wait() - - -class TestMigrate(unittest.TestCase): - def setUp(self): - os.chdir(os.path.split(os.path.abspath(__file__))[0]) - try: - os.remove('app.db') - except OSError: - pass - try: - shutil.rmtree('migrations') - except OSError: - pass - try: - shutil.rmtree('temp_folder') - except OSError: - pass - - def tearDown(self): - try: - os.remove('app.db') - except OSError: - pass - try: - shutil.rmtree('migrations') - except OSError: - pass - try: - shutil.rmtree('temp_folder') - except OSError: - pass - - def test_alembic_version(self): - from flask_migrate import alembic_version - self.assertEqual(len(alembic_version), 3) - for v in alembic_version: - self.assertTrue(isinstance(v, int)) - - def test_migrate_upgrade(self): - (o, e, s) = run_cmd('app.py', 'flask db init') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app.py', 'flask db migrate') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app.py', 'flask db upgrade') - self.assertTrue(s == 0) - - from .app import db, User - db.session.add(User(name='test')) - db.session.commit() - - def test_custom_directory(self): - (o, e, s) = run_cmd('app_custom_directory.py', 'flask db init') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_custom_directory.py', 'flask db migrate') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_custom_directory.py', 'flask db upgrade') - self.assertTrue(s == 0) - - from .app_custom_directory import db, User - 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) - (o, e, s) = run_cmd('app_compare_type1.py', 'flask db migrate') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_compare_type1.py', 'flask db upgrade') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_compare_type2.py', 'flask db migrate') - self.assertTrue(s == 0) - self.assertTrue(b'Detected type change from VARCHAR(length=128) ' - b'to String(length=10)' in e) diff --git a/tests/test_multidb_migrate.py b/tests/test_multidb_migrate.py index b026ab3..26be8a4 100644 --- a/tests/test_multidb_migrate.py +++ b/tests/test_multidb_migrate.py @@ -1,14 +1,14 @@ import os -import shlex import shutil -import sqlite3 -import subprocess -import sys import unittest +import subprocess +import shlex +import sqlite3 -def run_cmd(cmd): +def run_cmd(app, cmd): """Run a command and return a tuple with (stdout, stderr, exit_code)""" + os.environ['FLASK_APP'] = app process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() @@ -40,12 +40,11 @@ def tearDown(self): pass def test_multidb_migrate_upgrade(self): - (o, e, s) = run_cmd( - sys.executable + ' app_multidb.py db init --multidb') + (o, e, s) = run_cmd('app_multidb.py', 'flask db init --multidb') self.assertTrue(s == 0) - (o, e, s) = run_cmd(sys.executable + ' app_multidb.py db migrate') + (o, e, s) = run_cmd('app_multidb.py', 'flask db migrate') self.assertTrue(s == 0) - (o, e, s) = run_cmd(sys.executable + ' app_multidb.py db upgrade') + (o, e, s) = run_cmd('app_multidb.py', 'flask db upgrade') self.assertTrue(s == 0) # ensure the tables are in the correct databases @@ -66,11 +65,13 @@ def test_multidb_migrate_upgrade(self): self.assertIn(('group',), tables) # ensure the databases can be written to - (o, e, s) = run_cmd(sys.executable + ' app_multidb.py add') - self.assertTrue(s == 0) + from .app_multidb import db, User, Group + db.session.add(User(name='test')) + db.session.add(Group(name='group')) + db.session.commit() # ensure the downgrade works - (o, e, s) = run_cmd(sys.executable + ' app_multidb.py db downgrade') + (o, e, s) = run_cmd('app_multidb.py', 'flask db downgrade') self.assertTrue(s == 0) conn1 = sqlite3.connect('app1.db') diff --git a/tests/test_multidb_migrate_flaskcli.py b/tests/test_multidb_migrate_flaskcli.py deleted file mode 100644 index 26be8a4..0000000 --- a/tests/test_multidb_migrate_flaskcli.py +++ /dev/null @@ -1,91 +0,0 @@ -import os -import shutil -import unittest -import subprocess -import shlex -import sqlite3 - - -def run_cmd(app, cmd): - """Run a command and return a tuple with (stdout, stderr, exit_code)""" - os.environ['FLASK_APP'] = app - process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - (stdout, stderr) = process.communicate() - return stdout, stderr, process.wait() - - -class TestMigrate(unittest.TestCase): - def setUp(self): - os.chdir(os.path.split(os.path.abspath(__file__))[0]) - try: - os.remove('app1.db') - os.remove('app2.db') - except OSError: - pass - try: - shutil.rmtree('migrations') - except OSError: - pass - - def tearDown(self): - try: - os.remove('app1.db') - os.remove('app2.db') - except OSError: - pass - try: - shutil.rmtree('migrations') - except OSError: - pass - - def test_multidb_migrate_upgrade(self): - (o, e, s) = run_cmd('app_multidb.py', 'flask db init --multidb') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_multidb.py', 'flask db migrate') - self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_multidb.py', 'flask db upgrade') - self.assertTrue(s == 0) - - # ensure the tables are in the correct databases - conn1 = sqlite3.connect('app1.db') - c = conn1.cursor() - c.execute('select name from sqlite_master') - tables = c.fetchall() - conn1.close() - self.assertIn(('alembic_version',), tables) - self.assertIn(('user',), tables) - - conn2 = sqlite3.connect('app2.db') - c = conn2.cursor() - c.execute('select name from sqlite_master') - tables = c.fetchall() - conn2.close() - self.assertIn(('alembic_version',), tables) - self.assertIn(('group',), tables) - - # ensure the databases can be written to - from .app_multidb import db, User, Group - db.session.add(User(name='test')) - db.session.add(Group(name='group')) - db.session.commit() - - # ensure the downgrade works - (o, e, s) = run_cmd('app_multidb.py', 'flask db downgrade') - self.assertTrue(s == 0) - - conn1 = sqlite3.connect('app1.db') - c = conn1.cursor() - c.execute('select name from sqlite_master') - tables = c.fetchall() - conn1.close() - self.assertIn(('alembic_version',), tables) - self.assertNotIn(('user',), tables) - - conn2 = sqlite3.connect('app2.db') - c = conn2.cursor() - c.execute('select name from sqlite_master') - tables = c.fetchall() - conn2.close() - self.assertIn(('alembic_version',), tables) - self.assertNotIn(('group',), tables) diff --git a/tox.ini b/tox.ini index e273785..aee7890 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,6 @@ python = [testenv] deps= - flask-script commands= python -m unittest discover