Skip to content

Commit

Permalink
Merge branch 'master' into timestamptz_column
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Jan 27, 2021
2 parents 18b4a26 + 3b19aa0 commit b417dc7
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changes
=======

0.16.0
------
* Fixed a bug with creating a ``ForeignKey`` column with ``references="self"``
in auto migrations.
* Changed migration file naming, so there are no characters in there which
are unsupported on Windows.

0.15.1
------
Changing the status code when creating a migration, and no changes were
Expand Down
2 changes: 1 addition & 1 deletion piccolo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__VERSION__ = "0.15.1"
__VERSION__ = "0.16.0"
3 changes: 2 additions & 1 deletion piccolo/apps/migrations/auto/serialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def deserialise_params(params: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
for key, value in params.items():
# This is purely for backwards compatibility.
if isinstance(value, str) and not isinstance(value, Enum):
params[key] = deserialise_legacy_params(name=key, value=value)
if value != "self":
params[key] = deserialise_legacy_params(name=key, value=value)
elif isinstance(value, SerialisedClass):
params[key] = value.instance
elif isinstance(value, SerialisedUUID):
Expand Down
9 changes: 8 additions & 1 deletion piccolo/apps/migrations/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ async def _create_new_migration(app_config: AppConfig, auto=False) -> None:
Creates a new migration file on disk.
"""
_id = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
path = os.path.join(app_config.migrations_folder_path, f"{_id}.py")

# Originally we just used the _id as the filename, but colons aren't
# supported in Windows, so we need to sanitize it. We don't want to
# change the _id format though, as it would break existing migrations.
# The filename doesn't have any special significance - only the id matters.
filename = _id.replace(":", "-")

path = os.path.join(app_config.migrations_folder_path, f"{filename}.py")

if auto:
alter_statements = await AutoMigrationManager().get_alter_statements(
Expand Down
46 changes: 46 additions & 0 deletions tests/apps/migrations/auto/test_migration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from piccolo.apps.migrations.auto import MigrationManager
from piccolo.apps.migrations.commands.base import BaseMigrationManager
from piccolo.columns import Varchar
from piccolo.columns.base import OnDelete, OnUpdate

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

@postgres_only
def test_add_foreign_key_self_column(self):
"""
Test adding a ForeignKey column to a MigrationManager, with a
references argument of 'self'.
"""
manager = MigrationManager()
manager.add_column(
table_class_name="Manager",
tablename="manager",
column_name="advisor",
column_class_name="ForeignKey",
params={
"references": "self",
"on_delete": OnDelete.cascade,
"on_update": OnUpdate.cascade,
"default": None,
"null": True,
"primary": False,
"key": False,
"unique": False,
"index": False,
},
)
asyncio.run(manager.run())

self.run_sync("INSERT INTO manager VALUES (default, 'Alice', null);")
self.run_sync("INSERT INTO manager VALUES (default, 'Dave', 1);")

response = self.run_sync("SELECT * FROM manager;")
self.assertEqual(
response,
[
{"id": 1, "name": "Alice", "advisor": None},
{"id": 2, "name": "Dave", "advisor": 1},
],
)

# Reverse
asyncio.run(manager.run_backwards())
response = self.run_sync("SELECT * FROM manager;")
self.assertEqual(
response, [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Dave"}],
)

@postgres_only
def test_add_non_nullable_column(self):
"""
Expand Down

0 comments on commit b417dc7

Please sign in to comment.