Skip to content

Commit 5175294

Browse files
Updates for Flask-SQLAlchemy 3.x
1 parent de822d8 commit 5175294

File tree

10 files changed

+68
-27
lines changed

10 files changed

+68
-27
lines changed

src/flask_migrate/templates/flask-multidb/env.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
fileConfig(config.config_file_name)
2020
logger = logging.getLogger('alembic.env')
2121

22+
23+
def get_engine(bind_key=None):
24+
try:
25+
# this works with Flask-SQLAlchemy>=3
26+
return current_app.extensions['migrate'].db.get_engine(
27+
bind_key=bind_key)
28+
except TypeError:
29+
# this works with Flask-SQLAlchemy<3
30+
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
31+
32+
2233
# add your model's MetaData object here
2334
# for 'autogenerate' support
2435
# from myapp import mymodel
@@ -38,8 +49,7 @@
3849
for bind in bind_names:
3950
context.config.set_section_option(
4051
bind, "sqlalchemy.url",
41-
str(current_app.extensions['migrate'].db.get_engine(
42-
bind=bind).url).replace('%', '%%'))
52+
str(get_engine(bind_key=bind).url).replace('%', '%%'))
4353
target_db = current_app.extensions['migrate'].db
4454

4555
# other values from the config, defined by the needs of env.py,
@@ -132,8 +142,7 @@ def process_revision_directives(context, revision, directives):
132142
}
133143
for name in bind_names:
134144
engines[name] = rec = {}
135-
rec['engine'] = current_app.extensions['migrate'].db.get_engine(
136-
bind=name)
145+
rec['engine'] = get_engine(bind_key=name)
137146

138147
for name, rec in engines.items():
139148
engine = rec['engine']

tests/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/bin/env python
2+
import os
23
from flask import Flask
34
from flask_sqlalchemy import SQLAlchemy
45
from flask_migrate import Migrate
56

7+
basedir = os.path.abspath(os.path.dirname(__file__))
8+
69
app = Flask(__name__)
7-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
10+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
11+
basedir, 'app.db')
812
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
913

1014
db = SQLAlchemy(app)

tests/app_compare_type1.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import os
12
from flask import Flask
23
from flask_sqlalchemy import SQLAlchemy
34
from flask_migrate import Migrate
45

6+
basedir = os.path.abspath(os.path.dirname(__file__))
7+
58
app = Flask(__name__)
6-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
9+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
10+
basedir, 'app.db')
711
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
812

913
db = SQLAlchemy(app)

tests/app_compare_type2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import os
12
from flask import Flask
23
from flask_sqlalchemy import SQLAlchemy
34
from flask_migrate import Migrate
45

6+
basedir = os.path.abspath(os.path.dirname(__file__))
7+
58
app = Flask(__name__)
6-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
9+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
10+
basedir, 'app.db')
711
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
812

913
db = SQLAlchemy(app)

tests/app_custom_directory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import os
12
from flask import Flask
23
from flask_sqlalchemy import SQLAlchemy
34
from flask_migrate import Migrate
45

6+
basedir = os.path.abspath(os.path.dirname(__file__))
7+
58
app = Flask(__name__)
6-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
9+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
10+
basedir, 'app.db')
711
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
812

913
db = SQLAlchemy(app)

tests/app_custom_directory_path.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import os
12
from flask import Flask
23
from flask_sqlalchemy import SQLAlchemy
34
from flask_migrate import Migrate
45
from pathlib import Path
56

7+
basedir = os.path.abspath(os.path.dirname(__file__))
8+
69
app = Flask(__name__)
7-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
10+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
11+
basedir, 'app.db')
812
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
913

1014
db = SQLAlchemy(app)

tests/app_multidb.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#!/bin/env python
2+
import os
23
from flask import Flask
34
from flask_sqlalchemy import SQLAlchemy
45
from flask_migrate import Migrate
56

7+
basedir = os.path.abspath(os.path.dirname(__file__))
8+
69
app = Flask(__name__)
7-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app1.db'
10+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
11+
basedir, 'app1.db')
812
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
913
app.config['SQLALCHEMY_BINDS'] = {
10-
"db1": "sqlite:///app2.db",
14+
"db1": "sqlite:///" + os.path.join(basedir, "app2.db"),
1115
}
1216

1317
db = SQLAlchemy(app)

tests/test_custom_template.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ def test_migrate_upgrade(self):
6161
(o, e, s) = run_cmd('app.py', 'flask db upgrade')
6262
self.assertTrue(s == 0)
6363

64-
from .app import db, User
65-
db.session.add(User(name='test'))
66-
db.session.commit()
64+
from .app import app, db, User
65+
with app.app_context():
66+
db.session.add(User(name='test'))
67+
db.session.commit()
6768

6869
with open('migrations/README', 'rt') as f:
6970
assert f.readline().strip() == 'Custom template.'

tests/test_migrate.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ def test_migrate_upgrade(self):
6161
(o, e, s) = run_cmd('app.py', 'flask db upgrade')
6262
self.assertTrue(s == 0)
6363

64-
from .app import db, User
65-
db.session.add(User(name='test'))
66-
db.session.commit()
64+
from .app import app, db, User
65+
with app.app_context():
66+
db.session.add(User(name='test'))
67+
db.session.commit()
6768

6869
def test_custom_directory(self):
6970
(o, e, s) = run_cmd('app_custom_directory.py', 'flask db init')
@@ -73,9 +74,10 @@ def test_custom_directory(self):
7374
(o, e, s) = run_cmd('app_custom_directory.py', 'flask db upgrade')
7475
self.assertTrue(s == 0)
7576

76-
from .app_custom_directory import db, User
77-
db.session.add(User(name='test'))
78-
db.session.commit()
77+
from .app_custom_directory import app, db, User
78+
with app.app_context():
79+
db.session.add(User(name='test'))
80+
db.session.commit()
7981

8082
def test_custom_directory_path(self):
8183
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db init')
@@ -85,9 +87,10 @@ def test_custom_directory_path(self):
8587
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db upgrade')
8688
self.assertTrue(s == 0)
8789

88-
from .app_custom_directory_path import db, User
89-
db.session.add(User(name='test'))
90-
db.session.commit()
90+
from .app_custom_directory_path import app, db, User
91+
with app.app_context():
92+
db.session.add(User(name='test'))
93+
db.session.commit()
9194

9295
def test_compare_type(self):
9396
(o, e, s) = run_cmd('app_compare_type1.py', 'flask db init')

tests/test_multidb_migrate.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def run_cmd(app, cmd):
1212
process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE,
1313
stderr=subprocess.PIPE)
1414
(stdout, stderr) = process.communicate()
15+
print('\n$ ' + cmd)
16+
print(stdout.decode('utf-8'))
17+
print(stderr.decode('utf-8'))
1518
return stdout, stderr, process.wait()
1619

1720

@@ -65,10 +68,11 @@ def test_multidb_migrate_upgrade(self):
6568
self.assertIn(('group',), tables)
6669

6770
# ensure the databases can be written to
68-
from .app_multidb import db, User, Group
69-
db.session.add(User(name='test'))
70-
db.session.add(Group(name='group'))
71-
db.session.commit()
71+
from .app_multidb import app, db, User, Group
72+
with app.app_context():
73+
db.session.add(User(name='test'))
74+
db.session.add(Group(name='group'))
75+
db.session.commit()
7276

7377
# ensure the downgrade works
7478
(o, e, s) = run_cmd('app_multidb.py', 'flask db downgrade')

0 commit comments

Comments
 (0)