Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"from fastcore.xtras import dataclass_src\n",
"from itertools import starmap\n",
"\n",
"import sqlalchemy as sa, subprocess"
"import sqlparse, sqlalchemy as sa, subprocess"
]
},
{
Expand Down Expand Up @@ -2675,7 +2675,7 @@
{
"data": {
"text/plain": [
"[(1, Path('migrations/1-add_priority_to_todo.sql'))]"
"[(1, Path('migrations/1-add_priority_and_due_date_to_todo.sql'))]"
]
},
"execution_count": null,
Expand All @@ -2702,7 +2702,8 @@
" cver = self.version\n",
" for v, p in _get_migrations(mdir)[self.version:]:\n",
" try:\n",
" if p.suffix == '.sql': self.execute(sa.text(p.read_text()))\n",
" if p.suffix == '.sql':\n",
" for stmt in filter(str.strip, sqlparse.split(p.read_text())): self.execute(sa.text(stmt))\n",
" elif p.suffix == '.py':\n",
" subprocess.run([sys.executable, p, self.conn_str], check=True)\n",
" self.version = v\n",
Expand Down Expand Up @@ -2739,14 +2740,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Applied migration 1: 1-add_priority_to_todo.sql\n"
"Applied migration 1: 1-add_priority_and_due_date_to_todo.sql\n"
]
}
],
"source": [
"db.migrate(mdir)\n",
"test_eq(db.version, 1)\n",
"assert 'priority' in db.t.todo.c"
"assert 'priority' in db.t.todo.c\n",
"assert 'due_date' in db.t.todo.c"
]
},
{
Expand All @@ -2758,8 +2760,8 @@
{
"data": {
"text/plain": [
"[Todo(title='get it done', name='rlt', id=2, done=False, details='', priority=0),\n",
" Todo(title='get it done', name='rlt', id=4, done=False, details='', priority=0)]"
"[Todo(title='get it done', name='rlt', id=2, done=False, details='', priority=0, due_date=None),\n",
" Todo(title='get it done', name='rlt', id=4, done=False, details='', priority=0, due_date=None)]"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2842,7 +2844,7 @@
{
"data": {
"text/plain": [
"'user todo pending_todos'"
"'_meta todo user'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2873,11 +2875,13 @@
{
"data": {
"text/plain": [
"[Column('title', String(), table=<todo>),\n",
" Column('name', String(), table=<todo>),\n",
" Column('id', Integer(), table=<todo>, primary_key=True),\n",
" Column('done', Boolean(), table=<todo>),\n",
" Column('details', String(), table=<todo>)]"
"[Column('title', VARCHAR(), table=<todo>),\n",
" Column('name', VARCHAR(), table=<todo>),\n",
" Column('id', INTEGER(), table=<todo>, primary_key=True),\n",
" Column('done', BOOLEAN(), table=<todo>),\n",
" Column('details', VARCHAR(), table=<todo>),\n",
" Column('priority', INTEGER(), table=<todo>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object>, for_update=False)),\n",
" Column('due_date', TEXT(), table=<todo>)]"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3018,7 +3022,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"SELECT todo.title, todo.name, todo.id, todo.done, todo.details \n",
"SELECT todo.title, todo.name, todo.id, todo.done, todo.details, todo.priority, todo.due_date \n",
"FROM todo \n",
"WHERE (todo.title LIKE :title_1 || '%')\n",
" LIMIT :param_1\n"
Expand Down
5 changes: 3 additions & 2 deletions fastsql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from fastcore.xtras import dataclass_src
from itertools import starmap

import sqlalchemy as sa, subprocess
import sqlparse, sqlalchemy as sa, subprocess

# %% ../00_core.ipynb 3
class Default:
Expand Down Expand Up @@ -1036,7 +1036,8 @@ def migrate(self:Database, mdir):
cver = self.version
for v, p in _get_migrations(mdir)[self.version:]:
try:
if p.suffix == '.sql': self.execute(sa.text(p.read_text()))
if p.suffix == '.sql':
for stmt in filter(str.strip, sqlparse.split(p.read_text())): self.execute(sa.text(stmt))
elif p.suffix == '.py':
subprocess.run([sys.executable, p, self.conn_str], check=True)
self.version = v
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ALTER TABLE todo ADD COLUMN priority INTEGER DEFAULT 0;
ALTER TABLE todo ADD COLUMN due_date TEXT;
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ keywords = nbdev jupyter notebook python
language = English
status = 3
user = answerdotai
requirements = fastcore>=1.7.1 sqlalchemy
requirements = fastcore>=1.7.1 sqlalchemy sqlparse
readme_nb = index.ipynb
allowed_metadata_keys =
allowed_cell_metadata_keys =
Expand Down