Skip to content

Commit e4ae83f

Browse files
committed
Optional create_objects keyword argument for install and initialize functions
1 parent b5022a5 commit e4ae83f

6 files changed

Lines changed: 50 additions & 10 deletions

File tree

tests/test_cli.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from bddcli import Given, Application as CLIApplication, status, stderr, \
22
when, stdout
33
import easycli
4+
from pony.orm import PrimaryKey, Required
45
from yhttp import Application
56
from yhttp.ext.pony import install
67

@@ -20,16 +21,21 @@ def __call__(self, args):
2021
install(app, cliarguments=[Bar])
2122

2223

24+
class Foo(app.db.Entity):
25+
id = PrimaryKey(int, auto=True)
26+
title = Required(str)
27+
28+
2329
def test_applicationcli():
2430
cliapp = CLIApplication('example', 'tests.test_cli:app.climain')
25-
with Given(cliapp, 'db --help'):
31+
with Given(cliapp, 'db'):
2632
assert stderr == ''
2733
assert status == 0
2834

2935
when('db drop')
3036
when('db create')
31-
assert status == 0
3237
assert stderr == ''
38+
assert status == 0
3339

3440
when('db drop')
3541
assert status == 0
@@ -40,3 +46,12 @@ def test_applicationcli():
4046
assert status == 0
4147
assert stderr == ''
4248
assert stdout == 'bar\n'
49+
50+
when('db c')
51+
assert status == 0
52+
assert stderr == ''
53+
assert stdout == '''Following objects has been created successfully:
54+
S foo_id_seq
55+
r foo
56+
i foo_pkey
57+
'''

tests/test_extension.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ def test_extension(app, Given, freshdb):
1111
db:
1212
url: {freshdb}
1313
''')
14-
dbsession = install(app)
14+
15+
dbsession = install(app, create_objects=True)
1516

1617
class Foo(app.db.Entity):
1718
id = PrimaryKey(int, auto=True)
1819
title = Required(str)
1920

21+
app.ready()
22+
2023
@dbsession
2124
def mockup():
2225
Foo(title='foo 1')
2326
Foo(title='foo 2')
2427

25-
app.ready()
2628
mockup()
2729

2830
@app.route()

yhttp/ext/pony/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
from .orm import initialize, deinitialize
66

77

8-
__version__ = '2.3.1'
8+
__version__ = '2.4.0'

yhttp/ext/pony/cli.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import getpass
33

44
from easycli import SubCommand, Argument
5+
from pony.orm import db_session
56

6-
from . import dbmanager, uri
7+
from . import dbmanager, uri, orm
78

89

910
getdbpass = functools.partial(getpass.getpass, 'Enter db password: ')
@@ -47,6 +48,25 @@ def __call__(self, args):
4748
self.getdbmanager(args) \
4849
.create(info['database'], owner=info.get('user'))
4950

51+
self.create_objects(args)
52+
self.report_objects(args)
53+
54+
def create_objects(self, args):
55+
app = args.application
56+
orm.initialize(app.db, app.settings.db.url, create_objects=True)
57+
58+
@db_session
59+
def report_objects(self, args):
60+
app = args.application
61+
result = app.db.execute('''
62+
SELECT relname, relkind
63+
FROM pg_class
64+
WHERE relname !~ '^(pg|sql)_' AND relkind != 'v';
65+
''')
66+
print('Following objects has been created successfully:')
67+
for name, kind in result.fetchall():
68+
print(kind, name)
69+
5070

5171
class DropDatabase(DatabaseAdministrativeCommand):
5272
__command__ = 'drop'

yhttp/ext/pony/install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def wrapper(*a, **kw):
3131
return outter
3232

3333

34-
def install(app, db=None, cliarguments=None):
34+
def install(app, db=None, cliarguments=None, create_objects=False):
3535
app.cliarguments.append(DatabaseCLI)
3636
if cliarguments:
3737
DatabaseCLI.__arguments__.extend(cliarguments)
@@ -49,7 +49,7 @@ def ready(app):
4949
'postgres://:@/dbname'
5050
)
5151

52-
orm.initialize(db, app.settings.db.url)
52+
orm.initialize(db, app.settings.db.url, create_objects=create_objects)
5353

5454
@app.when
5555
def shutdown(app):

yhttp/ext/pony/orm.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from . import uri
22

33

4-
def initialize(db, url):
4+
def initialize(db, url, create_objects=False):
55
url = uri.parse(url)
66
db.bind(**url)
7-
db.generate_mapping(create_tables=True)
7+
db.generate_mapping(
8+
check_tables=create_objects,
9+
create_tables=create_objects
10+
)
811

912

1013
def deinitialize(db):

0 commit comments

Comments
 (0)