mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
parent
923768db2e
commit
6500fed8b2
2 changed files with 56 additions and 2 deletions
|
|
@ -542,6 +542,24 @@ class Database:
|
|||
'"{}"'.format(bit) if not bit.startswith('"') else bit for bit in bits
|
||||
)
|
||||
|
||||
def quote_default_value(self, value: str) -> str:
|
||||
if any(
|
||||
[
|
||||
str(value).startswith("'") and str(value).endswith("'"),
|
||||
str(value).startswith('"') and str(value).endswith('"'),
|
||||
]
|
||||
):
|
||||
return value
|
||||
|
||||
if str(value).upper() in ("CURRENT_TIME", "CURRENT_DATE", "CURRENT_TIMESTAMP"):
|
||||
return value
|
||||
|
||||
if str(value).endswith(")"):
|
||||
# Expr
|
||||
return "({})".format(value)
|
||||
|
||||
return self.quote(value)
|
||||
|
||||
def table_names(self, fts4: bool = False, fts5: bool = False) -> List[str]:
|
||||
"""
|
||||
List of string table names in this database.
|
||||
|
|
@ -839,7 +857,7 @@ class Database:
|
|||
column_extras.append("NOT NULL")
|
||||
if column_name in defaults and defaults[column_name] is not None:
|
||||
column_extras.append(
|
||||
"DEFAULT {}".format(self.quote(defaults[column_name]))
|
||||
"DEFAULT {}".format(self.quote_default_value(defaults[column_name]))
|
||||
)
|
||||
if column_name in foreign_keys_by_column:
|
||||
column_extras.append(
|
||||
|
|
@ -2020,7 +2038,9 @@ class Table(Queryable):
|
|||
col_type = str
|
||||
not_null_sql = None
|
||||
if not_null_default is not None:
|
||||
not_null_sql = "NOT NULL DEFAULT {}".format(self.db.quote(not_null_default))
|
||||
not_null_sql = "NOT NULL DEFAULT {}".format(
|
||||
self.db.quote_default_value(not_null_default)
|
||||
)
|
||||
sql = "ALTER TABLE [{table}] ADD COLUMN [{col_name}] {col_type}{not_null_default};".format(
|
||||
table=self.name,
|
||||
col_name=col_name,
|
||||
|
|
|
|||
34
tests/test_default_value.py
Normal file
34
tests/test_default_value.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import pytest
|
||||
|
||||
|
||||
EXAMPLES = [
|
||||
("TEXT DEFAULT 'foo'", "'foo'", "'foo'"),
|
||||
("TEXT DEFAULT 'foo)'", "'foo)'", "'foo)'"),
|
||||
("INTEGER DEFAULT '1'", "'1'", "'1'"),
|
||||
("INTEGER DEFAULT 1", "1", "'1'"),
|
||||
("INTEGER DEFAULT (1)", "1", "'1'"),
|
||||
# Expressions
|
||||
(
|
||||
"TEXT DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))",
|
||||
"STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
|
||||
"(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))",
|
||||
),
|
||||
# Special values
|
||||
("TEXT DEFAULT CURRENT_TIME", "CURRENT_TIME", "CURRENT_TIME"),
|
||||
("TEXT DEFAULT CURRENT_DATE", "CURRENT_DATE", "CURRENT_DATE"),
|
||||
("TEXT DEFAULT CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP"),
|
||||
("TEXT DEFAULT current_timestamp", "current_timestamp", "current_timestamp"),
|
||||
("TEXT DEFAULT (CURRENT_TIMESTAMP)", "CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP"),
|
||||
# Strings
|
||||
("TEXT DEFAULT 'CURRENT_TIMESTAMP'", "'CURRENT_TIMESTAMP'", "'CURRENT_TIMESTAMP'"),
|
||||
('TEXT DEFAULT "CURRENT_TIMESTAMP"', '"CURRENT_TIMESTAMP"', '"CURRENT_TIMESTAMP"'),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("column_def,initial_value,expected_value", EXAMPLES)
|
||||
def test_quote_default_value(fresh_db, column_def, initial_value, expected_value):
|
||||
fresh_db.execute("create table foo (col {})".format(column_def))
|
||||
assert initial_value == fresh_db["foo"].columns[0].default_value
|
||||
assert expected_value == fresh_db.quote_default_value(
|
||||
fresh_db["foo"].columns[0].default_value
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue