Skip to content

Commit

Permalink
fix sqlmodels generation (#60)
Browse files Browse the repository at this point in the history
* fix sqlmodels generation

* fix flake8 issues
  • Loading branch information
xnuinside authored May 12, 2024
1 parent 4893e38 commit 9817c43
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 70 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
**v0.16.0**

### Updates
1. fix character varying type - https://github.com/xnuinside/omymodels/issues/59
2. sqlalchemy import removed from generation in sqlmodels if it is not used
3. = Field() - is not placed in SQLModel if there is no defaults or other settings to the field


**v0.15.1**
## Updates
1. Foreign Key processing updates - https://github.com/xnuinside/omymodels/pull/55
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h
One more time, big 'thank you!' goes to https://github.com/archongum for Web-version: https://archon-omymodels-online.hf.space/

## Changelog
**v0.16.0**

### Updates
1. fix character varying type - https://github.com/xnuinside/omymodels/issues/59
2. sqlalchemy import removed from generation in sqlmodels if it is not used
3. = Field() - is not placed in SQLModel if there is no defaults or other settings to the field


**v0.15.1**
## Updates
1. Foreign Key processing updates - https://github.com/xnuinside/omymodels/pull/55
Expand Down
10 changes: 10 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ One more time, big 'thank you!' goes to https://github.com/archongum for Web-ver
Changelog
---------

**v0.16.0**

Updates
^^^^^^^


#. fix character varying type - https://github.com/xnuinside/omymodels/issues/59
#. sqlalchemy import removed from generation in sqlmodels if it is not used
#. = Field() - is not placed in SQLModel if there is no defaults or other settings to the field

**v0.15.1**

Updates
Expand Down
3 changes: 2 additions & 1 deletion omymodels/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def version(**kwargs):

def cli():
omm_cli = argparse.ArgumentParser(
description="O! My Models. Create GinoORM models from SQL DDL"
description="O! My Models. "
"Create SQLModels, SQLAlchemy, GinoORM and other models from SQL DDL or another models"
)

omm_cli.add_argument(
Expand Down
4 changes: 3 additions & 1 deletion omymodels/from_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def generate_models_file(
schema_global=schema_global,
defaults_off=defaults_off,
)
header += generator.create_header(data["tables"], schema=schema_global)
header += generator.create_header(
data["tables"], schema=schema_global, models_str=models_str
)
else:
models_type = "enum"
output = render_jinja2_template(models_type, models_str, header)
Expand Down
2 changes: 1 addition & 1 deletion omymodels/models/enum/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def create_types(self) -> str:
self.custom_types = {x.name: ("db.Enum", x.name) for x in self.types}
return types_str

def create_header(self) -> str:
def create_header(self, *args, **kwargs) -> str:
self.enum_imports = list(self.enum_imports)
self.enum_imports.sort()
return enum_import.format(enums=", ".join(self.enum_imports)) + "\n"
4 changes: 3 additions & 1 deletion omymodels/models/gino/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def generate_model(
# create sequence
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
def create_header(
self, tables: List[Dict], schema: bool = False, *args, **kwargs
) -> str:
"""header of the file - imports & gino init"""
header = ""
if "func" in self.state:
Expand Down
4 changes: 3 additions & 1 deletion omymodels/models/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def generate_model(
model = logic.add_table_args(self, model, table, schema_global)
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
def create_header(
self, tables: List[Dict], schema: bool = False, *args, **kwargs
) -> str:
"""header of the file - imports & sqlalchemy init"""
header = ""
if "func" in self.state:
Expand Down
4 changes: 3 additions & 1 deletion omymodels/models/sqlalchemy_core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def generate_model(self, data: Dict, *args, **kwargs) -> str:
model += index
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
def create_header(
self, tables: List[Dict], schema: bool = False, *args, **kwargs
) -> str:
"""header of the file - imports & sqlalchemy init"""
header = ""
if "func" in self.state:
Expand Down
26 changes: 15 additions & 11 deletions omymodels/models/sqlmodel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def generate_model(
**kwargs,
) -> str:
"""method to prepare one Model defention - name & tablename & columns"""
model = ""

model = st.model_template.format(
model_name=create_class_name(table.name, singular, exceptions),
Expand All @@ -133,26 +132,31 @@ def generate_model(
col_str = st.column_template.format(
column_name=column.name.replace(" ", "_"), column_type=pydantic_type_str
)

col_str = logic.setup_column_attributes(
column, table.primary_key, col_str, table, schema_global, st, self
attrs_col_str = logic.setup_column_attributes(
column, table.primary_key, "", table, schema_global, st, self
)
if column_type["sa"]:
sa_type = types.add_size_to_orm_column(column_type["sa"], column)
col_str += st.sa_type.format(satype=sa_type)
col_str += ")\n"

col_str = col_str.replace("(, ", "(")

attrs_col_str += st.sa_type.format(satype=sa_type)
if attrs_col_str:
attrs_col_str = attrs_col_str.replace(",", "", 1).strip()
col_str += st.field_template.format(attr_data=attrs_col_str)
col_str += "\n"
model += col_str
if table.indexes or table.alter or table.checks or not schema_global:
model = self.add_table_args(model, table, schema_global)
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
def create_header(
self,
tables: List[Dict],
models_str: str,
schema: bool = False,
) -> str:
"""header of the file - imports & sqlalchemy init"""
header = ""
header += st.sqlalchemy_import # Do we always need this import?
if "sa." in models_str:
header += st.sqlalchemy_import # Do we always need this import?
if "func" in self.state:
header += st.sql_alchemy_func_import + "\n"
if self.postgresql_dialect_cols:
Expand Down
4 changes: 1 addition & 3 deletions omymodels/models/sqlmodel/sqlmodel.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ import datetime
import decimal
from typing import Optional
from sqlmodel import Field, SQLModel

{{ headers }}
{{ models }}
{{ headers }}{{ models }}
3 changes: 2 additions & 1 deletion omymodels/models/sqlmodel/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class {model_name}(SQLModel, table=True):\n
"""

# columns defenition
column_template = """ {column_name}: {column_type} = Field("""
column_template = """ {column_name}: {column_type}"""
field_template = """ = Field({attr_data})"""
required = ""
default = ", sa_column_kwargs={{'server_default': {default}}}"
pk_template = ", default=None, primary_key=True"
Expand Down
2 changes: 1 addition & 1 deletion omymodels/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"str",
"varchar",
"character",
"character_vying",
"character varying",
"varying",
"char",
"string",
Expand Down
3 changes: 3 additions & 0 deletions one.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE `option` (
FIELD1 VARCHAR(256),
) ;
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "omymodels"
version = "0.15.1"
version = "0.16.0"
description = "O! My Models (omymodels) is a library to generate Python Models for SQLAlchemy (ORM & Core), GinoORM, Pydantic, Pydal tables & Python Dataclasses from SQL DDL. And convert one models to another."
authors = ["Iuliia Volkova <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -32,14 +32,10 @@ table-meta = "^0.1.5"

[tool.poetry.dev-dependencies]
pytest = "^7.4"
m2r = "^0.3.1"

[tool.poetry.scripts]
omm = 'omymodels.cli:main'

[tool.poetry.group.dev.dependencies]
twine = "^4.0.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Loading

0 comments on commit 9817c43

Please sign in to comment.