Skip to content

Commit b3676be

Browse files
committed
added run_sync
1 parent 6344aa8 commit b3676be

File tree

11 files changed

+75
-50
lines changed

11 files changed

+75
-50
lines changed

aragorm/migration.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import click
77

88
from aragorm.migrations.template import TEMPLATE
9+
from aragorm.migrations.table import Migration
910

1011

1112
MIGRATIONS_FOLDER = os.path.join(os.getcwd(), 'migrations')
@@ -43,14 +44,25 @@ def new():
4344
_create_new_migration()
4445

4546

47+
###############################################################################
48+
49+
def _create_migration_table() -> bool:
50+
if not Migration.table_exists().run():
51+
Migration.create().run()
52+
return True
53+
return False
54+
55+
4656
@click.command()
4757
def run():
4858
"""
4959
Runs any migrations which haven't been run yet, or up to a specific
5060
migration.
5161
"""
5262
print('Running migrations ...')
63+
_create_migration_table()
5364

65+
###############################################################################
5466

5567
cli.add_command(new)
5668
cli.add_command(run)

aragorm/query.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import asyncpg
1+
import asyncio
22
import dataclasses
33

4-
from .columns import And, Where, Column
4+
import asyncpg
5+
6+
from .columns import And
57
from .types import Combinable
68

79

@@ -48,10 +50,9 @@ def __init__(self, table: 'Table', base: str = '') -> None:
4850
self._limit: Limit = None
4951
self._order_by: OrderBy = None
5052

51-
# TODO - I want sync and async versions of this
52-
async def execute(self, as_dict=True, credentials=None):
53+
async def run(self, as_dict=True, credentials=None):
5354
"""
54-
Now ... just execute it from within here for now ...
55+
Should use an engine.
5556
"""
5657
if not credentials:
5758
credentials = getattr(self.table.Meta, 'db', None)
@@ -64,6 +65,18 @@ async def execute(self, as_dict=True, credentials=None):
6465
raw = [dict(i.items()) for i in results]
6566
return self.response_handler(raw)
6667

68+
def run_sync(self, *args, **kwargs):
69+
"""
70+
A convenience method for running the coroutine synchronously.
71+
72+
Might make it more sophisticated in the future, so not creating /
73+
tearing down connections, but instead running it in a separate
74+
process, and dispatching coroutines to it.
75+
"""
76+
return asyncio.run(
77+
self.sync(*args, **kwargs)
78+
)
79+
6780
def response_handler(self, response):
6881
"""
6982
Subclasses can override this to modify the raw response returned by

aragorm/table.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Database(object):
1717

18-
async def execute(*queries):
18+
async def run(*queries):
1919
"""
2020
Use asyncio.gather here ...
2121
"""
@@ -113,7 +113,7 @@ def update(cls, **columns) -> Update:
113113
"""
114114
await Pokemon.update(name='raichu').where(
115115
Pokemon.name='pikachu'
116-
).execute()
116+
).run()
117117
"""
118118
columns_str = ','.join([
119119
f'{column} = {getattr(cls, column).format_value(value)}' for (
@@ -127,7 +127,7 @@ def update(cls, **columns) -> Update:
127127
@classmethod
128128
def delete(cls, **columns) -> Delete:
129129
"""
130-
await Pokemon.delete().where(Pokemon.name='weedle').execute()
130+
await Pokemon.delete().where(Pokemon.name='weedle').run()
131131
132132
DELETE FROM pokemon where name = 'weedle'
133133
"""
@@ -139,7 +139,7 @@ def delete(cls, **columns) -> Delete:
139139
@classmethod
140140
def create(cls) -> Create:
141141
"""
142-
await Pokemon.create().execute()
142+
await Pokemon.create().run()
143143
"""
144144
return Create(
145145
table=cls,
@@ -149,7 +149,7 @@ def create(cls) -> Create:
149149
@classmethod
150150
def drop(cls) -> Drop:
151151
"""
152-
await Pokemon.drop().execute()
152+
await Pokemon.drop().run()
153153
"""
154154
return Drop(
155155
table=cls,

tests/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
class DBTestCase(TestCase):
1010

11-
def execute(self, query):
12-
async def _execute():
11+
def run_sync(self, query):
12+
async def _run():
1313
connection = await asyncpg.connect(**Pokemon.Meta.db)
1414
await connection.execute(query)
1515
await connection.close()
1616

17-
asyncio.run(_execute())
17+
asyncio.run(_run())
1818

1919
def create_table(self):
20-
self.execute('''
20+
self.run_sync('''
2121
CREATE TABLE pokemon (
2222
name VARCHAR(50),
2323
trainer VARCHAR(20),
@@ -26,7 +26,7 @@ def create_table(self):
2626
)
2727

2828
def insert_row(self):
29-
self.execute('''
29+
self.run_sync('''
3030
INSERT INTO pokemon (
3131
name,
3232
trainer,
@@ -39,7 +39,7 @@ def insert_row(self):
3939
)
4040

4141
def insert_rows(self):
42-
self.execute('''
42+
self.run_sync('''
4343
INSERT INTO pokemon (
4444
name,
4545
trainer,
@@ -60,7 +60,7 @@ def insert_rows(self):
6060
)
6161

6262
def drop_table(self):
63-
self.execute('DROP TABLE pokemon;')
63+
self.run_sync('DROP TABLE pokemon;')
6464

6565
def setUp(self):
6666
self.create_table()

tests/table/test_create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def setUp(self):
1414

1515
def test_create_table(self):
1616
async def create_table():
17-
return await Pokemon.create().execute()
17+
return await Pokemon.create().run()
1818

1919
async def count_rows():
2020
return await Pokemon.select(
2121
'name', 'trainer', 'power'
22-
).count().execute()
22+
).count().run()
2323

2424
asyncio.run(create_table())
2525

tests/table/test_delete.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ def test_delete(self):
1212
async def delete_pokemon():
1313
return await Pokemon.delete().where(
1414
Pokemon.name == 'weedle'
15-
).execute()
15+
).run()
1616

1717
async def check_pokemon():
1818
return await Pokemon.select().where(
1919
Pokemon.name == 'weedle'
20-
).count().execute()
20+
).count().run()
2121

2222
asyncio.run(delete_pokemon())
2323
response = asyncio.run(check_pokemon())

tests/table/test_select.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_query_all_columns(self):
1010
self.insert_row()
1111

1212
async def get_pokemon():
13-
return await Pokemon.select().execute()
13+
return await Pokemon.select().run()
1414

1515
response = asyncio.run(get_pokemon())
1616
print(f'response = {response}')
@@ -24,7 +24,7 @@ def test_query_some_columns(self):
2424
self.insert_row()
2525

2626
async def get_pokemon():
27-
return await Pokemon.select('name').execute()
27+
return await Pokemon.select('name').run()
2828

2929
response = asyncio.run(get_pokemon())
3030
print(f'response = {response}')
@@ -42,7 +42,7 @@ async def get_pokemon():
4242
'name'
4343
).where(
4444
Pokemon.name.like('%chu')
45-
).execute()
45+
).run()
4646

4747
response = asyncio.run(get_pokemon())
4848
print(f'response = {response}')
@@ -60,7 +60,7 @@ async def get_pokemon():
6060
'name'
6161
).where(
6262
Pokemon.power > 1000
63-
).execute()
63+
).run()
6464

6565
response = asyncio.run(get_pokemon())
6666
print(f'response = {response}')
@@ -78,7 +78,7 @@ async def get_pokemon():
7878
'name'
7979
).where(
8080
Pokemon.power >= 1000
81-
).execute()
81+
).run()
8282

8383
response = asyncio.run(get_pokemon())
8484
print(f'response = {response}')
@@ -96,7 +96,7 @@ async def get_pokemon():
9696
'name'
9797
).where(
9898
Pokemon.power < 1000
99-
).execute()
99+
).run()
100100

101101
response = asyncio.run(get_pokemon())
102102
print(f'response = {response}')
@@ -114,7 +114,7 @@ async def get_pokemon():
114114
'name'
115115
).where(
116116
Pokemon.power <= 1000
117-
).execute()
117+
).run()
118118

119119
response = asyncio.run(get_pokemon())
120120
print(f'response = {response}')
@@ -132,7 +132,7 @@ async def get_pokemon():
132132
'name'
133133
).where(
134134
(Pokemon.power <= 1000) & (Pokemon.name.like('%chu'))
135-
).execute()
135+
).run()
136136

137137
response = asyncio.run(get_pokemon())
138138
print(f'response = {response}')
@@ -150,7 +150,7 @@ async def get_pokemon():
150150
'name'
151151
).where(
152152
(Pokemon.name == 'raichu') | (Pokemon.name == 'weedle')
153-
).execute()
153+
).run()
154154

155155
response = asyncio.run(get_pokemon())
156156
print(f'response = {response}')
@@ -175,7 +175,7 @@ def test_multiple_where(self):
175175
)
176176

177177
async def get_pokemon():
178-
return await query.execute()
178+
return await query.run()
179179

180180
response = asyncio.run(get_pokemon())
181181
print(f'response = {response}')
@@ -200,7 +200,7 @@ async def get_pokemon():
200200
).where(
201201
((Pokemon.power == 2000) & (Pokemon.trainer == 'sally')) |
202202
((Pokemon.power == 10) & (Pokemon.trainer == 'gordon'))
203-
).execute()
203+
).run()
204204

205205
response = asyncio.run(get_pokemon())
206206
print(f'response = {response}')
@@ -218,7 +218,7 @@ async def get_pokemon():
218218
'name'
219219
).limit(
220220
1
221-
).execute()
221+
).run()
222222

223223
response = asyncio.run(get_pokemon())
224224
print(f'response = {response}')
@@ -236,7 +236,7 @@ async def get_pokemon():
236236
'name'
237237
).order_by(
238238
'name'
239-
).limit(1).execute()
239+
).limit(1).run()
240240

241241
response = asyncio.run(get_pokemon())
242242
print(f'response = {response}')
@@ -254,7 +254,7 @@ async def get_pokemon():
254254
'name'
255255
).order_by(
256256
'-name'
257-
).limit(1).execute()
257+
).limit(1).run()
258258

259259
response = asyncio.run(get_pokemon())
260260
print(f'response = {response}')
@@ -270,7 +270,7 @@ def test_count(self):
270270
async def get_pokemon():
271271
return await Pokemon.select().where(
272272
Pokemon.name == 'pikachu'
273-
).count().execute()
273+
).count().run()
274274

275275
response = asyncio.run(get_pokemon())
276276
print(f'response = {response}')

tests/table/test_table_exists.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
class TestTableExists(TestCase):
88

99
def setUp(self):
10-
asyncio.run(Pokemon.create().execute())
10+
asyncio.run(Pokemon.create().run())
1111

1212
def test_table_exists(self):
13-
response = asyncio.run(Pokemon.table_exists().execute())
13+
response = asyncio.run(Pokemon.table_exists().run())
1414
self.assertTrue(response is True)
1515

1616
def tearDown(self):
17-
asyncio.run(Pokemon.drop().execute())
17+
asyncio.run(Pokemon.drop().run())

tests/table/test_update.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ async def update_pokemon():
1414
name='kakuna'
1515
).where(
1616
Pokemon.name == 'weedle'
17-
).execute()
17+
).run()
1818

1919
async def check_pokemon():
2020
return await Pokemon.select(
2121
'name'
2222
).where(
2323
Pokemon.name == 'kakuna'
24-
).execute()
24+
).run()
2525

2626
asyncio.run(update_pokemon())
2727
response = asyncio.run(check_pokemon())

0 commit comments

Comments
 (0)