Skip to content

Commit 90af0bb

Browse files
committed
support custom migration table
1 parent 02b7493 commit 90af0bb

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

pum/schema_migrations.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,17 @@ def create(self, conn: Connection, commit: bool = True):
7777
if self.exists(conn):
7878
logger.debug(f"{self.config.schema_migrations_table} table already exists")
7979
return
80+
81+
# Create the schema if it doesn't exist
82+
create_schema_query = None
83+
schema = "public"
84+
table_identifiers = self.config.schema_migrations_table.split(".")
85+
if len(table_identifiers) == 2:
86+
schema = table_identifiers[0]
87+
if schema != "public":
88+
create_schema_query = sql.SQL("CREATE SCHEMA IF NOT EXISTS {schema};").format(schema=sql.Identifier(schema))
8089

81-
create_query = sql.SQL(
90+
create_table_query = sql.SQL(
8291
"""CREATE TABLE IF NOT EXISTS {schema_migrations_table}
8392
(
8493
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
@@ -102,7 +111,9 @@ def create(self, conn: Connection, commit: bool = True):
102111
schema_migrations_table=sql.Identifier(*self.config.schema_migrations_table.split("."))
103112
)
104113

105-
execute_sql(conn, create_query)
114+
if create_schema_query:
115+
execute_sql(conn, create_schema_query)
116+
execute_sql(conn, create_table_query)
106117
execute_sql(conn, comment_query)
107118

108119
logger.info(f"Created {self.config.schema_migrations_table} table")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
schema_migrations_table:
3+
pum_test.custom_migration
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE SCHEMA IF NOT EXISTS pum_test_data;
2+
3+
CREATE TABLE pum_test_data.some_table (
4+
id INT PRIMARY KEY,
5+
name VARCHAR(100) NOT NULL,
6+
created_date DATE DEFAULT CURRENT_DATE,
7+
is_active BOOLEAN DEFAULT TRUE,
8+
amount NUMERIC(10,2)
9+
);

test/test_upgrader.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TestUpgrader(unittest.TestCase):
1919
def tearDown(self):
2020
self.cur.execute("DROP SCHEMA IF EXISTS pum_test_data CASCADE;")
2121
self.cur.execute("DROP TABLE IF EXISTS public.pum_migrations;")
22+
self.cur.execute("DROP TABLE IF EXISTS pum_test.custom_migration;")
2223
self.conn.commit()
2324
self.conn.close()
2425

@@ -35,6 +36,7 @@ def setUp(self):
3536
self.cur = self.conn.cursor()
3637
self.cur.execute("DROP SCHEMA IF EXISTS pum_test_data CASCADE;")
3738
self.cur.execute("DROP TABLE IF EXISTS public.pum_migrations;")
39+
self.cur.execute("DROP TABLE IF EXISTS pum_test.custom_migration;")
3840
self.conn.commit()
3941

4042
self.tmpdir = tempfile.TemporaryDirectory()
@@ -92,6 +94,16 @@ def test_install_custom_directory(self):
9294
upgrader.install()
9395
self.assertTrue(sm.exists(self.conn))
9496

97+
def test_install_custom_migration_table(self):
98+
cfg = PumConfig.from_yaml(str(Path("test") / "data" / "custom_migration_table" / ".pum-config.yaml"))
99+
sm = SchemaMigrations(cfg)
100+
self.assertFalse(sm.exists(self.conn))
101+
upgrader = Upgrader(
102+
pg_service=self.pg_service, config=cfg, dir=str(Path("test") / "data" / "custom_migration_table")
103+
)
104+
upgrader.install()
105+
self.assertTrue(sm.exists(self.conn))
106+
95107
def test_install_multiple_changelogs(self):
96108
cfg = PumConfig()
97109
sm = SchemaMigrations(cfg)

0 commit comments

Comments
 (0)