-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a primary key to all tables that lack one
For logical replication, Postgres requires that all tables have a primary key. This commit adds primary keys to tables like the databasechangelog and join tables. It also adds the "pg_stat_statements" library to our database container so that we can monitor queries in development. "A published table must have a 'replica identity' configured in order to be able to replicate UPDATE and DELETE operations, so that appropriate rows to update or delete can be identified on the subscriber side. By default, this is the primary key, if there is one." From https://www.postgresql.org/docs/12/logical-replication-publication.html Also alter testcontainers to make the rhsm-subscriptions user a superuser so we can create extensions in those containers as well.
- Loading branch information
Showing
7 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
hba_file = '/pg_hba.conf' | ||
listen_addresses = '*' | ||
shared_preload_libraries = 'pg_stat_statements' |
72 changes: 72 additions & 0 deletions
72
src/main/resources/liquibase/202412041537-primary-keys-on-all-tables.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd"> | ||
<!-- | ||
These changeSets are only for PostgreSQL because the primary keys we're adding are just to satisfy | ||
PostgreSQL's requirements for logical replication. See | ||
https://www.postgresql.org/docs/12/logical-replication-publication.html | ||
Consequently, these keys aren't mapped in JPA so we don't need to worry about them during | ||
in-memory DB tests. | ||
--> | ||
<changeSet id="202412041537-01" author="awood" dbms="postgresql"> | ||
<!-- | ||
Creating an extension requires superuser permissions. But in production and stage, we don't | ||
have those permissions. We have to create the extension in an out-of-band migration. | ||
The creation command checks permissions before it checks the extensions existence, so even an | ||
invocation that would have no effect will cause the changeset to fail. Accordingly, we have to | ||
have a pre-condition to check if we should even attempt to create the extension. That way we | ||
can use the same changeset in both development and production. | ||
--> | ||
<preConditions onFail="MARK_RAN"> | ||
<sqlCheck expectedResult="0"> | ||
SELECT count(1) FROM pg_extension WHERE extname = 'uuid-ossp'; | ||
</sqlCheck> | ||
</preConditions> | ||
|
||
<sql> | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
</sql> | ||
<!-- | ||
I am intentionally not providing a rollback because we have two change logs: this one and | ||
the one for swatch-contracts. Rolling back the extension in one changelog could adversely | ||
affect the operations of the other changelog. | ||
--> | ||
</changeSet> | ||
|
||
<changeSet id="202412041537-02" author="awood" dbms="postgresql"> | ||
<addColumn tableName="databasechangelog"> | ||
<column name="uuid" type="uuid" defaultValueComputed="uuid_generate_v4()"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</addColumn> | ||
</changeSet> | ||
|
||
<changeSet id="202412041537-03" author="awood" dbms="postgresql"> | ||
<addColumn tableName="sku_child_sku"> | ||
<column name="id" type="uuid" defaultValueComputed="uuid_generate_v4()"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</addColumn> | ||
</changeSet> | ||
|
||
<changeSet id="202412041537-04" author="awood" dbms="postgresql"> | ||
<addColumn tableName="sku_oid"> | ||
<column name="id" type="uuid" defaultValueComputed="uuid_generate_v4()"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</addColumn> | ||
</changeSet> | ||
|
||
<changeSet id="202412041537-05" author="awood" dbms="postgresql"> | ||
<addColumn tableName="sku_product_tag"> | ||
<column name="id" type="uuid" defaultValueComputed="uuid_generate_v4()"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</addColumn> | ||
</changeSet> | ||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
swatch-contracts/src/main/resources/db/202412051144-primary-keys-on-changelog-table.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd"> | ||
<!-- | ||
These changeSets are only for PostgreSQL because the primary keys we're adding are just to satisfy | ||
PostgreSQL's requirements for logical replication. See | ||
https://www.postgresql.org/docs/12/logical-replication-publication.html | ||
Consequently, these keys aren't mapped in JPA so we don't need to worry about them during | ||
in-memory DB tests. | ||
--> | ||
<changeSet id="202412051144-01" author="awood" dbms="postgresql"> | ||
<!-- | ||
Creating an extension requires superuser permissions. But in production and stage, we don't | ||
have those permissions. We have to create the extension in an out-of-band migration. | ||
The creation command checks permissions before it checks the extensions existence, so even an | ||
invocation that would have no effect will cause the changeset to fail. Accordingly, we have to | ||
have a pre-condition to check if we should even attempt to create the extension. That way we | ||
can use the same changeset in both development and production. | ||
--> | ||
<preConditions onFail="MARK_RAN"> | ||
<sqlCheck expectedResult="0"> | ||
SELECT count(1) FROM pg_extension WHERE extname = 'uuid-ossp'; | ||
</sqlCheck> | ||
</preConditions> | ||
<sql> | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
</sql> | ||
<!-- | ||
I am intentionally not providing a rollback because we have two change logs: this one and | ||
the one for swatch-contracts. Rolling back the extension in one changelog could adversely | ||
affect the operations of the other changelog. | ||
--> | ||
</changeSet> | ||
|
||
<changeSet id="202412051144-02" author="awood" dbms="postgresql"> | ||
<addColumn tableName="databasechangelog_swatch_contracts"> | ||
<column name="uuid" type="uuid" defaultValueComputed="uuid_generate_v4()"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</addColumn> | ||
</changeSet> | ||
|
||
<changeSet id="202412051144-03" author="awood" dbms="postgresql"> | ||
<addColumn tableName="contract_metrics"> | ||
<column name="id" type="uuid" defaultValueComputed="uuid_generate_v4()"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</addColumn> | ||
</changeSet> | ||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters