Skip to content

Commit b70742b

Browse files
committed
add tests for Timestamp columns in migrations
1 parent 2c543f5 commit b70742b

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

piccolo/apps/migrations/commands/new.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def _generate_migration_meta(app_config: AppConfig) -> NewMigrationMeta:
8383
)
8484

8585

86+
class NoChanges(Exception):
87+
pass
88+
89+
8690
async def _create_new_migration(
8791
app_config: AppConfig, auto=False
8892
) -> NewMigrationMeta:
@@ -108,8 +112,7 @@ async def _create_new_migration(
108112
)
109113

110114
if sum([len(i.statements) for i in alter_statements]) == 0:
111-
print("No changes detected - exiting.")
112-
sys.exit(0)
115+
raise NoChanges()
113116

114117
file_contents = render_template(
115118
migration_id=meta.migration_id,
@@ -193,4 +196,7 @@ async def new(app_name: str, auto: bool = False):
193196
app_config = Finder().get_app_config(app_name=app_name)
194197

195198
_create_migrations_folder(app_config.migrations_folder_path)
196-
await _create_new_migration(app_config=app_config, auto=auto)
199+
try:
200+
await _create_new_migration(app_config=app_config, auto=auto)
201+
except NoChanges:
202+
print("No changes detected - exiting.")

piccolo/columns/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def get_sql_value(self, value: t.Any) -> t.Any:
540540
elif isinstance(value, bool):
541541
output = str(value).lower()
542542
elif isinstance(value, datetime.datetime):
543-
output = f"'{value.isoformat().replace('T', '')}'"
543+
output = f"'{value.isoformat().replace('T', ' ')}'"
544544
elif isinstance(value, bytes):
545545
output = f"'{value.hex()}'"
546546
elif isinstance(value, uuid.UUID):

tests/apps/migrations/auto/integration/test_migrations.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
import datetime
23
import os
34
import shutil
45
import tempfile
@@ -14,6 +15,7 @@
1415
Integer,
1516
SmallInt,
1617
Text,
18+
Timestamp,
1719
UUID,
1820
Varchar,
1921
)
@@ -43,6 +45,10 @@ def uuid_default():
4345
return uuid.uuid4()
4446

4547

48+
def datetime_default():
49+
return datetime.datetime.now()
50+
51+
4652
@postgres_only
4753
class TestMigrations(TestCase):
4854
def tearDown(self):
@@ -198,3 +204,22 @@ def test_uuid_column(self):
198204
]
199205
]
200206
)
207+
208+
def test_timestamp_column(self):
209+
self._test_migrations(
210+
table_classes=[
211+
self.table(column)
212+
for column in [
213+
Timestamp(),
214+
Timestamp(
215+
default=datetime.datetime(year=2021, month=1, day=1)
216+
),
217+
Timestamp(default=datetime.datetime.now),
218+
Timestamp(default=datetime_default),
219+
Timestamp(null=True, default=None),
220+
Timestamp(null=False),
221+
Timestamp(index=True),
222+
Timestamp(index=False),
223+
]
224+
]
225+
)

0 commit comments

Comments
 (0)