Skip to content

Commit 9f37b30

Browse files
authored
Merge pull request #66 from eakoli/feat/defaults
Feat/defaults
2 parents 1204bdf + 21842f2 commit 9f37b30

7 files changed

Lines changed: 96 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ CREATE TYPE "orders_status" AS ENUM (
9797
'created',
9898
'running',
9999
'done',
100-
'failure',
100+
'failure'
101101
);
102102
<BLANKLINE>
103103
CREATE TYPE "product status" AS ENUM (
104104
'Out of Stock',
105-
'In Stock',
105+
'In Stock'
106106
);
107107
<BLANKLINE>
108108
CREATE TABLE "orders" (

pydbml/renderer/dbml/default/column.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def render_options(model: Column) -> str:
2424
options.append('pk')
2525
if model.autoinc:
2626
options.append('increment')
27-
if model.default:
27+
if model.default is not None:
2828
options.append(f'default: {default_to_str(model.default)}')
2929
if model.unique:
3030
options.append('unique')

pydbml/renderer/sql/default/column.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
from typing import Union
2+
13
from pydbml.classes import Column, Enum, Expression
24
from pydbml.renderer.sql.default.renderer import DefaultSQLRenderer
35
from .utils import comment_to_sql
46
from .enum import get_full_name_for_sql as get_full_name_for_sql_enum
57

8+
def default_to_str(val: Union[Expression, str, int, float, bool]) -> str:
9+
if isinstance(val, Expression):
10+
return DefaultSQLRenderer.render(val)
11+
elif isinstance(val, str):
12+
val = val.replace("'", "''")
13+
return f"'{val}'"
14+
else:
15+
return str(val)
616

717
@DefaultSQLRenderer.renderer_for(Column)
818
def render_column(model: Column) -> str:
@@ -28,11 +38,7 @@ def render_column(model: Column) -> str:
2838
if model.not_null:
2939
components.append('NOT NULL')
3040
if model.default is not None:
31-
if isinstance(model.default, Expression):
32-
default = DefaultSQLRenderer.render(model.default)
33-
else:
34-
default = model.default # type: ignore
35-
components.append(f'DEFAULT {default}')
41+
components.append(f'DEFAULT {default_to_str(model.default)}')
3642

3743
result = comment_to_sql(model.comment) if model.comment else ''
3844
result += ' '.join(components)

test/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,50 @@ def complex_column_with_table(db: Database, table1: Table, complex_column: Colum
6565
db.add(table1)
6666
return complex_column
6767

68+
@pytest.fixture
69+
def string_column() -> Column:
70+
return Column(
71+
name='name',
72+
type='varchar(255)',
73+
pk=False,
74+
autoinc=False,
75+
unique=True,
76+
not_null=True,
77+
default='value\'s',
78+
comment='This is a defaulted string column',
79+
note=Note('This is a note for the column'),
80+
properties={'foo': 'bar', 'baz': "qux\nqux"}
81+
)
82+
83+
84+
@pytest.fixture
85+
def string_column_with_table(db: Database, table1: Table, string_column: Column) -> Column:
86+
table1.add_column(string_column)
87+
db.add(table1)
88+
return string_column
89+
90+
@pytest.fixture
91+
def boolean_column() -> Column:
92+
return Column(
93+
name='enabled',
94+
type='boolean',
95+
pk=False,
96+
autoinc=False,
97+
unique=False,
98+
not_null=True,
99+
default=False,
100+
comment='This is a defaulted boolean column',
101+
note=Note('This is a note for the column'),
102+
properties={'foo': 'bar', 'baz': "qux\nqux"}
103+
)
104+
105+
106+
@pytest.fixture
107+
def boolean_column_with_table(db: Database, table1: Table, boolean_column: Column) -> Column:
108+
table1.add_column(boolean_column)
109+
db.add(table1)
110+
return boolean_column
111+
68112

69113
@pytest.fixture
70114
def table1() -> Table:

test/test_data/integration1.dbml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Enum "level" {
1212
Table "Employees" as "emp" {
1313
"id" integer [pk, increment]
1414
"name" varchar [note: 'Full employee name']
15-
"age" number
15+
"age" number [default: 0]
1616
"level" level
1717
"favorite_book_id" integer
1818
}

test/test_renderer/test_dbml/test_column.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,25 @@ def test_enum(simple_column_with_table: Column, enum1: Enum) -> None:
133133
def test_complex(complex_column_with_table: Column) -> None:
134134
expected = (
135135
"// This is a counter column\n"
136-
'"counter" "product status" [pk, increment, unique, not null, note: \'This is '
136+
'"counter" "product status" [pk, increment, default: 0, unique, not null, note: \'This is '
137137
"a note for the column']"
138138
)
139139
assert render_column(complex_column_with_table) == expected
140+
141+
@staticmethod
142+
def test_string(string_column_with_table: Column) -> None:
143+
expected = (
144+
"// This is a defaulted string column\n"
145+
'"name" varchar(255) [default: \'value\\\'s\', unique, not null, note: \'This is '
146+
"a note for the column']"
147+
)
148+
assert render_column(string_column_with_table) == expected
149+
150+
@staticmethod
151+
def test_boolean(boolean_column_with_table: Column) -> None:
152+
expected = (
153+
"// This is a defaulted boolean column\n"
154+
'"enabled" boolean [default: False, not null, note: \'This is '
155+
"a note for the column']"
156+
)
157+
assert render_column(boolean_column_with_table) == expected

test/test_renderer/test_sql/test_default/test_column.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,21 @@ def test_complex(complex_column: Column) -> None:
1717
"0"
1818
)
1919
assert render_column(complex_column) == expected
20+
21+
@staticmethod
22+
def test_string(string_column: Column) -> None:
23+
expected = (
24+
"-- This is a defaulted string column\n"
25+
'"name" varchar(255) UNIQUE NOT NULL DEFAULT '
26+
"'value''s'"
27+
)
28+
assert render_column(string_column) == expected
29+
30+
@staticmethod
31+
def test_string(boolean_column: Column) -> None:
32+
expected = (
33+
"-- This is a defaulted boolean column\n"
34+
'"enabled" boolean NOT NULL DEFAULT '
35+
"False"
36+
)
37+
assert render_column(boolean_column) == expected

0 commit comments

Comments
 (0)