Skip to content

Commit

Permalink
current_timestamp to over models
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuinside committed Oct 13, 2022
1 parent 38033b8 commit de6259f
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 38 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
**v0.12.1**
### Improvements

1. current_timestamp function processed now same was as "now()" function from ddl


**v0.12.0**
### Fixes

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

### New feature:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,14 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h


## Changelog
**v0.12.1**
### Improvements

1. current_timestamp https://github.com/xnuinside/omymodels/pull/38 processed now same was as now()


**v0.12.0**
### Fixes

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

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

**v0.12.1**

Improvements
^^^^^^^^^^^^


#. current_timestamp https://github.com/xnuinside/omymodels/pull/38 processed now same was as now()

**v0.12.0**

Fixes
Expand Down
8 changes: 7 additions & 1 deletion omymodels/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_singular_name(table_name: Text, exceptions: Optional[List] = None) -> Te
def create_class_name(
table_name: Text, singular: bool = False, exceptions: Optional[List] = None
) -> Text:
""" create correct class name for table in PascalCase """
"""create correct class name for table in PascalCase"""
if singular:
model_name = get_singular_name(table_name)
else:
Expand Down Expand Up @@ -97,3 +97,9 @@ def add_custom_types_to_generator(types: List[Type], generator: object) -> objec
for _type in types
}
return generator


def datetime_now_check(string: str) -> bool:
if "now" in string or "current_timestamp" in string:
return True
return False
11 changes: 6 additions & 5 deletions omymodels/models/dataclass/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Optional, List, Dict
from typing import Dict, List, Optional

import omymodels.types as t
from omymodels.helpers import create_class_name, datetime_now_check
from omymodels.models.dataclass import templates as dt
from omymodels.helpers import create_class_name
from omymodels.models.dataclass.types import types_mapping
from omymodels.types import datetime_types
import omymodels.types as t


class ModelGenerator:
Expand Down Expand Up @@ -59,7 +60,7 @@ def generate_attr(self, column: Dict, defaults_off: bool) -> str:
@staticmethod
def add_column_default(column_str: str, column: Dict) -> str:
if column.type.upper() in datetime_types:
if "now" in column.default.lower():
if datetime_now_check(column.default.lower()):
# todo: need to add other popular PostgreSQL & MySQL functions
column.default = dt.field_datetime_now
elif "'" not in column.default:
Expand Down Expand Up @@ -88,7 +89,7 @@ def generate_model(
)
) + "\n\n"
columns = {"default": [], "non_default": []}

# generate columns / attrs
for column in table.columns:
column = t.prepare_column_data(column)
Expand Down
13 changes: 7 additions & 6 deletions omymodels/models/gino/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Optional, List, Dict
from typing import Dict, List, Optional

import omymodels.models.gino.templates as gt
from omymodels import logic
from omymodels.helpers import create_class_name, datetime_now_check
from omymodels.models.gino.types import types_mapping
from omymodels.types import datetime_types
from omymodels.helpers import create_class_name
from omymodels import logic


class ModelGenerator:
Expand All @@ -20,7 +21,7 @@ def __init__(self):
def prepare_column_default(self, column_data: Dict, column: str) -> str:
if isinstance(column_data.default, str):
if column_data.type.upper() in datetime_types:
if "now" in column_data.default.lower():
if datetime_now_check(column_data.default.lower()):
# todo: need to add other popular PostgreSQL & MySQL functions
column_data.default = "func.now()"
self.state.add("func")
Expand All @@ -43,7 +44,7 @@ def generate_model(
*args,
**kwargs,
) -> str:
""" method to prepare one Model defention - name & tablename & columns """
"""method to prepare one Model defention - name & tablename & columns"""
model = ""
model = gt.model_template.format(
model_name=create_class_name(table.name, singular, exceptions),
Expand All @@ -57,7 +58,7 @@ def generate_model(
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
""" header of the file - imports & gino init """
"""header of the file - imports & gino init"""
header = ""
if "func" in self.state:
header += gt.sql_alchemy_func_import + "\n"
Expand Down
12 changes: 7 additions & 5 deletions omymodels/models/pydantic/core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Optional, List, Dict
from typing import Dict, List, Optional

from table_meta.model import Column

import omymodels.types as t
from omymodels.helpers import create_class_name, datetime_now_check
from omymodels.models.pydantic import templates as pt
from omymodels.helpers import create_class_name
from omymodels.models.pydantic.types import types_mapping
from omymodels.types import datetime_types
import omymodels.types as t
from table_meta.model import Column


class ModelGenerator:
Expand Down Expand Up @@ -69,7 +71,7 @@ def generate_attr(self, column: Dict, defaults_off: bool) -> str:
@staticmethod
def add_default_values(column_str: str, column: Dict) -> str:
if column.type.upper() in datetime_types:
if "now" in column.default.lower() or "current_timestamp" in column.default.lower():
if datetime_now_check(column.default.lower()):
# todo: need to add other popular PostgreSQL & MySQL functions
column.default = "datetime.datetime.now()"
elif "'" not in column.default:
Expand Down
13 changes: 7 additions & 6 deletions omymodels/models/sqlalchemy/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Optional, List, Dict
from typing import Dict, List, Optional

import omymodels.models.sqlalchemy.templates as st
from omymodels import logic
from omymodels.helpers import create_class_name, datetime_now_check
from omymodels.models.sqlalchemy.types import types_mapping
from omymodels.types import datetime_types
from omymodels.helpers import create_class_name
from omymodels import logic


class GeneratorBase:
Expand All @@ -25,7 +26,7 @@ def __init__(self):
def prepare_column_default(self, column_data: Dict, column: str) -> str:
if isinstance(column_data.default, str):
if column_data.type.upper() in datetime_types:
if "now" in column_data.default.lower():
if datetime_now_check(column_data.default.lower()):
# todo: need to add other popular PostgreSQL & MySQL functions
column_data.default = "func.now()"
self.state.add("func")
Expand All @@ -48,7 +49,7 @@ def generate_model(
*args,
**kwargs,
) -> str:
""" method to prepare one Model defention - name & tablename & columns """
"""method to prepare one Model defention - name & tablename & columns"""
model = ""

model = st.model_template.format(
Expand All @@ -62,7 +63,7 @@ def generate_model(
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
""" header of the file - imports & sqlalchemy init """
"""header of the file - imports & sqlalchemy init"""
header = ""
if "func" in self.state:
header += st.sql_alchemy_func_import + "\n"
Expand Down
26 changes: 14 additions & 12 deletions omymodels/models/sqlalchemy_core/core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import List, Dict
import omymodels.models.sqlalchemy_core.templates as st
from omymodels.models.sqlalchemy.types import types_mapping, postgresql_dialect
from omymodels.types import datetime_types
import omymodels.types as t
from typing import Dict, List

from table_meta.model import Column

import omymodels.models.sqlalchemy_core.templates as st
import omymodels.types as t
from omymodels.helpers import datetime_now_check
from omymodels.models.sqlalchemy.types import postgresql_dialect, types_mapping
from omymodels.types import datetime_types


class ModelGenerator:
def __init__(self):
Expand All @@ -27,7 +29,7 @@ def add_custom_type(self, column_data_type: str, column_type: str) -> str:
return column_type

def prepare_column_type(self, column_data: Dict) -> str:
""" extract and map column type """
"""extract and map column type"""
self.no_need_par = False
column_type = None
if "." in column_data.type:
Expand Down Expand Up @@ -59,10 +61,10 @@ def add_size_to_column_type(size):
return f"({','.join([str(x) for x in size])})"

def column_default(self, column_data: Dict) -> str:
""" extract & format column default values """
"""extract & format column default values"""
if isinstance(column_data.default, str):
if column_data.type.upper() in datetime_types:
if "now" in column_data.default.lower():
if datetime_now_check(column_data.default.lower()):
# todo: need to add other popular PostgreSQL & MySQL functions
column_data.default = "func.now()"
self.state.add("func")
Expand Down Expand Up @@ -114,7 +116,7 @@ def get_column_attributes(

@staticmethod
def column_reference(column_name: str, reference: Dict[str, str]) -> str:
""" ForeignKey property creator """
"""ForeignKey property creator"""
ref_property = st.fk_in_column.format(
ref_table=reference["table"], ref_column=reference["column"] or column_name
)
Expand All @@ -127,7 +129,7 @@ def column_reference(column_name: str, reference: Dict[str, str]) -> str:
def generate_column(
self, column_data: Column, table_pk: List[str], table_data: Dict
) -> str:
""" method to generate full column defention """
"""method to generate full column defention"""
column_data = t.prepare_column_data(column_data)
column_type = self.prepare_column_type(column_data)
properties = "".join(
Expand Down Expand Up @@ -174,7 +176,7 @@ def get_indexes_and_unique(
return indexes, unique_constr

def generate_model(self, data: Dict, *args, **kwargs) -> str:
""" method to prepare one Model defention - name & tablename & columns """
"""method to prepare one Model defention - name & tablename & columns"""
model = ""
# mean this is a table
table = data
Expand Down Expand Up @@ -207,7 +209,7 @@ def generate_model(self, data: Dict, *args, **kwargs) -> str:
return model

def create_header(self, tables: List[Dict], schema: bool = False) -> str:
""" header of the file - imports & sqlalchemy init """
"""header of the file - imports & sqlalchemy init"""
header = ""
if "func" in self.state:
header += st.sql_alchemy_func_import + "\n"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "omymodels"
version = "0.12.0"
version = "0.12.1"
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 <xnuinside@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit de6259f

Please sign in to comment.