Skip to content

Commit d51500f

Browse files
authored
Merge pull request piccolo-orm#29 from piccolo-orm/improved_migration_exit_codes
modified exit codes when no migrations are left to run / reverse
2 parents 2c50122 + 18c45f6 commit d51500f

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

piccolo/apps/migrations/commands/backwards.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ async def run(self):
4040
app_name=self.app_name
4141
)
4242
if len(ran_migration_ids) == 0:
43-
sys.exit("No migrations to reverse!")
43+
# Make sure a status of 0 is returned, as we don't want this
44+
# to appear as an error in automated scripts.
45+
print("No migrations to reverse!")
46+
sys.exit(0)
4447

4548
#######################################################################
4649

piccolo/apps/migrations/commands/forwards.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ async def run_migrations(self, app_config: AppConfig) -> None:
3838
print(f"Haven't run = {havent_run}")
3939

4040
if len(havent_run) == 0:
41-
sys.exit("No migrations left to run!")
41+
# Make sure a status of 0 is returned, as we don't want this
42+
# to appear as an error in automated scripts.
43+
print("No migrations left to run!")
44+
sys.exit(0)
4245

4346
if self.migration_id == "all":
4447
subset = havent_run

tests/apps/migrations/commands/test_forwards_backwards.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import typing as t
44
from unittest import TestCase
5+
from unittest.mock import patch, call, MagicMock
56

67
from piccolo.apps.migrations.tables import Migration
78
from piccolo.apps.migrations.commands.backwards import backwards
@@ -121,7 +122,8 @@ def test_backwards_unknown_migration(self):
121122
)
122123
)
123124

124-
def test_backwards_no_migrations(self):
125+
@patch("piccolo.apps.migrations.commands.backwards.print")
126+
def test_backwards_no_migrations(self, print_):
125127
"""
126128
Test running migrations backwards if none have been run previously.
127129
"""
@@ -134,11 +136,13 @@ def test_backwards_no_migrations(self):
134136
)
135137
)
136138

137-
self.assertEqual(
138-
manager.exception.__str__(), "No migrations to reverse!"
139+
self.assertEqual(manager.exception.code, 0)
140+
self.assertTrue(
141+
print_.mock_calls[-1] == call("No migrations to reverse!")
139142
)
140143

141-
def test_forwards_no_migrations(self):
144+
@patch("piccolo.apps.migrations.commands.forwards.print")
145+
def test_forwards_no_migrations(self, print_: MagicMock):
142146
"""
143147
Test running the migrations if they've already run.
144148
"""
@@ -147,8 +151,9 @@ def test_forwards_no_migrations(self):
147151
with self.assertRaises(SystemExit) as manager:
148152
run_sync(forwards(app_name="example_app", migration_id="all"))
149153

150-
self.assertEqual(
151-
manager.exception.__str__(), "No migrations left to run!"
154+
self.assertEqual(manager.exception.code, 0)
155+
self.assertTrue(
156+
print_.mock_calls[-1] == call("No migrations left to run!")
152157
)
153158

154159
def test_forwards_fake(self):

0 commit comments

Comments
 (0)