Skip to content

Schema migrations

Manage the schema migrations in the database. It provides methods to create the schema_migrations table, check its existence, set the baseline version, and retrieve migration details.

Version Added

1.0.0

Source code in pum/schema_migrations.py
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
class SchemaMigrations:
    """Manage the schema migrations in the database.
    It provides methods to create the schema_migrations table, check its existence,
    set the baseline version, and retrieve migration details.

    Version Added:
        1.0.0
    """

    def __init__(self, config: PumConfig) -> None:
        """Initialize the SchemaMigrations class with a database connection and configuration.

        Args:
            config (PumConfig): An instance of the PumConfig class containing configuration settings for the PUM system.

        """
        self.config = config
        self.migration_table_identifier = psycopg.sql.SQL(".").join(
            [
                psycopg.sql.Identifier(self.config.config.pum.migration_table_schema),
                psycopg.sql.Identifier(MIGRATION_TABLE_NAME),
            ]
        )
        self.migration_table_identifier_str = (
            f"{self.config.config.pum.migration_table_schema}.{MIGRATION_TABLE_NAME}"
        )

    def exists(self, connection: psycopg.Connection) -> bool:
        """Check if the schema_migrations information table exists.

        Args:
            connection: The database connection to check for the existence of the table.

        Returns:
            bool: True if the table exists, False otherwise.

        """
        query = psycopg.sql.SQL(
            """
        SELECT EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_name = 'pum_migrations' AND table_schema = {schema}
        );
        """
        )

        parameters = {
            "schema": psycopg.sql.Literal(self.config.config.pum.migration_table_schema),
        }

        with connection.transaction():
            cursor = SqlContent(query).execute(connection, parameters=parameters)
            result = cursor._pum_results[0] if cursor._pum_results else None
            return result[0] if result else False

    @staticmethod
    def schemas_with_migrations(connection: psycopg.Connection) -> list[str]:
        """Check if the schema_migrations information table exists in any schemas.

        Args:
            connection: The database connection to check for the existence of the table.

        Returns:
            List[str]: List of all schemas where the table exists.

        """
        query = psycopg.sql.SQL(
            """
            SELECT table_schema
            FROM information_schema.tables
            WHERE table_name = 'pum_migrations'
        """
        )
        with connection.transaction():
            cursor = SqlContent(query).execute(connection, parameters={})
            return [row[0] for row in (cursor._pum_results or [])]

    @staticmethod
    def schemas_with_migration_details(
        connection: psycopg.Connection,
    ) -> list[dict]:
        """Return detailed migration info for each schema that has a pum_migrations table.

        For each schema, returns the module name, current version, first installation
        date and latest upgrade date (if different from the installation date).

        Args:
            connection: The database connection.

        Returns:
            list[dict]: A list of dicts with keys: schema, module, version,
                        installed_date, upgrade_date (None if never upgraded).

        Version Added:
            1.4.0
        """
        with connection.transaction():
            schemas = SchemaMigrations.schemas_with_migrations(connection)
            details = []
            for schema in schemas:
                table_id = psycopg.sql.SQL(".").join(
                    [psycopg.sql.Identifier(schema), psycopg.sql.Identifier(MIGRATION_TABLE_NAME)]
                )
                query = psycopg.sql.SQL(
                    """
                    SELECT
                        module,
                        version,
                        installed_date,
                        CASE WHEN upgrade_date > installed_date THEN upgrade_date END AS upgrade_date,
                        beta_testing
                    FROM (
                        SELECT
                            module,
                            (SELECT version FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS version,
                            MIN(date_installed) AS installed_date,
                            MAX(date_installed) AS upgrade_date,
                            (SELECT beta_testing FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS beta_testing
                        FROM {table}
                        GROUP BY module
                    ) sub
                    """
                )
                try:
                    cursor = SqlContent(query).execute(connection, parameters={"table": table_id})
                    for row in cursor._pum_results or []:
                        details.append(
                            {
                                "schema": schema,
                                "module": row[0],
                                "version": row[1],
                                "installed_date": row[2],
                                "upgrade_date": row[3],
                                "beta_testing": row[4],
                            }
                        )
                except Exception:
                    logger.warning(f"Could not read migration details from schema {schema}")
                    details.append(
                        {
                            "schema": schema,
                            "module": None,
                            "version": None,
                            "installed_date": None,
                            "upgrade_date": None,
                            "beta_testing": None,
                        }
                    )
            return details

    def exists_in_other_schemas(self, connection: psycopg.Connection) -> list[str]:
        """Check if the schema_migrations information table exists in other schemas.

        Args:
            connection: The database connection to check for the existence of the table.

        Returns:
            List[str]: List of schemas where the table exists.

        """
        with connection.transaction():
            all_schemas = self.schemas_with_migrations(connection)
            return [
                schema
                for schema in all_schemas
                if schema != self.config.config.pum.migration_table_schema
            ]

    def create(
        self,
        connection: psycopg.Connection,
        *,
        allow_multiple_modules: bool = False,
        commit: bool = False,
    ) -> None:
        """Create the schema_migrations information table
        Args:
            connection: The database connection to create the table.
            commit: If true, the transaction is committed. The default is false.
            allow_multiple_modules: If true, allows multiple PUM modules (with separate
                pum_migrations tables in different schemas) in the same database. Default is false.
        """
        if self.exists(connection):
            logger.debug(
                f"{self.config.config.pum.migration_table_schema}.pum_migrations table already exists."
            )
            return

        if not allow_multiple_modules:
            schemas = self.exists_in_other_schemas(connection)
            if len(schemas) > 1:
                raise PumSchemaMigrationError(
                    f"Another {MIGRATION_TABLE_NAME} table exists in other schemas {', '.join(schemas)}, indicating other PUM modules in this database. "
                    "Use allow_multiple_modules=True to allow multiple PUM modules per database."
                )
            elif len(schemas) == 1:
                raise PumSchemaMigrationError(
                    f"Another {MIGRATION_TABLE_NAME} table exists in schema {schemas[0]}, indicating another PUM module in this database. "
                    "Use allow_multiple_modules=True to allow multiple PUM modules per database."
                )
        # Create the schema if it doesn't exist
        parameters = {
            "version": psycopg.sql.Literal(MIGRATION_TABLE_VERSION),
            "schema": psycopg.sql.Identifier(self.config.config.pum.migration_table_schema),
            "table": self.migration_table_identifier,
        }

        create_schema_query = None
        if self.config.config.pum.migration_table_schema != "public":
            create_schema_query = psycopg.sql.SQL("CREATE SCHEMA IF NOT EXISTS {schema};")

        create_table_query = psycopg.sql.SQL(
            """CREATE TABLE IF NOT EXISTS {table}
            (
            id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
            date_installed timestamp without time zone NOT NULL DEFAULT now(),
            module character varying(50) NOT NULL,
            version character varying(50) NOT NULL,
            beta_testing boolean NOT NULL DEFAULT false,
            changelog_files text[],
            parameters jsonb,
            migration_table_version integer NOT NULL DEFAULT {version}
            );
        """
        )

        comment_query = psycopg.sql.SQL("COMMENT ON TABLE {table} IS 'migration_table_version: 2';")

        if create_schema_query:
            SqlContent(create_schema_query).execute(connection, parameters=parameters)
        SqlContent(create_table_query).execute(connection, parameters=parameters)
        SqlContent(comment_query).execute(connection, parameters=parameters)

        logger.info(f"Created migration table: {self.migration_table_identifier_str}")

        if commit:
            connection.commit()

    def migration_table_version(self, connection: psycopg.Connection) -> int:
        """Return the migration table version.

        Args:
            connection: The database connection to get the migration table version.

        Returns:
            int | None: The migration table version, or None if the table does not exist.

        """
        query = psycopg.sql.SQL(
            """
            SELECT migration_table_version
            FROM {table}
            ORDER BY migration_table_version DESC
            LIMIT 1;
        """
        )

        parameters = {
            "table": self.migration_table_identifier,
        }

        with connection.transaction():
            cursor = SqlContent(query).execute(connection, parameters=parameters)
            row = cursor._pum_results[0] if cursor._pum_results else None
            if row is None:
                raise PumSchemaMigrationError(
                    f"Migration table {self.migration_table_identifier_str} does not exist."
                )
            return row[0]

    def update_migration_table_schema(self, connection: psycopg.Connection) -> None:
        """Update the migration table schema to the latest version.

        Args:
            connection: The database connection to update the table.

        """
        table_version = self.migration_table_version(connection)
        logger.info(
            f"Updating migration table {self.migration_table_identifier_str} from version {table_version} to {MIGRATION_TABLE_VERSION}."
        )
        if table_version == 1:
            alter_query = psycopg.sql.SQL("""
                                          ALTER TABLE {table} ALTER COLUMN migration_table_version ALTER TYPE integer SET DEFAULT {version} USING 1;
                                          ALTER TABLE {table} ALTER COLUMN module SET NOT NULL USING 'tww';
                                          """)
            parameters = {
                "table": self.migration_table_identifier,
                "version": psycopg.sql.Literal(MIGRATION_TABLE_VERSION),
            }
            SqlContent(alter_query).execute(connection, parameters=parameters)

    def set_baseline(
        self,
        connection: psycopg.Connection,
        version: packaging.version.Version | str,
        changelog_files: list[str] | None = None,
        parameters: dict | None = None,
        *,
        beta_testing: bool = False,
        commit: bool = False,
    ) -> None:
        """Set the baseline into the migration table.

        Args:
            connection: The database connection to set the baseline version.
            version: The version of the current database to set in the information.
            changelog_files: The list of changelog files that were applied.
            parameters: The parameters used in the migration.
            beta_testing: If true, the baseline is set to beta testing mode. The default is false.
            commit: If true, the transaction is committed. The default is False.

        """
        version_str = version
        version_packaging = version
        if isinstance(version_str, packaging.version.Version):
            version_str = str(version_str)
        if isinstance(version_packaging, str):
            version_packaging = packaging.version.parse(version_packaging)
        pattern = re.compile(r"^\d+\.\d+(\.\d+)?$")
        if not re.match(pattern, version_str):
            raise ValueError(f"Wrong version format: {version}. Must be x.y or x.y.z")

        try:
            current = self.baseline(connection=connection)
        except PumSchemaMigrationNoBaselineError:
            current = None
        if current:
            self.update_migration_table_schema(connection)
        if current and current >= version_packaging:
            raise PumSchemaMigrationError(
                f"Cannot set baseline {version_str} as it is already set at {current}."
            )

        code = psycopg.sql.SQL("""
INSERT INTO {table} (
    module,
    version,
    beta_testing,
    migration_table_version,
    changelog_files,
    parameters
) VALUES (
    {module},
    {version},
    {beta_testing},
    {migration_table_version},
    {changelog_files},
    {parameters}
);""")

        query_parameters = {
            "table": self.migration_table_identifier,
            "module": psycopg.sql.Literal(self.config.config.pum.module),
            "version": psycopg.sql.Literal(version_str),
            "beta_testing": psycopg.sql.Literal(beta_testing),
            "migration_table_version": psycopg.sql.Literal(MIGRATION_TABLE_VERSION),
            "changelog_files": psycopg.sql.Literal(changelog_files or []),
            "parameters": psycopg.sql.Literal(json.dumps(parameters or {})),
        }

        logger.info(
            f"Setting baseline version {version} in {self.migration_table_identifier_str} table"
        )
        SqlContent(code).execute(connection, parameters=query_parameters, commit=commit)

    def has_baseline(self, connection: psycopg.Connection) -> bool:
        """Check if the migration table has a baseline version.

        Args:
            connection: The database connection to check for the baseline version.
        Returns:
            bool: True if the baseline version exists, False otherwise.
        """
        with connection.transaction():
            try:
                self.baseline(connection=connection)
                return True
            except PumSchemaMigrationError:
                return False

    def baseline(self, connection: psycopg.Connection) -> packaging.version.Version:
        """Return the baseline version from the migration table.

        Args:
            connection: psycopg.Connection
                The database connection to get the baseline version.

        Returns:
            packaging.version.Version | None: The baseline version.

        Raises:
            PumSchemaMigrationError: If the migration table does not exist or if no baseline version is found
            PumSchemaMigrationNoBaselineError: If the migration table does not exist
        """

        with connection.transaction():
            if not self.exists(connection=connection):
                raise PumSchemaMigrationError(
                    f"{self.migration_table_identifier_str} table does not exist."
                )

            query = psycopg.sql.SQL(
                """
                SELECT version
                FROM {table}
                WHERE id = (
                    SELECT id
                    FROM {table}
                    ORDER BY version DESC, date_installed DESC
                    LIMIT 1
                )
            """
            )

            parameters = {
                "table": self.migration_table_identifier,
            }

            cursor = SqlContent(query).execute(connection, parameters=parameters)
            row = cursor._pum_results[0] if cursor._pum_results else None
            if row is None:
                raise PumSchemaMigrationNoBaselineError(
                    f"Baseline version not found in the {self.migration_table_identifier_str} table."
                )
            return packaging.version.parse(row[0])

    def migration_summary(self, connection: psycopg.Connection) -> dict:
        """Return aggregated migration summary for the configured schema.

        Returns the module name, current version, first installation date,
        latest upgrade date (if different from the installation date),
        beta testing flag, and parameters.

        Args:
            connection: The database connection.

        Returns:
            dict: A dict with keys: schema, module, version, installed_date,
                  upgrade_date (None if never upgraded), beta_testing, parameters.

        Raises:
            PumSchemaMigrationError: If the migration table does not exist or has no data.
        """
        query = psycopg.sql.SQL(
            """
            SELECT
                module,
                version,
                installed_date,
                CASE WHEN upgrade_date > installed_date THEN upgrade_date END AS upgrade_date,
                beta_testing,
                parameters
            FROM (
                SELECT
                    module,
                    (SELECT version FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS version,
                    MIN(date_installed) AS installed_date,
                    MAX(date_installed) AS upgrade_date,
                    (SELECT beta_testing FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS beta_testing,
                    (SELECT parameters FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS parameters
                FROM {table}
                GROUP BY module
            ) sub
            """
        )
        with connection.transaction():
            cursor = SqlContent(query).execute(
                connection, parameters={"table": self.migration_table_identifier}
            )
            row = cursor._pum_results[0] if cursor._pum_results else None
            if row is None:
                raise PumSchemaMigrationError(
                    f"No migration data found in {self.migration_table_identifier_str}."
                )
            return {
                "schema": self.config.config.pum.migration_table_schema,
                "module": row[0],
                "version": row[1],
                "installed_date": row[2],
                "upgrade_date": row[3],
                "beta_testing": row[4],
                "parameters": row[5],
            }

    def migration_details(self, connection: psycopg.Connection, version: str | None = None) -> dict:
        """Return the migration details from the migration table.

        Args:
            connection:
                The database connection to get the migration details.
            version:
                The version of the migration to get details for.
                If None, last migration is returned.

        Returns:
            dict: The migration details.

        Raises:
            PumSchemaMigrationError: If the migration table does not exist or if no migration details are found.
        """
        query = None
        if version is None:
            query = psycopg.sql.SQL(
                """
                SELECT *
                FROM {table}
                WHERE id = (
                        SELECT id
                        FROM {table}
                        ORDER BY version DESC, date_installed DESC
                        LIMIT 1
                    )
                ORDER BY date_installed DESC
            """
            )

            parameters = {
                "table": self.migration_table_identifier,
            }
        else:
            query = psycopg.sql.SQL(
                """
                SELECT *
                FROM {table}
                WHERE version = {version}
            """
            )

            parameters = {
                "table": self.migration_table_identifier,
                "version": psycopg.sql.Literal(version),
            }

        with connection.transaction():
            cursor = SqlContent(query).execute(connection, parameters=parameters)
            row = cursor._pum_results[0] if cursor._pum_results else None
            if row is None:
                raise PumSchemaMigrationError(
                    f"Migration details not found for version {version} in the {self.migration_table_identifier_str} table."
                )
            return dict(zip([desc[0] for desc in cursor._pum_description], row, strict=False))

    def compare(self, connection: psycopg.Connection) -> int:
        """Compare the migrations details in the database to the changelogs in the source.

        Args:
            connection: The database connection to get the baseline version.
        Returns:
            int: -1 if database is behind, 0 if up to date.

        Raises:
            PumSchemaMigrationError: If there is a mismatch between the database and the source.
        """

        with connection.transaction():
            current_version = self.baseline(connection=connection)
            migration_details = self.migration_details(connection=connection)
            changelogs = [str(changelog.version) for changelog in self.config.changelogs()]

            # Check if the current migration version is in the changelogs
            if migration_details["version"] not in changelogs:
                raise PumSchemaMigrationError(
                    f"Changelog for version {migration_details['version']} not found in the source."
                )

            # Check if there are newer changelogs than current version
            for changelog_version in changelogs:
                if packaging.version.parse(changelog_version) > current_version:
                    return -1  # database is behind

            return 0  # database is up to date

__init__

__init__(config: PumConfig) -> None

Initialize the SchemaMigrations class with a database connection and configuration.

Parameters:

Name Type Description Default
config PumConfig

An instance of the PumConfig class containing configuration settings for the PUM system.

required
Source code in pum/schema_migrations.py
def __init__(self, config: PumConfig) -> None:
    """Initialize the SchemaMigrations class with a database connection and configuration.

    Args:
        config (PumConfig): An instance of the PumConfig class containing configuration settings for the PUM system.

    """
    self.config = config
    self.migration_table_identifier = psycopg.sql.SQL(".").join(
        [
            psycopg.sql.Identifier(self.config.config.pum.migration_table_schema),
            psycopg.sql.Identifier(MIGRATION_TABLE_NAME),
        ]
    )
    self.migration_table_identifier_str = (
        f"{self.config.config.pum.migration_table_schema}.{MIGRATION_TABLE_NAME}"
    )

baseline

baseline(connection: Connection) -> packaging.version.Version

Return the baseline version from the migration table.

Parameters:

Name Type Description Default
connection Connection

psycopg.Connection The database connection to get the baseline version.

required

Returns:

Type Description
Version

packaging.version.Version | None: The baseline version.

Raises:

Type Description
PumSchemaMigrationError

If the migration table does not exist or if no baseline version is found

PumSchemaMigrationNoBaselineError

If the migration table does not exist

Source code in pum/schema_migrations.py
def baseline(self, connection: psycopg.Connection) -> packaging.version.Version:
    """Return the baseline version from the migration table.

    Args:
        connection: psycopg.Connection
            The database connection to get the baseline version.

    Returns:
        packaging.version.Version | None: The baseline version.

    Raises:
        PumSchemaMigrationError: If the migration table does not exist or if no baseline version is found
        PumSchemaMigrationNoBaselineError: If the migration table does not exist
    """

    with connection.transaction():
        if not self.exists(connection=connection):
            raise PumSchemaMigrationError(
                f"{self.migration_table_identifier_str} table does not exist."
            )

        query = psycopg.sql.SQL(
            """
            SELECT version
            FROM {table}
            WHERE id = (
                SELECT id
                FROM {table}
                ORDER BY version DESC, date_installed DESC
                LIMIT 1
            )
        """
        )

        parameters = {
            "table": self.migration_table_identifier,
        }

        cursor = SqlContent(query).execute(connection, parameters=parameters)
        row = cursor._pum_results[0] if cursor._pum_results else None
        if row is None:
            raise PumSchemaMigrationNoBaselineError(
                f"Baseline version not found in the {self.migration_table_identifier_str} table."
            )
        return packaging.version.parse(row[0])

compare

compare(connection: Connection) -> int

Compare the migrations details in the database to the changelogs in the source.

Parameters:

Name Type Description Default
connection Connection

The database connection to get the baseline version.

required

Returns: int: -1 if database is behind, 0 if up to date.

Raises:

Type Description
PumSchemaMigrationError

If there is a mismatch between the database and the source.

Source code in pum/schema_migrations.py
def compare(self, connection: psycopg.Connection) -> int:
    """Compare the migrations details in the database to the changelogs in the source.

    Args:
        connection: The database connection to get the baseline version.
    Returns:
        int: -1 if database is behind, 0 if up to date.

    Raises:
        PumSchemaMigrationError: If there is a mismatch between the database and the source.
    """

    with connection.transaction():
        current_version = self.baseline(connection=connection)
        migration_details = self.migration_details(connection=connection)
        changelogs = [str(changelog.version) for changelog in self.config.changelogs()]

        # Check if the current migration version is in the changelogs
        if migration_details["version"] not in changelogs:
            raise PumSchemaMigrationError(
                f"Changelog for version {migration_details['version']} not found in the source."
            )

        # Check if there are newer changelogs than current version
        for changelog_version in changelogs:
            if packaging.version.parse(changelog_version) > current_version:
                return -1  # database is behind

        return 0  # database is up to date

create

create(connection: Connection, *, allow_multiple_modules: bool = False, commit: bool = False) -> None

Create the schema_migrations information table Args: connection: The database connection to create the table. commit: If true, the transaction is committed. The default is false. allow_multiple_modules: If true, allows multiple PUM modules (with separate pum_migrations tables in different schemas) in the same database. Default is false.

Source code in pum/schema_migrations.py
def create(
    self,
    connection: psycopg.Connection,
    *,
    allow_multiple_modules: bool = False,
    commit: bool = False,
) -> None:
    """Create the schema_migrations information table
    Args:
        connection: The database connection to create the table.
        commit: If true, the transaction is committed. The default is false.
        allow_multiple_modules: If true, allows multiple PUM modules (with separate
            pum_migrations tables in different schemas) in the same database. Default is false.
    """
    if self.exists(connection):
        logger.debug(
            f"{self.config.config.pum.migration_table_schema}.pum_migrations table already exists."
        )
        return

    if not allow_multiple_modules:
        schemas = self.exists_in_other_schemas(connection)
        if len(schemas) > 1:
            raise PumSchemaMigrationError(
                f"Another {MIGRATION_TABLE_NAME} table exists in other schemas {', '.join(schemas)}, indicating other PUM modules in this database. "
                "Use allow_multiple_modules=True to allow multiple PUM modules per database."
            )
        elif len(schemas) == 1:
            raise PumSchemaMigrationError(
                f"Another {MIGRATION_TABLE_NAME} table exists in schema {schemas[0]}, indicating another PUM module in this database. "
                "Use allow_multiple_modules=True to allow multiple PUM modules per database."
            )
    # Create the schema if it doesn't exist
    parameters = {
        "version": psycopg.sql.Literal(MIGRATION_TABLE_VERSION),
        "schema": psycopg.sql.Identifier(self.config.config.pum.migration_table_schema),
        "table": self.migration_table_identifier,
    }

    create_schema_query = None
    if self.config.config.pum.migration_table_schema != "public":
        create_schema_query = psycopg.sql.SQL("CREATE SCHEMA IF NOT EXISTS {schema};")

    create_table_query = psycopg.sql.SQL(
        """CREATE TABLE IF NOT EXISTS {table}
        (
        id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
        date_installed timestamp without time zone NOT NULL DEFAULT now(),
        module character varying(50) NOT NULL,
        version character varying(50) NOT NULL,
        beta_testing boolean NOT NULL DEFAULT false,
        changelog_files text[],
        parameters jsonb,
        migration_table_version integer NOT NULL DEFAULT {version}
        );
    """
    )

    comment_query = psycopg.sql.SQL("COMMENT ON TABLE {table} IS 'migration_table_version: 2';")

    if create_schema_query:
        SqlContent(create_schema_query).execute(connection, parameters=parameters)
    SqlContent(create_table_query).execute(connection, parameters=parameters)
    SqlContent(comment_query).execute(connection, parameters=parameters)

    logger.info(f"Created migration table: {self.migration_table_identifier_str}")

    if commit:
        connection.commit()

exists

exists(connection: Connection) -> bool

Check if the schema_migrations information table exists.

Parameters:

Name Type Description Default
connection Connection

The database connection to check for the existence of the table.

required

Returns:

Name Type Description
bool bool

True if the table exists, False otherwise.

Source code in pum/schema_migrations.py
def exists(self, connection: psycopg.Connection) -> bool:
    """Check if the schema_migrations information table exists.

    Args:
        connection: The database connection to check for the existence of the table.

    Returns:
        bool: True if the table exists, False otherwise.

    """
    query = psycopg.sql.SQL(
        """
    SELECT EXISTS (
        SELECT 1
        FROM information_schema.tables
        WHERE table_name = 'pum_migrations' AND table_schema = {schema}
    );
    """
    )

    parameters = {
        "schema": psycopg.sql.Literal(self.config.config.pum.migration_table_schema),
    }

    with connection.transaction():
        cursor = SqlContent(query).execute(connection, parameters=parameters)
        result = cursor._pum_results[0] if cursor._pum_results else None
        return result[0] if result else False

exists_in_other_schemas

exists_in_other_schemas(connection: Connection) -> list[str]

Check if the schema_migrations information table exists in other schemas.

Parameters:

Name Type Description Default
connection Connection

The database connection to check for the existence of the table.

required

Returns:

Type Description
list[str]

List[str]: List of schemas where the table exists.

Source code in pum/schema_migrations.py
def exists_in_other_schemas(self, connection: psycopg.Connection) -> list[str]:
    """Check if the schema_migrations information table exists in other schemas.

    Args:
        connection: The database connection to check for the existence of the table.

    Returns:
        List[str]: List of schemas where the table exists.

    """
    with connection.transaction():
        all_schemas = self.schemas_with_migrations(connection)
        return [
            schema
            for schema in all_schemas
            if schema != self.config.config.pum.migration_table_schema
        ]

has_baseline

has_baseline(connection: Connection) -> bool

Check if the migration table has a baseline version.

Parameters:

Name Type Description Default
connection Connection

The database connection to check for the baseline version.

required

Returns: bool: True if the baseline version exists, False otherwise.

Source code in pum/schema_migrations.py
def has_baseline(self, connection: psycopg.Connection) -> bool:
    """Check if the migration table has a baseline version.

    Args:
        connection: The database connection to check for the baseline version.
    Returns:
        bool: True if the baseline version exists, False otherwise.
    """
    with connection.transaction():
        try:
            self.baseline(connection=connection)
            return True
        except PumSchemaMigrationError:
            return False

migration_details

migration_details(connection: Connection, version: str | None = None) -> dict

Return the migration details from the migration table.

Parameters:

Name Type Description Default
connection Connection

The database connection to get the migration details.

required
version str | None

The version of the migration to get details for. If None, last migration is returned.

None

Returns:

Name Type Description
dict dict

The migration details.

Raises:

Type Description
PumSchemaMigrationError

If the migration table does not exist or if no migration details are found.

Source code in pum/schema_migrations.py
def migration_details(self, connection: psycopg.Connection, version: str | None = None) -> dict:
    """Return the migration details from the migration table.

    Args:
        connection:
            The database connection to get the migration details.
        version:
            The version of the migration to get details for.
            If None, last migration is returned.

    Returns:
        dict: The migration details.

    Raises:
        PumSchemaMigrationError: If the migration table does not exist or if no migration details are found.
    """
    query = None
    if version is None:
        query = psycopg.sql.SQL(
            """
            SELECT *
            FROM {table}
            WHERE id = (
                    SELECT id
                    FROM {table}
                    ORDER BY version DESC, date_installed DESC
                    LIMIT 1
                )
            ORDER BY date_installed DESC
        """
        )

        parameters = {
            "table": self.migration_table_identifier,
        }
    else:
        query = psycopg.sql.SQL(
            """
            SELECT *
            FROM {table}
            WHERE version = {version}
        """
        )

        parameters = {
            "table": self.migration_table_identifier,
            "version": psycopg.sql.Literal(version),
        }

    with connection.transaction():
        cursor = SqlContent(query).execute(connection, parameters=parameters)
        row = cursor._pum_results[0] if cursor._pum_results else None
        if row is None:
            raise PumSchemaMigrationError(
                f"Migration details not found for version {version} in the {self.migration_table_identifier_str} table."
            )
        return dict(zip([desc[0] for desc in cursor._pum_description], row, strict=False))

migration_summary

migration_summary(connection: Connection) -> dict

Return aggregated migration summary for the configured schema.

Returns the module name, current version, first installation date, latest upgrade date (if different from the installation date), beta testing flag, and parameters.

Parameters:

Name Type Description Default
connection Connection

The database connection.

required

Returns:

Name Type Description
dict dict

A dict with keys: schema, module, version, installed_date, upgrade_date (None if never upgraded), beta_testing, parameters.

Raises:

Type Description
PumSchemaMigrationError

If the migration table does not exist or has no data.

Source code in pum/schema_migrations.py
def migration_summary(self, connection: psycopg.Connection) -> dict:
    """Return aggregated migration summary for the configured schema.

    Returns the module name, current version, first installation date,
    latest upgrade date (if different from the installation date),
    beta testing flag, and parameters.

    Args:
        connection: The database connection.

    Returns:
        dict: A dict with keys: schema, module, version, installed_date,
              upgrade_date (None if never upgraded), beta_testing, parameters.

    Raises:
        PumSchemaMigrationError: If the migration table does not exist or has no data.
    """
    query = psycopg.sql.SQL(
        """
        SELECT
            module,
            version,
            installed_date,
            CASE WHEN upgrade_date > installed_date THEN upgrade_date END AS upgrade_date,
            beta_testing,
            parameters
        FROM (
            SELECT
                module,
                (SELECT version FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS version,
                MIN(date_installed) AS installed_date,
                MAX(date_installed) AS upgrade_date,
                (SELECT beta_testing FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS beta_testing,
                (SELECT parameters FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS parameters
            FROM {table}
            GROUP BY module
        ) sub
        """
    )
    with connection.transaction():
        cursor = SqlContent(query).execute(
            connection, parameters={"table": self.migration_table_identifier}
        )
        row = cursor._pum_results[0] if cursor._pum_results else None
        if row is None:
            raise PumSchemaMigrationError(
                f"No migration data found in {self.migration_table_identifier_str}."
            )
        return {
            "schema": self.config.config.pum.migration_table_schema,
            "module": row[0],
            "version": row[1],
            "installed_date": row[2],
            "upgrade_date": row[3],
            "beta_testing": row[4],
            "parameters": row[5],
        }

migration_table_version

migration_table_version(connection: Connection) -> int

Return the migration table version.

Parameters:

Name Type Description Default
connection Connection

The database connection to get the migration table version.

required

Returns:

Type Description
int

int | None: The migration table version, or None if the table does not exist.

Source code in pum/schema_migrations.py
def migration_table_version(self, connection: psycopg.Connection) -> int:
    """Return the migration table version.

    Args:
        connection: The database connection to get the migration table version.

    Returns:
        int | None: The migration table version, or None if the table does not exist.

    """
    query = psycopg.sql.SQL(
        """
        SELECT migration_table_version
        FROM {table}
        ORDER BY migration_table_version DESC
        LIMIT 1;
    """
    )

    parameters = {
        "table": self.migration_table_identifier,
    }

    with connection.transaction():
        cursor = SqlContent(query).execute(connection, parameters=parameters)
        row = cursor._pum_results[0] if cursor._pum_results else None
        if row is None:
            raise PumSchemaMigrationError(
                f"Migration table {self.migration_table_identifier_str} does not exist."
            )
        return row[0]

schemas_with_migration_details staticmethod

schemas_with_migration_details(connection: Connection) -> list[dict]

Return detailed migration info for each schema that has a pum_migrations table.

For each schema, returns the module name, current version, first installation date and latest upgrade date (if different from the installation date).

Parameters:

Name Type Description Default
connection Connection

The database connection.

required

Returns:

Type Description
list[dict]

list[dict]: A list of dicts with keys: schema, module, version, installed_date, upgrade_date (None if never upgraded).

Version Added

1.4.0

Source code in pum/schema_migrations.py
@staticmethod
def schemas_with_migration_details(
    connection: psycopg.Connection,
) -> list[dict]:
    """Return detailed migration info for each schema that has a pum_migrations table.

    For each schema, returns the module name, current version, first installation
    date and latest upgrade date (if different from the installation date).

    Args:
        connection: The database connection.

    Returns:
        list[dict]: A list of dicts with keys: schema, module, version,
                    installed_date, upgrade_date (None if never upgraded).

    Version Added:
        1.4.0
    """
    with connection.transaction():
        schemas = SchemaMigrations.schemas_with_migrations(connection)
        details = []
        for schema in schemas:
            table_id = psycopg.sql.SQL(".").join(
                [psycopg.sql.Identifier(schema), psycopg.sql.Identifier(MIGRATION_TABLE_NAME)]
            )
            query = psycopg.sql.SQL(
                """
                SELECT
                    module,
                    version,
                    installed_date,
                    CASE WHEN upgrade_date > installed_date THEN upgrade_date END AS upgrade_date,
                    beta_testing
                FROM (
                    SELECT
                        module,
                        (SELECT version FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS version,
                        MIN(date_installed) AS installed_date,
                        MAX(date_installed) AS upgrade_date,
                        (SELECT beta_testing FROM {table} ORDER BY version DESC, date_installed DESC LIMIT 1) AS beta_testing
                    FROM {table}
                    GROUP BY module
                ) sub
                """
            )
            try:
                cursor = SqlContent(query).execute(connection, parameters={"table": table_id})
                for row in cursor._pum_results or []:
                    details.append(
                        {
                            "schema": schema,
                            "module": row[0],
                            "version": row[1],
                            "installed_date": row[2],
                            "upgrade_date": row[3],
                            "beta_testing": row[4],
                        }
                    )
            except Exception:
                logger.warning(f"Could not read migration details from schema {schema}")
                details.append(
                    {
                        "schema": schema,
                        "module": None,
                        "version": None,
                        "installed_date": None,
                        "upgrade_date": None,
                        "beta_testing": None,
                    }
                )
        return details

schemas_with_migrations staticmethod

schemas_with_migrations(connection: Connection) -> list[str]

Check if the schema_migrations information table exists in any schemas.

Parameters:

Name Type Description Default
connection Connection

The database connection to check for the existence of the table.

required

Returns:

Type Description
list[str]

List[str]: List of all schemas where the table exists.

Source code in pum/schema_migrations.py
@staticmethod
def schemas_with_migrations(connection: psycopg.Connection) -> list[str]:
    """Check if the schema_migrations information table exists in any schemas.

    Args:
        connection: The database connection to check for the existence of the table.

    Returns:
        List[str]: List of all schemas where the table exists.

    """
    query = psycopg.sql.SQL(
        """
        SELECT table_schema
        FROM information_schema.tables
        WHERE table_name = 'pum_migrations'
    """
    )
    with connection.transaction():
        cursor = SqlContent(query).execute(connection, parameters={})
        return [row[0] for row in (cursor._pum_results or [])]

set_baseline

set_baseline(connection: Connection, version: Version | str, changelog_files: list[str] | None = None, parameters: dict | None = None, *, beta_testing: bool = False, commit: bool = False) -> None

Set the baseline into the migration table.

Parameters:

Name Type Description Default
connection Connection

The database connection to set the baseline version.

required
version Version | str

The version of the current database to set in the information.

required
changelog_files list[str] | None

The list of changelog files that were applied.

None
parameters dict | None

The parameters used in the migration.

None
beta_testing bool

If true, the baseline is set to beta testing mode. The default is false.

False
commit bool

If true, the transaction is committed. The default is False.

False
Source code in pum/schema_migrations.py
    def set_baseline(
        self,
        connection: psycopg.Connection,
        version: packaging.version.Version | str,
        changelog_files: list[str] | None = None,
        parameters: dict | None = None,
        *,
        beta_testing: bool = False,
        commit: bool = False,
    ) -> None:
        """Set the baseline into the migration table.

        Args:
            connection: The database connection to set the baseline version.
            version: The version of the current database to set in the information.
            changelog_files: The list of changelog files that were applied.
            parameters: The parameters used in the migration.
            beta_testing: If true, the baseline is set to beta testing mode. The default is false.
            commit: If true, the transaction is committed. The default is False.

        """
        version_str = version
        version_packaging = version
        if isinstance(version_str, packaging.version.Version):
            version_str = str(version_str)
        if isinstance(version_packaging, str):
            version_packaging = packaging.version.parse(version_packaging)
        pattern = re.compile(r"^\d+\.\d+(\.\d+)?$")
        if not re.match(pattern, version_str):
            raise ValueError(f"Wrong version format: {version}. Must be x.y or x.y.z")

        try:
            current = self.baseline(connection=connection)
        except PumSchemaMigrationNoBaselineError:
            current = None
        if current:
            self.update_migration_table_schema(connection)
        if current and current >= version_packaging:
            raise PumSchemaMigrationError(
                f"Cannot set baseline {version_str} as it is already set at {current}."
            )

        code = psycopg.sql.SQL("""
INSERT INTO {table} (
    module,
    version,
    beta_testing,
    migration_table_version,
    changelog_files,
    parameters
) VALUES (
    {module},
    {version},
    {beta_testing},
    {migration_table_version},
    {changelog_files},
    {parameters}
);""")

        query_parameters = {
            "table": self.migration_table_identifier,
            "module": psycopg.sql.Literal(self.config.config.pum.module),
            "version": psycopg.sql.Literal(version_str),
            "beta_testing": psycopg.sql.Literal(beta_testing),
            "migration_table_version": psycopg.sql.Literal(MIGRATION_TABLE_VERSION),
            "changelog_files": psycopg.sql.Literal(changelog_files or []),
            "parameters": psycopg.sql.Literal(json.dumps(parameters or {})),
        }

        logger.info(
            f"Setting baseline version {version} in {self.migration_table_identifier_str} table"
        )
        SqlContent(code).execute(connection, parameters=query_parameters, commit=commit)

update_migration_table_schema

update_migration_table_schema(connection: Connection) -> None

Update the migration table schema to the latest version.

Parameters:

Name Type Description Default
connection Connection

The database connection to update the table.

required
Source code in pum/schema_migrations.py
def update_migration_table_schema(self, connection: psycopg.Connection) -> None:
    """Update the migration table schema to the latest version.

    Args:
        connection: The database connection to update the table.

    """
    table_version = self.migration_table_version(connection)
    logger.info(
        f"Updating migration table {self.migration_table_identifier_str} from version {table_version} to {MIGRATION_TABLE_VERSION}."
    )
    if table_version == 1:
        alter_query = psycopg.sql.SQL("""
                                      ALTER TABLE {table} ALTER COLUMN migration_table_version ALTER TYPE integer SET DEFAULT {version} USING 1;
                                      ALTER TABLE {table} ALTER COLUMN module SET NOT NULL USING 'tww';
                                      """)
        parameters = {
            "table": self.migration_table_identifier,
            "version": psycopg.sql.Literal(MIGRATION_TABLE_VERSION),
        }
        SqlContent(alter_query).execute(connection, parameters=parameters)