Skip to content

Commit b8fdb7f

Browse files
committed
add support for migrations with multiple statements
1 parent e12df21 commit b8fdb7f

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

00_core.ipynb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"from fastcore.xtras import dataclass_src\n",
4040
"from itertools import starmap\n",
4141
"\n",
42-
"import sqlalchemy as sa, subprocess"
42+
"import sqlparse, sqlalchemy as sa, subprocess"
4343
]
4444
},
4545
{
@@ -2675,7 +2675,7 @@
26752675
{
26762676
"data": {
26772677
"text/plain": [
2678-
"[(1, Path('migrations/1-add_priority_to_todo.sql'))]"
2678+
"[(1, Path('migrations/1-add_priority_and_due_date_to_todo.sql'))]"
26792679
]
26802680
},
26812681
"execution_count": null,
@@ -2702,7 +2702,8 @@
27022702
" cver = self.version\n",
27032703
" for v, p in _get_migrations(mdir)[self.version:]:\n",
27042704
" try:\n",
2705-
" if p.suffix == '.sql': self.execute(sa.text(p.read_text()))\n",
2705+
" if p.suffix == '.sql':\n",
2706+
" for stmt in filter(str.strip, sqlparse.split(p.read_text())): self.execute(sa.text(stmt))\n",
27062707
" elif p.suffix == '.py':\n",
27072708
" subprocess.run([sys.executable, p, self.conn_str], check=True)\n",
27082709
" self.version = v\n",
@@ -2739,14 +2740,15 @@
27392740
"name": "stdout",
27402741
"output_type": "stream",
27412742
"text": [
2742-
"Applied migration 1: 1-add_priority_to_todo.sql\n"
2743+
"Applied migration 1: 1-add_priority_and_due_date_to_todo.sql\n"
27432744
]
27442745
}
27452746
],
27462747
"source": [
27472748
"db.migrate(mdir)\n",
27482749
"test_eq(db.version, 1)\n",
2749-
"assert 'priority' in db.t.todo.c"
2750+
"assert 'priority' in db.t.todo.c\n",
2751+
"assert 'due_date' in db.t.todo.c"
27502752
]
27512753
},
27522754
{
@@ -2758,8 +2760,8 @@
27582760
{
27592761
"data": {
27602762
"text/plain": [
2761-
"[Todo(title='get it done', name='rlt', id=2, done=False, details='', priority=0),\n",
2762-
" Todo(title='get it done', name='rlt', id=4, done=False, details='', priority=0)]"
2763+
"[Todo(title='get it done', name='rlt', id=2, done=False, details='', priority=0, due_date=None),\n",
2764+
" Todo(title='get it done', name='rlt', id=4, done=False, details='', priority=0, due_date=None)]"
27632765
]
27642766
},
27652767
"execution_count": null,
@@ -2842,7 +2844,7 @@
28422844
{
28432845
"data": {
28442846
"text/plain": [
2845-
"'user todo pending_todos'"
2847+
"'_meta todo user'"
28462848
]
28472849
},
28482850
"execution_count": null,
@@ -2873,11 +2875,13 @@
28732875
{
28742876
"data": {
28752877
"text/plain": [
2876-
"[Column('title', String(), table=<todo>),\n",
2877-
" Column('name', String(), table=<todo>),\n",
2878-
" Column('id', Integer(), table=<todo>, primary_key=True),\n",
2879-
" Column('done', Boolean(), table=<todo>),\n",
2880-
" Column('details', String(), table=<todo>)]"
2878+
"[Column('title', VARCHAR(), table=<todo>),\n",
2879+
" Column('name', VARCHAR(), table=<todo>),\n",
2880+
" Column('id', INTEGER(), table=<todo>, primary_key=True),\n",
2881+
" Column('done', BOOLEAN(), table=<todo>),\n",
2882+
" Column('details', VARCHAR(), table=<todo>),\n",
2883+
" Column('priority', INTEGER(), table=<todo>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object>, for_update=False)),\n",
2884+
" Column('due_date', TEXT(), table=<todo>)]"
28812885
]
28822886
},
28832887
"execution_count": null,
@@ -3018,7 +3022,7 @@
30183022
"name": "stdout",
30193023
"output_type": "stream",
30203024
"text": [
3021-
"SELECT todo.title, todo.name, todo.id, todo.done, todo.details \n",
3025+
"SELECT todo.title, todo.name, todo.id, todo.done, todo.details, todo.priority, todo.due_date \n",
30223026
"FROM todo \n",
30233027
"WHERE (todo.title LIKE :title_1 || '%')\n",
30243028
" LIMIT :param_1\n"

fastsql/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from fastcore.xtras import dataclass_src
1717
from itertools import starmap
1818

19-
import sqlalchemy as sa, subprocess
19+
import sqlparse, sqlalchemy as sa, subprocess
2020

2121
# %% ../00_core.ipynb 3
2222
class Default:
@@ -1036,7 +1036,8 @@ def migrate(self:Database, mdir):
10361036
cver = self.version
10371037
for v, p in _get_migrations(mdir)[self.version:]:
10381038
try:
1039-
if p.suffix == '.sql': self.execute(sa.text(p.read_text()))
1039+
if p.suffix == '.sql':
1040+
for stmt in filter(str.strip, sqlparse.split(p.read_text())): self.execute(sa.text(stmt))
10401041
elif p.suffix == '.py':
10411042
subprocess.run([sys.executable, p, self.conn_str], check=True)
10421043
self.version = v
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
ALTER TABLE todo ADD COLUMN priority INTEGER DEFAULT 0;
2+
ALTER TABLE todo ADD COLUMN due_date TEXT;

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ keywords = nbdev jupyter notebook python
2626
language = English
2727
status = 3
2828
user = answerdotai
29-
requirements = fastcore>=1.7.1 sqlalchemy
29+
requirements = fastcore>=1.7.1 sqlalchemy sqlparse
3030
readme_nb = index.ipynb
3131
allowed_metadata_keys =
3232
allowed_cell_metadata_keys =

0 commit comments

Comments
 (0)