Skip to content

Commit b417dc7

Browse files
committed
Merge branch 'master' into timestamptz_column
2 parents 18b4a26 + 3b19aa0 commit b417dc7

File tree

11 files changed

+64
-3
lines changed

11 files changed

+64
-3
lines changed

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changes
22
=======
33

4+
0.16.0
5+
------
6+
* Fixed a bug with creating a ``ForeignKey`` column with ``references="self"``
7+
in auto migrations.
8+
* Changed migration file naming, so there are no characters in there which
9+
are unsupported on Windows.
10+
411
0.15.1
512
------
613
Changing the status code when creating a migration, and no changes were

piccolo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__VERSION__ = "0.15.1"
1+
__VERSION__ = "0.16.0"

piccolo/apps/migrations/auto/serialisation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ def deserialise_params(params: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
225225
for key, value in params.items():
226226
# This is purely for backwards compatibility.
227227
if isinstance(value, str) and not isinstance(value, Enum):
228-
params[key] = deserialise_legacy_params(name=key, value=value)
228+
if value != "self":
229+
params[key] = deserialise_legacy_params(name=key, value=value)
229230
elif isinstance(value, SerialisedClass):
230231
params[key] = value.instance
231232
elif isinstance(value, SerialisedUUID):

piccolo/apps/migrations/commands/new.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ async def _create_new_migration(app_config: AppConfig, auto=False) -> None:
5757
Creates a new migration file on disk.
5858
"""
5959
_id = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
60-
path = os.path.join(app_config.migrations_folder_path, f"{_id}.py")
60+
61+
# Originally we just used the _id as the filename, but colons aren't
62+
# supported in Windows, so we need to sanitize it. We don't want to
63+
# change the _id format though, as it would break existing migrations.
64+
# The filename doesn't have any special significance - only the id matters.
65+
filename = _id.replace(":", "-")
66+
67+
path = os.path.join(app_config.migrations_folder_path, f"{filename}.py")
6168

6269
if auto:
6370
alter_statements = await AutoMigrationManager().get_alter_statements(

piccolo/apps/user/piccolo_migrations/2019-11-14T21:52:21.py renamed to piccolo/apps/user/piccolo_migrations/2019-11-14T21-52-21.py

File renamed without changes.

piccolo/apps/user/piccolo_migrations/2020-06-11T21:38:55.py renamed to piccolo/apps/user/piccolo_migrations/2020-06-11T21-38-55.py

File renamed without changes.

tests/apps/migrations/auto/test_migration_manager.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from piccolo.apps.migrations.auto import MigrationManager
66
from piccolo.apps.migrations.commands.base import BaseMigrationManager
77
from piccolo.columns import Varchar
8+
from piccolo.columns.base import OnDelete, OnUpdate
89

910
from tests.example_app.tables import Manager
1011
from tests.base import DBTestCase
@@ -145,6 +146,51 @@ def test_add_column(self):
145146
response = self.run_sync("SELECT * FROM manager;")
146147
self.assertEqual(response, [{"id": 1, "name": "Dave"}])
147148

149+
@postgres_only
150+
def test_add_foreign_key_self_column(self):
151+
"""
152+
Test adding a ForeignKey column to a MigrationManager, with a
153+
references argument of 'self'.
154+
"""
155+
manager = MigrationManager()
156+
manager.add_column(
157+
table_class_name="Manager",
158+
tablename="manager",
159+
column_name="advisor",
160+
column_class_name="ForeignKey",
161+
params={
162+
"references": "self",
163+
"on_delete": OnDelete.cascade,
164+
"on_update": OnUpdate.cascade,
165+
"default": None,
166+
"null": True,
167+
"primary": False,
168+
"key": False,
169+
"unique": False,
170+
"index": False,
171+
},
172+
)
173+
asyncio.run(manager.run())
174+
175+
self.run_sync("INSERT INTO manager VALUES (default, 'Alice', null);")
176+
self.run_sync("INSERT INTO manager VALUES (default, 'Dave', 1);")
177+
178+
response = self.run_sync("SELECT * FROM manager;")
179+
self.assertEqual(
180+
response,
181+
[
182+
{"id": 1, "name": "Alice", "advisor": None},
183+
{"id": 2, "name": "Dave", "advisor": 1},
184+
],
185+
)
186+
187+
# Reverse
188+
asyncio.run(manager.run_backwards())
189+
response = self.run_sync("SELECT * FROM manager;")
190+
self.assertEqual(
191+
response, [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Dave"}],
192+
)
193+
148194
@postgres_only
149195
def test_add_non_nullable_column(self):
150196
"""

tests/apps/migrations/commands/test_migrations/2020-03-31T20:38:22.py renamed to tests/apps/migrations/commands/test_migrations/2020-03-31T20-38-22.py

File renamed without changes.

tests/example_app/piccolo_migrations/2020-12-17T18:44:30.py renamed to tests/example_app/piccolo_migrations/2020-12-17T18-44-30.py

File renamed without changes.

tests/example_app/piccolo_migrations/2020-12-17T18:44:39.py renamed to tests/example_app/piccolo_migrations/2020-12-17T18-44-39.py

File renamed without changes.

0 commit comments

Comments
 (0)