Skip to content

Commit

Permalink
added set_precision to Alter query for Numeric columns
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed May 25, 2020
1 parent eebf552 commit 08a042e
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions piccolo/query/methods/alter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing as t

from piccolo.columns.base import Column, OnDelete, OnUpdate
from piccolo.columns.column_types import ForeignKey, Varchar
from piccolo.columns.column_types import ForeignKey, Varchar, Numeric
from piccolo.query.base import Query
from piccolo.querystring import QueryString
from piccolo.utils.warnings import colored_warning, Level
Expand Down Expand Up @@ -73,7 +73,10 @@ def querystring(self) -> QueryString:

@dataclass
class AddColumn(AlterColumnStatement):
__slots__ = ("name",)
__slots__ = (
"column",
"name",
)

column: Column
name: str
Expand Down Expand Up @@ -153,7 +156,13 @@ def querystring(self) -> QueryString:

@dataclass
class AddForeignKeyConstraint(AlterStatement):
__slots__ = ("constraint_name",)
__slots__ = (
"constraint_name",
"foreign_key_column_name",
"referenced_table_name",
"on_delete",
"on_update",
)

constraint_name: str
foreign_key_column_name: str
Expand All @@ -176,6 +185,25 @@ def querystring(self) -> QueryString:
return QueryString(query)


@dataclass
class SetPrecision(AlterColumnStatement):

__slots__ = ("precision", "scale")

precision: int
scale: int
column_type: str = "NUMERIC"

@property
def querystring(self) -> QueryString:
return QueryString(
f"ALTER COLUMN {{}} TYPE {self.column_type}({{}}, {{}})",
self.column_name,
self.precision,
self.scale,
)


class Alter(Query):

__slots__ = (
Expand All @@ -189,6 +217,7 @@ class Alter(Query):
"_rename_table",
"_set_length",
"_unique",
"_set_precision",
)

def __init__(self, table: t.Type[Table]):
Expand All @@ -203,6 +232,7 @@ def __init__(self, table: t.Type[Table]):
self._rename_table: t.List[RenameTable] = []
self._set_length: t.List[SetLength] = []
self._unique: t.List[Unique] = []
self._set_precision: t.List[SetPrecision] = []

def add_column(self, name: str, column: Column) -> Alter:
"""
Expand Down Expand Up @@ -341,6 +371,17 @@ def add_foreign_key_constraint(
)
return self

def set_precision(
self,
column: t.Union[str, Numeric],
precision: t.Optional[int],
scale: t.Optional[int],
):
kwargs: t.Dict[str, t.Any] = {"precision": precision, "scale": scale}
if isinstance(column, Numeric):
kwargs["column_type"] = column.__class__.__name__.upper()
self._set_precision.append(SetPrecision(**kwargs))

async def response_handler(self, response):
"""
We don't want to modify the response, but we need to add default values
Expand Down

0 comments on commit 08a042e

Please sign in to comment.