77in the database.
88"""
99
10+ import json
1011import logging
1112import re
1213
@@ -54,11 +55,11 @@ def exists(self, conn: Connection) -> bool:
5455 query = sql .SQL (
5556 """
5657 SELECT EXISTS (
57- SELECT 1
58- FROM information_schema.tables
59- WHERE table_name = {table} AND table_schema = {schema}
60- )
61- """
58+ SELECT 1
59+ FROM information_schema.tables
60+ WHERE table_name = {table} AND table_schema = {schema}
61+ );
62+ """
6263 ).format (
6364 schema = sql .Literal (schema ),
6465 table = sql .Literal (table ),
@@ -67,33 +68,6 @@ def exists(self, conn: Connection) -> bool:
6768 cursor = execute_sql (conn , query )
6869 return cursor .fetchone ()[0 ]
6970
70- def installed_modules (self , conn : Connection ) -> list [tuple [str , str ]]:
71- """
72- Returns the installed modules and their versions from the schema_migrations table.
73- Args:
74- conn (Connection): The database connection to fetch the installed modules and versions.
75- Returns:
76- list[tuple[str, str]]: A list of tuples containing the module name and version.
77- """
78- query = sql .SQL (
79- """
80- SELECT module, version
81- FROM (
82- SELECT module, version,
83- ROW_NUMBER() OVER (PARTITION BY module ORDER BY date_installed DESC) AS rn
84- FROM {schema_migrations_table}
85- ) t
86- WHERE t.rn = 1;
87- """
88- ).format (
89- schema_migrations_table = sql .Identifier (
90- * self .config .schema_migrations_table .split ("." )
91- )
92- )
93- cursor = conn .cursor ()
94- cursor .execute (query )
95- return cursor .fetchall ()
96-
9771 def create (self , conn : Connection , commit : bool = True ):
9872 """
9973 Creates the schema_migrations information table
@@ -115,6 +89,7 @@ def create(self, conn: Connection, commit: bool = True):
11589 version character varying(50) NOT NULL,
11690 beta_testing boolean NOT NULL DEFAULT false,
11791 changelog_files text[],
92+ parameters jsonb,
11893 migration_table_version character varying(50) NOT NULL DEFAULT {version}
11994 );
12095 """
@@ -147,6 +122,8 @@ def set_baseline(
147122 version : Version | str ,
148123 beta_testing : bool = False ,
149124 commit : bool = True ,
125+ changelog_files : list [str ] = None ,
126+ parameters : dict = None ,
150127 ):
151128 """
152129 Sets the baseline into the migration table
@@ -170,13 +147,17 @@ def set_baseline(
170147 query = sql .SQL (
171148 """
172149 INSERT INTO {schema_migrations_table} (
173- version,
174- beta_testing,
175- migration_table_version
150+ version,
151+ beta_testing,
152+ migration_table_version,
153+ changelog_files,
154+ parameters
176155 ) VALUES (
177- {version},
178- {beta_testing},
179- {migration_table_version}
156+ {version},
157+ {beta_testing},
158+ {migration_table_version},
159+ {changelog_files},
160+ {parameters}
180161 )
181162 """
182163 ).format (
@@ -186,10 +167,89 @@ def set_baseline(
186167 schema_migrations_table = sql .Identifier (
187168 * self .config .schema_migrations_table .split ("." )
188169 ),
170+ changelog_files = sql .Literal (changelog_files or []),
171+ parameters = sql .Literal (json .dumps (parameters or {})),
189172 )
190173 logger .info (
191174 f"Setting baseline version { version } in { self .config .schema_migrations_table } "
192175 )
193176 conn .execute (query )
194177 if commit :
195178 conn .commit ()
179+
180+ def baseline (self , conn : Connection ) -> str :
181+ """
182+ Returns the baseline version from the migration table
183+ Args:
184+ conn: Connection
185+ The database connection to get the baseline version.
186+ Returns:
187+ str: The baseline version.
188+ """
189+ query = sql .SQL (
190+ """
191+ SELECT version
192+ FROM {schema_migrations_table}
193+ WHERE id = (
194+ SELECT id
195+ FROM {schema_migrations_table}
196+ ORDER BY date_installed DESC
197+ LIMIT 1
198+ )
199+ """
200+ ).format (
201+ schema_migrations_table = sql .Identifier (
202+ * self .config .schema_migrations_table .split ("." )
203+ )
204+ )
205+ cursor = execute_sql (conn , query )
206+ return cursor .fetchone ()[0 ]
207+
208+ def migration_details (self , conn : Connection , version : str = None ) -> dict :
209+ """
210+ Returns the migration details from the migration table
211+ Args:
212+ conn: Connection
213+ The database connection to get the migration details.
214+ version: str
215+ The version of the migration to get details for. If None, last migration is returned.
216+ Returns:
217+ dict: The migration details.
218+ """
219+ query = None
220+ if version is None :
221+ query = sql .SQL (
222+ """
223+ SELECT *
224+ FROM {schema_migrations_table}
225+ WHERE id = (
226+ SELECT id
227+ FROM {schema_migrations_table}
228+ ORDER BY date_installed DESC
229+ LIMIT 1
230+ )
231+ ORDER BY date_installed DESC
232+ """
233+ ).format (
234+ schema_migrations_table = sql .Identifier (
235+ * self .config .schema_migrations_table .split ("." )
236+ ),
237+ )
238+ else :
239+ query = sql .SQL (
240+ """
241+ SELECT *
242+ FROM {schema_migrations_table}
243+ WHERE version = {version}
244+ """
245+ ).format (
246+ schema_migrations_table = sql .Identifier (
247+ * self .config .schema_migrations_table .split ("." )
248+ ),
249+ version = sql .Literal (version ),
250+ )
251+ cursor = execute_sql (conn , query )
252+ row = cursor .fetchone ()
253+ if row is None :
254+ return None
255+ return dict (zip ([desc [0 ] for desc in cursor .description ], row ))
0 commit comments