Skip to content

Commit 4f9c22f

Browse files
authored
Merge pull request #36 from RohitShende/master
Added support for --notables and fixed bug for ENUM
2 parents d214aa0 + bc4cdba commit 4f9c22f

File tree

8 files changed

+85
-7
lines changed

8 files changed

+85
-7
lines changed

.github/workflows/pythonpublish.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflows will upload a Python Package using Twine when a release is created
2+
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3+
4+
name: Upload Python Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
deploy:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Set up Python
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: '3.x'
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install setuptools wheel twine
25+
- name: Build and publish
26+
env:
27+
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
28+
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
29+
run: |
30+
python setup.py sdist bdist_wheel
31+
twine upload dist/*

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ What's different:
1111
* 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.
1212
* `--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`.
1313
* Uses the command `flask-sqlacodegen` instead of `sqlacodegen`.
14+
* Added support for `--notables` to only generate model classes, even for association tables
1415

1516
## Install
1617

@@ -25,3 +26,11 @@ git clone https://github.com/ksindi/flask-sqlacodegen.git
2526
cd flask-sqlacodegen/
2627
python setup.py install
2728
```
29+
30+
For contributing:
31+
```sh
32+
git clone https://github.com/ksindi/flask-sqlacodegen.git
33+
python -m venv env
34+
pip install -r requirements.txt
35+
python -m sqlacodegen.main --flask --outfile models.py mysql+pymysql://<username>:<password>@<database-ip>:<port>/<database-name> [--tables <tablenames>] [--notables]
36+
```

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ What's different:
2424
order to generate association table. Example:
2525
``--ignore-cols id,inserted,updated``.
2626
- Uses the command ``flask-sqlacodgen`` instead of ``sqlacodegen``.
27+
- Added support for ``--notables`` to only generate model classes, even for association tables
2728

2829
Install
2930
-------

requirements.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apipkg==1.5
2+
atomicwrites==1.4.0
3+
attrs==19.3.0
4+
colorama==0.4.3
5+
execnet==1.7.1
6+
importlib-metadata==1.6.0
7+
inflect==4.1.0
8+
more-itertools==8.2.0
9+
packaging==20.3
10+
sqlacodegen==1.1.6
11+
pep8==1.7.1
12+
pluggy==0.13.1
13+
py==1.8.1
14+
PyMySQL==0.9.3
15+
pyparsing==2.4.7
16+
pytest==5.4.2
17+
pytest-cache==1.0
18+
pytest-pep8==1.0.6
19+
six==1.14.0
20+
SQLAlchemy==1.3.17
21+
wcwidth==0.1.9
22+
zipp==3.1.0

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def run_tests(self):
4242
'Programming Language :: Python :: 3.2',
4343
'Programming Language :: Python :: 3.3',
4444
'Programming Language :: Python :: 3.4',
45-
'Programming Language :: Python :: 3.5'
45+
'Programming Language :: Python :: 3.5',
46+
'Programming Language :: Python :: 3.6',
47+
'Programming Language :: Python :: 3.7'
4648
],
4749
keywords=['sqlalchemy', 'sqlacodegen', 'flask'],
4850
license='MIT',

sqlacodegen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = __version__ = '1.1.7'
1+
version = __version__ = '1.1.8'

sqlacodegen/codegen.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ def _render_column_type(coltype):
127127
args.append(repr(value))
128128

129129
text = _flask_prepend + coltype.__class__.__name__
130+
131+
# In case of ENUM from sqlalchemy.dialects, the flask used db.Enum
132+
if text == "db.ENUM":
133+
text = "db.Enum"
134+
130135
if args:
131136
text += '({0})'.format(', '.join(args))
132137

@@ -529,7 +534,7 @@ class CodeGenerator(object):
529534

530535
def __init__(self, metadata, noindexes=False, noconstraints=False,
531536
nojoined=False, noinflect=False, nobackrefs=False,
532-
flask=False, ignore_cols=None, noclasses=False, nocomments=False):
537+
flask=False, ignore_cols=None, noclasses=False, nocomments=False, notables=False):
533538
super(CodeGenerator, self).__init__()
534539

535540
if noinflect:
@@ -603,15 +608,22 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
603608
table.c[colname].type = Enum(*options, native_enum=False)
604609
continue
605610

606-
# Only form model classes for tables that have a primary key and are not association tables
607-
if not table.primary_key or table.name in association_tables or noclasses:
611+
# Only generate classes when notables is set to True
612+
if notables:
613+
model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
614+
classes[model.name] = model
615+
elif not table.primary_key or table.name in association_tables or noclasses:
616+
# Only form model classes for tables that have a primary key and are not association tables
608617
model = ModelTable(table)
609618
elif not noclasses:
610619
model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
611620
classes[model.name] = model
612621

613622
self.models.append(model)
614-
model.add_imports(self.collector)
623+
624+
# collect imports for models only if flask is not specified
625+
if not self.flask:
626+
model.add_imports(self.collector)
615627

616628
# Nest inherited classes in their superclasses to ensure proper ordering
617629
for model in classes.values():

sqlacodegen/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def main():
3333
parser.add_argument('--nojoined', action='store_true', help="don't autodetect joined table inheritance")
3434
parser.add_argument('--noinflect', action='store_true', help="don't try to convert tables names to singular form")
3535
parser.add_argument('--noclasses', action='store_true', help="don't generate classes, only tables")
36+
parser.add_argument('--notables', action='store_true', help="don't generate tables, only classes")
3637
parser.add_argument('--outfile', help='file to write output to (default: stdout)')
3738
parser.add_argument('--nobackrefs', action='store_true', help="don't include backrefs")
3839
parser.add_argument('--flask', action='store_true', help="use Flask-SQLAlchemy columns")
@@ -57,7 +58,7 @@ def main():
5758
outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout
5859
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints,
5960
args.nojoined, args.noinflect, args.nobackrefs,
60-
args.flask, ignore_cols, args.noclasses, args.nocomments)
61+
args.flask, ignore_cols, args.noclasses, args.nocomments, args.notables)
6162
generator.render(outfile)
6263

6364

0 commit comments

Comments
 (0)