Skip to content

Commit ff08749

Browse files
authored
Merge pull request #33 from danerlt/master
[feat] add table column comment
2 parents 90bc61a + 529c7b2 commit ff08749

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

sqlacodegen/codegen.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ def _render_column(column, show_name):
160160
if column.server_default:
161161
server_default = 'server_default=' + _flask_prepend + 'FetchedValue()'
162162

163+
comment = getattr(column, 'comment', None)
163164
return _flask_prepend + 'Column({0})'.format(', '.join(
164165
([repr(column.name)] if show_name else []) +
165166
([_render_column_type(column.type)] if render_coltype else []) +
166167
[_render_constraint(x) for x in dedicated_fks] +
167168
[repr(x) for x in column.constraints] +
168169
['{0}={1}'.format(k, repr(getattr(column, k))) for k in kwarg] +
169-
([server_default] if column.server_default else [])
170+
([server_default] if column.server_default else []) +
171+
(['info={!r}'.format(comment)] if comment else [])
170172
))
171173

172174

@@ -527,7 +529,7 @@ class CodeGenerator(object):
527529

528530
def __init__(self, metadata, noindexes=False, noconstraints=False,
529531
nojoined=False, noinflect=False, nobackrefs=False,
530-
flask=False, ignore_cols=None, noclasses=False):
532+
flask=False, ignore_cols=None, noclasses=False, nocomments=False):
531533
super(CodeGenerator, self).__init__()
532534

533535
if noinflect:
@@ -544,6 +546,8 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
544546
global _flask_prepend
545547
_flask_prepend = ''
546548

549+
self.nocomments = nocomments
550+
547551
# Pick association tables from the metadata into their own set, don't process them normally
548552
links = defaultdict(lambda: [])
549553
association_tables = set()
@@ -654,7 +658,7 @@ def render(self, outfile=sys.stdout):
654658
# Render the model tables and classes
655659
for model in self.models:
656660
print('\n\n', file=outfile)
657-
print(model.render().rstrip('\n').encode('utf-8'), file=outfile)
661+
print(model.render().rstrip('\n'), file=outfile)
658662

659663
if self.footer:
660664
print(self.footer, file=outfile)

sqlacodegen/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def main():
3737
parser.add_argument('--nobackrefs', action='store_true', help="don't include backrefs")
3838
parser.add_argument('--flask', action='store_true', help="use Flask-SQLAlchemy columns")
3939
parser.add_argument('--ignore-cols', help="Don't check foreign key constraints on specified columns (comma-separated)")
40+
parser.add_argument('--nocomments', action='store_true', help="don't render column comments")
4041
args = parser.parse_args()
4142

4243
if args.version:
@@ -56,7 +57,7 @@ def main():
5657
outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout
5758
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints,
5859
args.nojoined, args.noinflect, args.nobackrefs,
59-
args.flask, ignore_cols, args.noclasses)
60+
args.flask, ignore_cols, args.noclasses, args.nocomments)
6061
generator.render(outfile)
6162

6263

0 commit comments

Comments
 (0)