-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from OCHA-DAP/reflection
Table names in snake case
- Loading branch information
Showing
5 changed files
with
33 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Other utilities""" | ||
|
||
import re | ||
|
||
|
||
def camel_to_snake_case(string: str) -> str: | ||
"""Convert a ``CamelCase`` name to ``snake_case``. | ||
(Taken from flask-sqlalchemy code.) | ||
Args: | ||
string (str): String to convert | ||
Returns: | ||
str: String in snake case | ||
""" | ||
string = re.sub( | ||
r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", string | ||
) | ||
return string.lower().lstrip("_") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from sqlalchemy.orm import DeclarativeBase, declared_attr | ||
|
||
from .utils import camel_to_snake_case | ||
|
||
|
||
class Base(DeclarativeBase): | ||
@declared_attr.directive | ||
def __tablename__(cls): | ||
return f"{cls.__name__.lower()}s" | ||
return camel_to_snake_case(cls.__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters