Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solve issue with kwargs in models #37

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
**v0.12.0**
### Fixes

1. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35

### New feature:
1. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36.
Now, if you want to keep names 1-to-1 as in your DDL file, you can set argument `no_auto_snake_case=True` and O!MyModels will do nothing with the table or column names.



**v0.11.1**

### Improvements:
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h


## Changelog
**v0.12.0**
### Fixes

1. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35

### New feature:
1. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36.
Now, if you want to keep names 1-to-1 as in your DDL file, you can set argument `no_auto_snake_case=True` and O!MyModels will do nothing with the table or column names.



**v0.11.1**

### Improvements:
Expand Down
15 changes: 15 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h
Changelog
---------

**v0.12.0**

Fixes
^^^^^


#. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35

New feature:
^^^^^^^^^^^^


#. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36.
Now, if you want to keep names 1-to-1 as in your DDL file, you can set argument ``no_auto_snake_case=True`` and O!MyModels will do nothing with the table or column names.

**v0.11.1**

Improvements:
Expand Down
10 changes: 7 additions & 3 deletions omymodels/from_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def create_models(
schema_global: Optional[bool] = True,
defaults_off: Optional[bool] = False,
exit_silent: Optional[bool] = False,
no_auto_snake_case: Optional[bool] = False
):
""" models_type can be: "gino", "dataclass", "pydantic" """
# extract data from ddl file
data = get_tables_information(ddl, ddl_path)
data = prepare_data(data)
data = convert_ddl_to_models(data)
data = convert_ddl_to_models(data, no_auto_snake_case)
if not data["tables"] and not data["types"]:
if exit_silent:
sys.exit(0)
Expand All @@ -61,15 +62,18 @@ def create_models(


def snake_case(string: str) -> str:
if string.lower() in ['id']:
return string.lower()
return re.sub(r"(?<!^)(?=[A-Z])", "_", string).lower()


def convert_ddl_to_models(data):
def convert_ddl_to_models(data: Dict, no_auto_snake_case: bool) -> Dict[str, list]:
final_data = {"tables": [], "types": []}
tables = []
for table in data["tables"]:
for column in table["columns"]:
column["name"] = snake_case(column["name"])
if not no_auto_snake_case:
column["name"] = snake_case(column["name"])
tables.append(TableMeta(**table))
final_data["tables"] = tables
_types = []
Expand Down
26 changes: 14 additions & 12 deletions omymodels/logic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Dict, List

import omymodels.types as t


def generate_column(
column_data: Dict, table_pk: List[str], table_data: Dict, templates, obj
) -> str:
""" method to generate full column defention for sqlalchemy & gino ORM models """
"""method to generate full column defention for sqlalchemy & gino ORM models"""
column_type = t.prepare_column_type_orm(obj, column_data)
column = templates.column_template.format(
column_name=column_data.name, column_type=column_type
Expand All @@ -25,7 +26,19 @@ def setup_column_attributes(
templates,
obj,
) -> str:
# foreign is a positional - so it should be before keyword args
if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
):

column = add_reference_to_the_column(
alter_column["name"], column, alter_column["references"], templates
)
# keyword named args
if column_data.type.lower() == "serial" or column_data.type.lower() == "bigserial":
column += templates.autoincrement
if column_data.references:
Expand All @@ -41,17 +54,6 @@ def setup_column_attributes(
if column_data.unique:
column += templates.unique

if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
):

column = add_reference_to_the_column(
alter_column["name"], column, alter_column["references"], templates
)
return column


Expand Down
3 changes: 3 additions & 0 deletions omymodels/models/dataclass/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ def generate_model(

# mean one model one table
model += "\n\n"
# generate class name
model += (
dt.dataclass_class.format(
class_name=create_class_name(table.name, singular, exceptions),
table_name=table.name,
)
) + "\n\n"
columns = {"default": [], "non_default": []}

# generate columns / attrs
for column in table.columns:
column = t.prepare_column_data(column)
column_str = self.generate_attr(column, defaults_off) + "\n"
Expand Down
Loading