Skip to content

Commit a590fb9

Browse files
committed
git push origin master
Merge branch 'pip-compatible'
2 parents 146b1ed + 7e109d8 commit a590fb9

File tree

6 files changed

+38
-151
lines changed

6 files changed

+38
-151
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Original work Copyright (c) Alex Gr�nholm
3+
Original work Copyright (c) Alex Gronholm
44
Modified work Copyright (c) Kamil Sindi
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
flask-sqlacodegen
22
=================
33

4-
Fork from <a href="https://pypi.python.org/pypi/sqlacodegen">sqlacodegen</a>. Based off of version 1.1.6.
4+
Fork of <a href="https://pypi.python.org/pypi/sqlacodegen">sqlacodegen</a> by Alex Gronholm. Based off of version 1.1.6.
55

66
What's different:
7-
* Support for Flask-SQLAlchemy syntax using `--flask` option. All this means:
8-
* SQLAlchemy class is instantiated (i.e. `db = SQLAlchemy()`).
9-
* Flask-SQLAlchemy columns are used (e.g. `db.Integer`).
10-
* Metadata is only implicit in tables
7+
* Use the command `flask-sqlacodgen` instead of `sqlacodegen`.
8+
* Support for Flask-SQLAlchemy syntax using `--flask` option:
9+
- SQLAlchemy class is instantiated (i.e. `db = SQLAlchemy()`).
10+
- Flask-SQLAlchemy columns are used (e.g. `db.Integer`).
11+
- Metadata is only implicit in tables.
1112
* Defaults to generating backrefs in relationships. `--nobackref` still included as option in case backrefs are not wanted.
12-
* Naming of backrefs is the class name is snake_case (as opposed to CamelCase) and is pluralized if it's Many-to-One or Many-to-Many using <a href="https://pypi.python.org/pypi/inflect">inflect</a>.
13+
* Naming of backrefs is class name in snake_case (as opposed to CamelCase) and is pluralized if it's Many-to-One or Many-to-Many using <a href="https://pypi.python.org/pypi/inflect">inflect</a>.
1314
* Generate explicit primary joins. I deal with pretty complicated tables that need explicit primary joins.
14-
* If column has a server_default set it to `FetchValue()` instead of trying to determine what that value is. Original code did not set the right server defaults in my set up.
15-
* `--ignorefk` ignores special name columns (e.g. id, inserted, updated) when generating association tables. Original code requires all columns to be foreign keys in order to generate association table. Example: `--ignorefk id,inserted,updated`.
15+
* If column has a server_default set it to `FetchValue()` instead of trying to determine what that value is. Original code did not set the right server defaults in my setup.
16+
* `--ignore-cols` ignores special columns when generating association tables. Original code requires all columns to be foreign keys in order to generate association table. Example: `--ignore-cols id,inserted,updated`.
17+
18+
## Install
19+
20+
With pip:
21+
```
22+
pip install flask-sqlacodegen
23+
```
24+
25+
Without pip:
26+
```
27+
git clone https://github.com/ksindi/flask-sqlacodegen.git
28+
cd flask-sqlacodegen/
29+
python setup.py install
30+
```

README.rst

Lines changed: 0 additions & 122 deletions
This file was deleted.

setup.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import os.path
32

43
from setuptools import setup, find_packages
54
from setuptools.command.test import test as TestCommand
@@ -23,17 +22,12 @@ def run_tests(self):
2322
if sys.version_info < (2, 7):
2423
extra_requirements = ('argparse',)
2524

26-
here = os.path.dirname(__file__)
27-
readme_path = os.path.join(here, 'README.rst')
28-
readme = open(readme_path).read()
29-
3025
setup(
31-
name='sqlacodegen',
32-
description='Automatic model code generator for SQLAlchemy',
33-
long_description=readme,
26+
name='flask-sqlacodegen',
27+
description='Automatic model code generator for SQLAlchemy with Flask support',
28+
long_description=open('README.md').read(),
3429
version=sqlacodegen.version,
3530
author='Kamil Sindi',
36-
author_email='kysindi@gmail.com',
3731
classifiers=[
3832
'Development Status :: 5 - Production/Stable',
3933
'Intended Audience :: Developers',
@@ -46,9 +40,11 @@ def run_tests(self):
4640
'Programming Language :: Python :: 2.7',
4741
'Programming Language :: Python :: 3',
4842
'Programming Language :: Python :: 3.2',
49-
'Programming Language :: Python :: 3.3'
43+
'Programming Language :: Python :: 3.3',
44+
'Programming Language :: Python :: 3.4',
45+
'Programming Language :: Python :: 3.5'
5046
],
51-
keywords='sqlalchemy',
47+
keywords=['sqlalchemy', 'flask-sqlacodegen', 'sqlacodegen'],
5248
license='MIT',
5349
packages=find_packages(exclude=['tests']),
5450
install_requires=(
@@ -60,7 +56,7 @@ def run_tests(self):
6056
zip_safe=False,
6157
entry_points={
6258
'console_scripts': [
63-
'sqlacodegen=sqlacodegen.main:main'
59+
'flask-sqlacodegen=sqlacodegen.main:main'
6460
]
6561
}
6662
)

sqlacodegen/codegen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class CodeGenerator(object):
527527

528528
def __init__(self, metadata, noindexes=False, noconstraints=False,
529529
nojoined=False, noinflect=False, nobackrefs=False,
530-
flask=False, fkcols=None):
530+
flask=False, ignore_cols=None):
531531
super(CodeGenerator, self).__init__()
532532

533533
if noinflect:
@@ -537,7 +537,7 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
537537
inflect_engine = inflect.engine()
538538

539539
# exclude these column names from consideration when generating association tables
540-
_special_columns = fkcols or []
540+
_ignore_columns = ignore_cols or []
541541

542542
self.flask = flask
543543
if not self.flask:
@@ -551,7 +551,7 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
551551
# Link tables have exactly two foreign key constraints and all columns are involved in them
552552
# except for special columns like id, inserted, and updated
553553
fk_constraints = [constr for constr in table.constraints if isinstance(constr, ForeignKeyConstraint)]
554-
if len(fk_constraints) == 2 and all(col.foreign_keys for col in table.columns if col.name not in _special_columns):
554+
if len(fk_constraints) == 2 and all(col.foreign_keys for col in table.columns if col.name not in _ignore_columns):
555555
association_tables.add(table.name)
556556
tablename = sorted(fk_constraints, key=_get_constraint_sort_key)[0].elements[0].column.table.name
557557
links[tablename].append(table)

sqlacodegen/main.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ def main():
3636
parser.add_argument('--outfile', help='file to write output to (default: stdout)')
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")
39-
parser.add_argument('--ignorefk', help="Don't check fk constraints on specified columns (comma-separated)")
40-
# parser.add_argument('--outfile', type=argparse.FileType('w'), default=sys.stdout,
41-
# help='file to write output to (default: stdout)')
39+
parser.add_argument('--ignore-cols', help="Don't check foreign key constraints on specified columns (comma-separated)")
4240
args = parser.parse_args()
4341

4442
if args.version:
@@ -53,10 +51,10 @@ def main():
5351
import_dialect_specificities(engine)
5452
metadata = MetaData(engine)
5553
tables = args.tables.split(',') if args.tables else None
56-
fkcols = args.ignorefk.split(',') if args.ignorefk else None
54+
ignore_cols = args.ignore_cols.split(',') if args.ignore_cols else None
5755
metadata.reflect(engine, args.schema, not args.noviews, tables)
5856
outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout
5957
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints,
6058
args.nojoined, args.noinflect, args.nobackrefs,
61-
args.flask, fkcols)
59+
args.flask, ignore_cols)
6260
generator.render(outfile)

0 commit comments

Comments
 (0)