diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml new file mode 100644 index 0000000..4e1ef42 --- /dev/null +++ b/.github/workflows/pythonpublish.yml @@ -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/* diff --git a/README.md b/README.md index b3771fe..86cdf46 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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://:@:/ [--tables ] [--notables] +``` \ No newline at end of file diff --git a/README.rst b/README.rst index bba4faf..9b10c59 100644 --- a/README.rst +++ b/README.rst @@ -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 ------- diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3f99ce0 --- /dev/null +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py index f7f55d9..b38222e 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/sqlacodegen/__init__.py b/sqlacodegen/__init__.py index 7347817..1d39f81 100644 --- a/sqlacodegen/__init__.py +++ b/sqlacodegen/__init__.py @@ -1 +1 @@ -version = __version__ = '1.1.7' +version = __version__ = '1.1.8' diff --git a/sqlacodegen/codegen.py b/sqlacodegen/codegen.py index e6c9794..4a6ef12 100644 --- a/sqlacodegen/codegen.py +++ b/sqlacodegen/codegen.py @@ -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)) @@ -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: @@ -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(): diff --git a/sqlacodegen/main.py b/sqlacodegen/main.py index d9444e2..8e62f03 100644 --- a/sqlacodegen/main.py +++ b/sqlacodegen/main.py @@ -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") @@ -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)