Skip to content

Commit 073aaea

Browse files
author
percious
committed
now we handle multiple schemas.
1 parent a7766fb commit 073aaea

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

sqlautocode/config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def create_parser():
3434
parser.add_option(
3535
"-s", "--schema",
3636
help="Optional, reflect a non-default schema",
37-
action="store", dest="schema")
37+
action="callback", callback=_prep_schema, type="string", dest="schema")
3838

3939
parser.add_option(
4040
"-t", "--tables",
@@ -104,6 +104,16 @@ def _prep_tables(option, opt_str, value, parser):
104104
for x in value.split(',')
105105
if x.strip() != '']
106106

107+
def _prep_schema(option, opt_str, value, parser):
108+
#handle multiple schemas on the command line
109+
value = [x.strip()
110+
for x in value.split(',')
111+
if x.strip() != '']
112+
if len(value) == 1:
113+
parser.values.schema = value[0]
114+
return
115+
parser.values.schema = value
116+
107117
def _version_check(parser):
108118
try:
109119
import sqlalchemy

sqlautocode/declarative.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ def __init__(self, config):
8686
if isinstance(schema, (list, tuple)):
8787
self.schemas = schema
8888
else:
89-
self.schems = (schema, )
89+
self.schemas = (schema, )
9090
for schema in self.schemas:
9191
logging.info('Reflecting database... schema:%s'%schema)
9292
self._metadata.reflect(schema=schema)
9393
else:
9494
logging.info('Reflecting database...')
95-
self._metadata.reflect
95+
self._metadata.reflect()
96+
9697

9798
self.DeclarativeBase = declarative_base(metadata=self._metadata)
9899

@@ -127,7 +128,7 @@ def __repr__(self):
127128

128129
@property
129130
def tables(self):
130-
return sorted(self._metadata.tables.keys())
131+
return self._metadata.tables.keys()
131132

132133
@property
133134
def models(self):

sqlautocode/main.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@
66
def main():
77
config.configure()
88

9+
options = config.options
10+
if options.declarative:
11+
config.interactive = None
12+
if options.interactive:
13+
config.interactive = True
14+
config.schema = None
15+
if options.schema:
16+
config.schema = options.schema
17+
config.example=False
18+
if options.example:
19+
config.example=True
20+
factory = ModelFactory(config)
21+
emit(repr(factory))
22+
config.out.close()
23+
config.out = sys.stdout
24+
print >>config.err, "Output written to %s" % options.output
25+
return
26+
927
import formatter
1028
formatter.monkey_patch_sa()
1129

@@ -46,22 +64,6 @@ def main():
4664
else:
4765
dialect = 'from sqlalchemy.databases.%s import *\n' % db.name
4866

49-
if options.declarative:
50-
config.interactive = None
51-
if options.interactive:
52-
config.interactive = True
53-
config.schema = None
54-
if options.schema:
55-
config.schema = options.schema
56-
config.example=False
57-
if options.example:
58-
config.example=True
59-
factory = ModelFactory(config)
60-
emit(repr(factory))
61-
config.out.close()
62-
config.out = sys.stdout
63-
print >>config.err, "Output written to %s" % options.output
64-
return
6567

6668
header = options.z3c and constants.HEADER_Z3C or constants.HEADER
6769
emit(header % {'dialect': dialect, 'encoding': options.encoding})

sqlautocode/tests/data/devdata.db

16 KB
Binary file not shown.

sqlautocode/tests/test_declarative.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
from nose.tools import eq_
33
from sqlautocode.declarative import ModelFactory
44
from sqlalchemy.orm import class_mapper
5-
#testdb = 'sqlite:///'+os.path.abspath(os.path.dirname(__file__))+'/data/devdata.db'
6-
testdb = 'postgres://postgres@localhost/TestSamples'
5+
testdb = 'sqlite:///'+os.path.abspath(os.path.dirname(__file__))+'/data/devdata.db'
6+
#testdb = 'postgres://postgres@localhost/TestSamples'
77

88
print testdb
99
class DummyConfig:
1010
engine = testdb
1111
example = True
1212
schema = None
1313
interactive = None
14-
schema = ['pdil_samples', 'pdil_tools']
14+
# schema = ['pdil_samples', 'pdil_tools']
1515

1616

1717
class TestModelFactory:
@@ -53,7 +53,7 @@ def test_get_foreign_keys(self):
5353
eq_(columns, ['town_id'])
5454

5555

56-
def test_model__repr__(self):
56+
def test_model___repr__(self):
5757
models = sorted(self._setup_all_models())
5858
for model in models:
5959
if model.__name__=='TgUser':

0 commit comments

Comments
 (0)