Skip to content

Commit c0215ec

Browse files
authored
prevent install in same migration schema (#165)
1 parent 0ccbc43 commit c0215ec

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pum/upgrader.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,16 @@ def upgrade(
294294
raise PumException(msg)
295295

296296
migration_details = self.schema_migrations.migration_details(connection)
297+
298+
installed_module = migration_details.get("module")
299+
if installed_module and installed_module != self.config.config.pum.module:
300+
msg = (
301+
f"Cannot upgrade: the installed module is '{installed_module}' but the "
302+
f"configuration defines module '{self.config.config.pum.module}'. "
303+
"Make sure you are using the correct configuration file for this database."
304+
)
305+
raise PumException(msg)
306+
297307
installed_beta_testing = bool(migration_details.get("beta_testing", False))
298308
if installed_beta_testing and not force:
299309
msg = (

test/test_schema_migrations.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,22 @@ def test_schemas_with_migration_details_multiple_schemas(self) -> None:
248248
self.assertEqual(by_schema["public"]["version"], "1.0.0")
249249
self.assertEqual(by_schema["pum_custom_migrations_schema"]["module"], "module_b")
250250
self.assertEqual(by_schema["pum_custom_migrations_schema"]["version"], "2.0.0")
251+
252+
def test_upgrade_module_mismatch(self) -> None:
253+
"""Test that upgrading module X over an installation of module Y is rejected."""
254+
test_dir = Path("test") / "data" / "single_changelog"
255+
256+
# Install module_a
257+
cfg_a = PumConfig(test_dir, pum={"module": "module_a"})
258+
upgrader_a = Upgrader(config=cfg_a)
259+
with psycopg.connect(f"service={self.pg_service}") as conn:
260+
upgrader_a.install(connection=conn)
261+
262+
# Try to upgrade with module_b config — should fail
263+
cfg_b = PumConfig(test_dir, pum={"module": "module_b"})
264+
upgrader_b = Upgrader(config=cfg_b)
265+
with psycopg.connect(f"service={self.pg_service}") as conn:
266+
with self.assertRaises(PumException) as context:
267+
upgrader_b.upgrade(connection=conn)
268+
self.assertIn("module_a", str(context.exception))
269+
self.assertIn("module_b", str(context.exception))

0 commit comments

Comments
 (0)