Skip to content

Commit

Permalink
Merge pull request #36 from RohitShende/master
Browse files Browse the repository at this point in the history
Added support for --notables and fixed bug for ENUM
  • Loading branch information
ksindi authored Sep 12, 2020
2 parents d214aa0 + bc4cdba commit 4f9c22f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 7 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ What's different:
* 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.
* `--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`.
* Uses the command `flask-sqlacodegen` instead of `sqlacodegen`.
* Added support for `--notables` to only generate model classes, even for association tables

## Install

Expand All @@ -25,3 +26,11 @@ git clone https://github.com/ksindi/flask-sqlacodegen.git
cd flask-sqlacodegen/
python setup.py install
```

For contributing:
```sh
git clone https://github.com/ksindi/flask-sqlacodegen.git
python -m venv env
pip install -r requirements.txt
python -m sqlacodegen.main --flask --outfile models.py mysql+pymysql://<username>:<password>@<database-ip>:<port>/<database-name> [--tables <tablenames>] [--notables]
```
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ What's different:
order to generate association table. Example:
``--ignore-cols id,inserted,updated``.
- Uses the command ``flask-sqlacodgen`` instead of ``sqlacodegen``.
- Added support for ``--notables`` to only generate model classes, even for association tables

Install
-------
Expand Down
22 changes: 22 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apipkg==1.5
atomicwrites==1.4.0
attrs==19.3.0
colorama==0.4.3
execnet==1.7.1
importlib-metadata==1.6.0
inflect==4.1.0
more-itertools==8.2.0
packaging==20.3
sqlacodegen==1.1.6
pep8==1.7.1
pluggy==0.13.1
py==1.8.1
PyMySQL==0.9.3
pyparsing==2.4.7
pytest==5.4.2
pytest-cache==1.0
pytest-pep8==1.0.6
six==1.14.0
SQLAlchemy==1.3.17
wcwidth==0.1.9
zipp==3.1.0
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def run_tests(self):
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5'
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
],
keywords=['sqlalchemy', 'sqlacodegen', 'flask'],
license='MIT',
Expand Down
2 changes: 1 addition & 1 deletion sqlacodegen/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = __version__ = '1.1.7'
version = __version__ = '1.1.8'
20 changes: 16 additions & 4 deletions sqlacodegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def _render_column_type(coltype):
args.append(repr(value))

text = _flask_prepend + coltype.__class__.__name__

# In case of ENUM from sqlalchemy.dialects, the flask used db.Enum
if text == "db.ENUM":
text = "db.Enum"

if args:
text += '({0})'.format(', '.join(args))

Expand Down Expand Up @@ -529,7 +534,7 @@ class CodeGenerator(object):

def __init__(self, metadata, noindexes=False, noconstraints=False,
nojoined=False, noinflect=False, nobackrefs=False,
flask=False, ignore_cols=None, noclasses=False, nocomments=False):
flask=False, ignore_cols=None, noclasses=False, nocomments=False, notables=False):
super(CodeGenerator, self).__init__()

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

# Only form model classes for tables that have a primary key and are not association tables
if not table.primary_key or table.name in association_tables or noclasses:
# Only generate classes when notables is set to True
if notables:
model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
classes[model.name] = model
elif not table.primary_key or table.name in association_tables or noclasses:
# Only form model classes for tables that have a primary key and are not association tables
model = ModelTable(table)
elif not noclasses:
model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
classes[model.name] = model

self.models.append(model)
model.add_imports(self.collector)

# collect imports for models only if flask is not specified
if not self.flask:
model.add_imports(self.collector)

# Nest inherited classes in their superclasses to ensure proper ordering
for model in classes.values():
Expand Down
3 changes: 2 additions & 1 deletion sqlacodegen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def main():
parser.add_argument('--nojoined', action='store_true', help="don't autodetect joined table inheritance")
parser.add_argument('--noinflect', action='store_true', help="don't try to convert tables names to singular form")
parser.add_argument('--noclasses', action='store_true', help="don't generate classes, only tables")
parser.add_argument('--notables', action='store_true', help="don't generate tables, only classes")
parser.add_argument('--outfile', help='file to write output to (default: stdout)')
parser.add_argument('--nobackrefs', action='store_true', help="don't include backrefs")
parser.add_argument('--flask', action='store_true', help="use Flask-SQLAlchemy columns")
Expand All @@ -57,7 +58,7 @@ def main():
outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints,
args.nojoined, args.noinflect, args.nobackrefs,
args.flask, ignore_cols, args.noclasses, args.nocomments)
args.flask, ignore_cols, args.noclasses, args.nocomments, args.notables)
generator.render(outfile)


Expand Down

0 comments on commit 4f9c22f

Please sign in to comment.