Skip to content

Commit

Permalink
MID-8842 database upgrade step can now upgrade db schema, added custo…
Browse files Browse the repository at this point in the history
…m statement delimiters to upgrade scripts
  • Loading branch information
1azyman committed May 30, 2023
1 parent 953e8d6 commit 118eae4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
8 changes: 8 additions & 0 deletions config/sql/native-new/postgres-new-upgrade-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Perhaps you have separate audit database?', current_database(), current_schema()
END
$$;

;; -- sql statements delimiter, used by ninja

-- SCHEMA-COMMIT 4.4: commit 69e8c29b

-- changes for 4.4.1
Expand Down Expand Up @@ -91,6 +93,8 @@ BEGIN
END $$;
$aac$);

;;

-- SCHEMA-COMMIT 4.4.1: commit de18c14f

-- changes for 4.5
Expand All @@ -100,6 +104,8 @@ call apply_audit_change(2, $aa$
ALTER TYPE ObjectType ADD VALUE IF NOT EXISTS 'MESSAGE_TEMPLATE' AFTER 'LOOKUP_TABLE';
$aa$);

;;

-- SCHEMA-COMMIT 4.6: commit 71f2df50

-- changes for 4.7
Expand All @@ -109,6 +115,8 @@ call apply_audit_change(3, $aa$
ALTER TYPE ObjectType ADD VALUE IF NOT EXISTS 'MARK' AFTER 'LOOKUP_TABLE';
$aa$);

;;

-- WRITE CHANGES ABOVE ^^
-- IMPORTANT: update apply_audit_change number at the end of postgres-new-audit.sql
-- to match the number used in the last change here!
32 changes: 32 additions & 0 deletions config/sql/native-new/postgres-new-upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Current database name is ''%'', schema name is ''%''.', current_database(), curr
END
$$;

;; -- sql statements delimiter, used by ninja

-- SCHEMA-COMMIT 4.4: commit 20ad200b
-- see: https://github.com/Evolveum/midpoint/blob/20ad200bd10a114fd70d2d131c0d11b5cd920150/config/sql/native-new/postgres-new.sql

Expand Down Expand Up @@ -59,6 +61,8 @@ CREATE TRIGGER m_org_mark_refresh_trunc_tr
FOR EACH STATEMENT EXECUTE FUNCTION mark_org_closure_for_refresh_org();
$aa$);

;;

-- SCHEMA-COMMIT 4.4.1: commit de18c14f

-- changes for 4.5
Expand All @@ -69,6 +73,8 @@ call apply_change(2, $aa$
ALTER TYPE ObjectType ADD VALUE IF NOT EXISTS 'MESSAGE_TEMPLATE' AFTER 'LOOKUP_TABLE';
$aa$);

;;

call apply_change(3, $aa$
CREATE TABLE m_message_template (
oid UUID NOT NULL PRIMARY KEY REFERENCES m_object_oid(oid),
Expand All @@ -92,12 +98,16 @@ CREATE INDEX m_message_template_createTimestamp_idx ON m_message_template (creat
CREATE INDEX m_message_template_modifyTimestamp_idx ON m_message_template (modifyTimestamp);
$aa$);

;;

-- MID-7487 Identity matching
-- We add the new enum value in separate change, because it must be committed before it is used.
call apply_change(4, $aa$
CREATE TYPE CorrelationSituationType AS ENUM ('UNCERTAIN', 'EXISTING_OWNER', 'NO_OWNER', 'ERROR');
$aa$);

;;

call apply_change(5, $aa$
ALTER TABLE m_shadow
ADD COLUMN correlationStartTimestamp TIMESTAMPTZ,
Expand All @@ -112,6 +122,8 @@ CREATE INDEX m_shadow_correlationCaseOpenTimestamp_idx ON m_shadow (correlationC
CREATE INDEX m_shadow_correlationCaseCloseTimestamp_idx ON m_shadow (correlationCaseCloseTimestamp);
$aa$);

;;

-- SCHEMA-COMMIT 4.5: commit c5f19c9e

-- changes for 4.6
Expand All @@ -122,18 +134,24 @@ call apply_change(6, $aa$
CREATE TYPE AdministrativeAvailabilityStatusType AS ENUM ('MAINTENANCE', 'OPERATIONAL');
$aa$);

;;

call apply_change(7, $aa$
ALTER TABLE m_resource
ADD COLUMN administrativeOperationalStateAdministrativeAvailabilityStatus AdministrativeAvailabilityStatusType;
$aa$);

;;

-- smart correlation
call apply_change(8, $aa$
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch; -- fuzzy string match (levenshtein, etc.)

ALTER TYPE ContainerType ADD VALUE IF NOT EXISTS 'FOCUS_IDENTITY' AFTER 'CASE_WORK_ITEM';
$aa$);

;;

call apply_change(9, $aa$
CREATE TABLE m_focus_identity (
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
Expand All @@ -152,16 +170,22 @@ ALTER TABLE m_focus ADD normalizedData JSONB;
CREATE INDEX m_focus_normalizedData_idx ON m_focus USING gin(normalizedData);
$aa$);

;;

-- resource templates
call apply_change(10, $aa$
ALTER TABLE m_resource ADD template BOOLEAN;
$aa$);

;;

-- MID-8053: "Active" connectors detection
call apply_change(11, $aa$
ALTER TABLE m_connector ADD available BOOLEAN;
$aa$);

;;

-- SCHEMA-COMMIT 4.5: commit c5f19c9e

-- No changes for audit schema in 4.6
Expand All @@ -178,6 +202,8 @@ ALTER TYPE ObjectType ADD VALUE IF NOT EXISTS 'SIMULATION_RESULT' AFTER 'SHADOW'
ALTER TYPE ContainerType ADD VALUE IF NOT EXISTS 'SIMULATION_RESULT_PROCESSED_OBJECT' AFTER 'OPERATION_EXECUTION';
$aa$);

;;

-- Simulations, tables
call apply_change(13, $aa$
CREATE TABLE m_simulation_result (
Expand Down Expand Up @@ -310,6 +336,8 @@ CREATE INDEX m_ref_object_effective_mark_targetOidRelationId_idx
ON m_ref_object_effective_mark (targetOid, relationId);
$aa$);

;;

-- Minor index name fixes
call apply_change(14, $aa$
ALTER INDEX m_ref_object_create_approverTargetOidRelationId_idx
Expand All @@ -318,11 +346,15 @@ ALTER INDEX m_ref_object_modify_approverTargetOidRelationId_idx
RENAME TO m_ref_object_modify_approver_targetOidRelationId_idx;
$aa$);

;;

-- Making resource.abstract queryable
call apply_change(15, $aa$
ALTER TABLE m_resource ADD abstract BOOLEAN;
$aa$);

;;

-- WRITE CHANGES ABOVE ^^
-- IMPORTANT: update apply_change number at the end of postgres-new.sql
-- to match the number used in the last change here!
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
package com.evolveum.midpoint.ninja.action.upgrade.step;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;

import com.evolveum.midpoint.init.AuditServiceProxy;

import com.evolveum.midpoint.repo.sqale.SqaleRepoContext;
import com.evolveum.midpoint.repo.sqale.audit.SqaleAuditService;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import com.evolveum.midpoint.init.AuditServiceProxy;
import com.evolveum.midpoint.ninja.action.upgrade.StepResult;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeStep;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeStepsContext;
import com.evolveum.midpoint.repo.sqale.SqaleRepoContext;
import com.evolveum.midpoint.repo.sqale.audit.SqaleAuditService;

public class DatabaseSchemaStep implements UpgradeStep<StepResult> {

Expand Down Expand Up @@ -73,12 +74,25 @@ public StepResult execute() throws Exception {
};
}

private void executeUpgradeScript(File upgradeScript, DataSource dataSource) {
private void executeUpgradeScript(File upgradeScript, DataSource dataSource) throws SQLException {
FileSystemResourceLoader loader = new FileSystemResourceLoader();
Resource script = loader.getResource("file:" + upgradeScript.getAbsolutePath());

ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(script);
populator.execute(dataSource);
try (Connection connection = dataSource.getConnection()) {
boolean autocommit = connection.getAutoCommit();
connection.setAutoCommit(true);

try {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setSeparator(";;");
populator.setSqlScriptEncoding(StandardCharsets.UTF_8.name());
populator.addScript(script);
populator.populate(connection);
} finally {
connection.setAutoCommit(autocommit);
}
}
}
}

//1685390031006-midpoint-latest-dist.zip

0 comments on commit 118eae4

Please sign in to comment.