Skip to content

Commit e73e347

Browse files
committed
SQL Check bug fixing: removed
1 parent abd9732 commit e73e347

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

lib/db/component.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ def datetime_now(cls, name: str, use_localtime: bool = False) -> 'Field':
7676
return cls(name=name, type="DATETIME", default=SqlUtils.datetime_strf_now(use_localtime))
7777

7878
@classmethod
79-
def nullable_date_with_now_check_field(cls, name: str, default: str | None = 'NULL', use_localtime: bool = False) -> 'Field':
80-
return cls(name=name, type="DATE", default=default, nullable=True,
81-
check=f"{name} IS NULL OR {SqlUtils.date_str_format(name, use_localtime=use_localtime)} > {SqlUtils.date_strf_now(use_localtime=use_localtime)}")
79+
def nullable_date(cls, name: str, default: str | None = 'NULL') -> 'Field':
80+
return cls(name=name, type="DATE", default=default, nullable=True)
8281

8382
@classmethod
84-
def nullable_datetime_with_now_check_field(cls, name: str, default: str | None = 'NULL', use_localtime: bool = False) -> 'Field':
85-
return cls(name=name, type="DATETIME", default=default, nullable=True,
86-
check=f"{name} IS NULL OR {SqlUtils.datetime_str_format(name, use_localtime=use_localtime)} > {SqlUtils.datetime_strf_now(use_localtime=use_localtime)}")
83+
def nullable_datetime(cls, name: str, default: str | None = 'NULL') -> 'Field':
84+
return cls(name=name, type="DATETIME", default=default, nullable=True)
8785

8886
@classmethod
8987
def hex_color(cls, name: str = "hex_color", nullable: bool = False, default: Optional[str] = None):
@@ -97,10 +95,14 @@ def to_sql(self) -> str:
9795
:rtype str:
9896
"""
9997

100-
return f"{self.name} {self.type} {'DEFAULT (' + self.default + ')' if self.default is not None else ''} " + \
101-
f"{'UNIQUE' if self.unique else ''} {'PRIMARY KEY' if self.pk else ''} {'AUTOINCREMENT' if self.autoincrement else ''} " + \
102-
f"{'NULL' if self.nullable and not self.pk else 'NOT NULL'}" + \
103-
f"CHECK({self.check})"
98+
query = f"{self.name} {self.type} {'DEFAULT (' + self.default + ')' if self.default is not None else ''} " + \
99+
f"{'UNIQUE' if self.unique else ''} {'PRIMARY KEY' if self.pk else ''} {'AUTOINCREMENT' if self.autoincrement else ''} " + \
100+
f"{'NULL' if self.nullable and not self.pk else 'NOT NULL'} "
101+
102+
if self.check is not None:
103+
query += f"CHECK({self.check})"
104+
105+
return query
104106

105107

106108
@dataclass

lib/db/db.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def tables(self) -> Dict[str, Table]:
258258
Field(name="password", type="VARCHAR(256)", unique=False),
259259
Field.hex_color(name="avatar_hex_color", default="'#cfcfcf'", nullable=False),
260260
Field(name="phone", type="VARCHAR(30)", nullable=True),
261-
Field.nullable_datetime_with_now_check_field("last_visit_at", use_localtime=self.use_localtime, default=None),
261+
Field.nullable_datetime("last_visit_at", default=None),
262262
Field.fk_field(name="role_id"),
263263
], fk_constraints=[
264264
FKConstraint.on_id(fk_field="role_id", on_table=self.role_table_name, on_update="CASCADE", on_delete="RESTRICT")
@@ -303,7 +303,7 @@ def tables(self) -> Dict[str, Table]:
303303
Field.id_field(),
304304
Field.name_field(unique=False),
305305
Field.description_field(),
306-
Field.nullable_datetime_with_now_check_field(name="deadline"),
306+
Field.nullable_datetime(name="deadline"),
307307
Field(name="priority", type="INTEGER", default="0"),
308308
Field.created_at_field(use_localtime=self.use_localtime),
309309
Field.updated_at_field(use_localtime=self.use_localtime),
@@ -330,7 +330,7 @@ def tables(self) -> Dict[str, Table]:
330330
self.task_table_name
331331
], other_fields=[
332332
Field.datetime_now("assigned_at", use_localtime=self.use_localtime),
333-
Field.nullable_datetime_with_now_check_field("last_watched_at", use_localtime=self.use_localtime, default=None),
333+
Field.nullable_datetime("last_watched_at", default=None),
334334
], with_triggers=[
335335
Trigger(
336336
name=f"{self.task_assignment_table_name}_on_insert_updater_trigger",
@@ -352,7 +352,7 @@ def tables(self) -> Dict[str, Table]:
352352
self.todo_item_table_name: Table(self.todo_item_table_name, [
353353
Field.id_field(),
354354
Field.description_field(nullable=False),
355-
Field.nullable_datetime_with_now_check_field(name="deadline"),
355+
Field.nullable_datetime(name="deadline"),
356356
Field.created_at_field(use_localtime=self.use_localtime),
357357
Field.updated_at_field(use_localtime=self.use_localtime),
358358
Field(name="done", type="INTEGER", default="0"),

0 commit comments

Comments
 (0)