Skip to content

Commit

Permalink
feat: add migrations for the policy-monitor sql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Dec 20, 2023
1 parent c431a9c commit d016a5d
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ spec:
- name: "EDC_DATASOURCE_EDR_URL"
value: {{ tpl .Values.postgresql.jdbcUrl . | quote }}

# see extension https://github.com/eclipse-edc/Connector/tree/main/extensions/policy-monitor/store/sql/policy-monitor-store-sql
- name: "EDC_DATASOURCE_POLICY-MONITOR_NAME"
value: "policy-monitor"
- name: "EDC_DATASOURCE_POLICY-MONITOR_USER"
value: {{ .Values.postgresql.auth.username | required ".Values.postgresql.auth.username is required" | quote }}
- name: "EDC_DATASOURCE_POLICY-MONITOR_PASSWORD"
value: {{ .Values.postgresql.auth.password | required ".Values.postgresql.auth.password is required" | quote }}
- name: "EDC_DATASOURCE_POLICY-MONITOR_URL"
value: {{ tpl .Values.postgresql.jdbcUrl . | quote }}

# see extension https://github.com/eclipse-tractusx/tractusx-edc/tree/main/edc-extensions/bpn-validation/business-partner-store-sql
- name: "EDC_DATASOURCE_BPN_NAME"
value: "bpn"
Expand Down
10 changes: 10 additions & 0 deletions charts/tractusx-connector/templates/deployment-controlplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ spec:
- name: "EDC_DATASOURCE_EDR_URL"
value: {{ tpl .Values.postgresql.jdbcUrl . | quote }}

# see extension https://github.com/eclipse-edc/Connector/tree/main/extensions/policy-monitor/store/sql/policy-monitor-store-sql
- name: "EDC_DATASOURCE_POLICY-MONITOR_NAME"
value: "policy-monitor"
- name: "EDC_DATASOURCE_POLICY-MONITOR_USER"
value: {{ .Values.postgresql.auth.username | required ".Values.postgresql.auth.username is required" | quote }}
- name: "EDC_DATASOURCE_POLICY-MONITOR_PASSWORD"
value: {{ .Values.postgresql.auth.password | required ".Values.postgresql.auth.password is required" | quote }}
- name: "EDC_DATASOURCE_POLICY-MONITOR_URL"
value: {{ tpl .Values.postgresql.jdbcUrl . | quote }}

# see extension https://github.com/eclipse-tractusx/tractusx-edc/tree/main/edc-extensions/bpn-validation/business-partner-store-sql
- name: "EDC_DATASOURCE_BPN_NAME"
value: "bpn"
Expand Down
15 changes: 9 additions & 6 deletions edc-extensions/postgresql-migration/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Postgresql SQL Migration Extension

This extension applies SQL migrations to
This extension applies SQL migrations to these stores:

* the asset-index
* the contract-definition store
* contract-negotiation store
* policy store
* transfer-process store
* asset-index
* contract-definition
* contract-negotiation
* edr
* policy
* policy-monitor
* transfer-process

## Configuration

Expand All @@ -17,5 +19,6 @@ This extension applies SQL migrations to
| org.eclipse.tractusx.edc.postgresql.migration.contractnegotiation.enabled | Enable migration for contract negotiation tables | | true |
| org.eclipse.tractusx.edc.postgresql.migration.edr.enabled | Enable migration for edr tables | | true |
| org.eclipse.tractusx.edc.postgresql.migration.policy.enabled | Enable migration for policy tables | | true |
| org.eclipse.tractusx.edc.postgresql.migration.policy-monitor.enabled | Enable migration for policy monitor tables | | true |
| org.eclipse.tractusx.edc.postgresql.migration.transferprocess.enabled | Enable migration for transfer process tables | | true |
| org.eclipse.tractusx.edc.postgresql.migration.schema | The DB schema to be used during migration | | "public" |
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,79 @@

package org.eclipse.tractusx.edc.postgresql.migration;

import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.sql.datasource.ConnectionFactoryDataSource;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.output.MigrateResult;

import java.util.Objects;
import java.util.Properties;

abstract class AbstractPostgresqlMigrationExtension implements ServiceExtension {

private static final String EDC_DATASOURCE_PREFIX = "edc.datasource";
private static final String MIGRATION_LOCATION_BASE =
String.format("classpath:%s", AbstractPostgresqlMigrationExtension.class.getPackageName().replace(".", "/"));

protected abstract String getDataSourceNameConfigurationKey();
private static final String DEFAULT_MIGRATION_ENABLED_TEMPLATE = "true";
@Setting(value = "Enable/disables subsystem schema migration", defaultValue = DEFAULT_MIGRATION_ENABLED_TEMPLATE, type = "boolean")
private static final String MIGRATION_ENABLED_TEMPLATE = "org.eclipse.tractusx.edc.postgresql.migration.%s.enabled";

private static final String DEFAULT_MIGRATION_SCHEMA = "public";
@Setting(value = "Schema used for the migration", defaultValue = DEFAULT_MIGRATION_SCHEMA)
private static final String MIGRATION_SCHEMA = "org.eclipse.tractusx.edc.postgresql.migration.schema";

@Override
public String name() {
return "Postgresql schema migration for subsystem " + getSubsystemName();
}

protected abstract String getSubsystemName();

@Override
public void initialize(final ServiceExtensionContext context) {
final String subSystemName = Objects.requireNonNull(getSubsystemName());
var config = context.getConfig();

var subSystemName = Objects.requireNonNull(getSubsystemName());
var enabled = config.getBoolean(MIGRATION_ENABLED_TEMPLATE.formatted(subSystemName), Boolean.valueOf(DEFAULT_MIGRATION_ENABLED_TEMPLATE));

final String dataSourceName =
context.getConfig().getString(getDataSourceNameConfigurationKey(), null);
if (dataSourceName == null) {
if (!enabled) {
return;
}

boolean enabled = context.getConfig()
.getBoolean(String.format("org.eclipse.tractusx.edc.postgresql.migration.%s.enabled", subSystemName), true);
String schema = context.getConfig()
.getString("org.eclipse.tractusx.edc.postgresql.migration.schema", "public");
var configGroup = "%s.%s".formatted(EDC_DATASOURCE_PREFIX, subSystemName);
var datasourceConfig = config.getConfig(configGroup);

if (!enabled) {
var dataSourceName = datasourceConfig.getString("name", null);
if (dataSourceName == null) {
context.getMonitor().warning("No 'name' setting in group %s found, no schema migrations will run for subsystem %s"
.formatted(configGroup, subSystemName));
return;
}

Config datasourceConfiguration = context.getConfig(String.join(".", EDC_DATASOURCE_PREFIX, dataSourceName));

final String jdbcUrl = Objects.requireNonNull(datasourceConfiguration.getString("url"));
final Properties jdbcProperties = new Properties();
jdbcProperties.putAll(datasourceConfiguration.getRelativeEntries());

final DriverManagerConnectionFactory driverManagerConnectionFactory =
new DriverManagerConnectionFactory(jdbcUrl, jdbcProperties);
final ConnectionFactoryDataSource dataSource =
new ConnectionFactoryDataSource(driverManagerConnectionFactory);
var jdbcUrl = datasourceConfig.getString("url");
var jdbcProperties = new Properties();
jdbcProperties.putAll(datasourceConfig.getRelativeEntries());

final String schemaHistoryTableName = getSchemaHistoryTableName(subSystemName);
final String migrationsLocation = getMigrationsLocation();
var driverManagerConnectionFactory = new DriverManagerConnectionFactory(jdbcUrl, jdbcProperties);
var dataSource = new ConnectionFactoryDataSource(driverManagerConnectionFactory);

final Flyway flyway =
var flyway =
Flyway.configure()
.baselineVersion(MigrationVersion.fromVersion("0.0.0"))
.failOnMissingLocations(true)
.dataSource(dataSource)
.table(schemaHistoryTableName)
.locations(migrationsLocation)
.defaultSchema(schema)
.table("flyway_schema_history_%s".formatted(subSystemName))
.locations("%s/%s".formatted(MIGRATION_LOCATION_BASE, subSystemName))
.defaultSchema(config.getString(MIGRATION_SCHEMA, DEFAULT_MIGRATION_SCHEMA))
.load();

flyway.baseline();

final MigrateResult migrateResult = flyway.migrate();
var migrateResult = flyway.migrate();

if (!migrateResult.success) {
throw new EdcPersistenceException(
Expand All @@ -96,11 +102,4 @@ public void initialize(final ServiceExtensionContext context) {
}
}

private String getMigrationsLocation() {
return String.join("/", MIGRATION_LOCATION_BASE, getSubsystemName());
}

private String getSchemaHistoryTableName(final String subSystemName) {
return String.format("flyway_schema_history_%s", subSystemName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@

package org.eclipse.tractusx.edc.postgresql.migration;

import org.eclipse.edc.connector.store.sql.assetindex.ConfigurationKeys;

public class AssetPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "asset";

protected String getDataSourceNameConfigurationKey() {
return ConfigurationKeys.DATASOURCE_SETTING_NAME;
}

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@

package org.eclipse.tractusx.edc.postgresql.migration;

import org.eclipse.edc.connector.store.sql.assetindex.ConfigurationKeys;

public class BusinessGroupPostgresMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME = "businessgroup";


@Override
protected String getDataSourceNameConfigurationKey() {
return ConfigurationKeys.DATASOURCE_SETTING_NAME;
}
private static final String NAME = "bpn";

@Override
protected String getSubsystemName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
public class ContractDefinitionPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "contractdefinition";

private static final String DATASOURCE_SETTING_NAME = "edc.datasource.contractdefinition.name";

protected String getDataSourceNameConfigurationKey() {
return DATASOURCE_SETTING_NAME;
}

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
public class ContractNegotiationPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "contractnegotiation";

private static final String DATASOURCE_SETTING_NAME = "edc.datasource.contractnegotiation.name";

protected String getDataSourceNameConfigurationKey() {
return DATASOURCE_SETTING_NAME;
}

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
import java.util.Objects;
import java.util.Properties;

/**
* From the EDC version after 0.4.1 this connection factory will be provided as injectable service, so this duplicate
* can be removed on the next version update.
*
* @deprecated will be removed on the next version
*/
@Deprecated(since = "0.6.0")
class DriverManagerConnectionFactory implements ConnectionFactory {
private final String jdbcUrl;
private final Properties properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
public class EdrPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "edr";

private static final String DATASOURCE_SETTING_NAME = "edc.datasource.edr.name";

protected String getDataSourceNameConfigurationKey() {
return DATASOURCE_SETTING_NAME;
}

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.tractusx.edc.postgresql.migration;

public class PolicyMonitorPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "policy-monitor";

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
public class PolicyPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "policy";

private static final String DATASOURCE_SETTING_NAME = "edc.datasource.policy.name";

protected String getDataSourceNameConfigurationKey() {
return DATASOURCE_SETTING_NAME;
}

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
public class TransferProcessPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension {
private static final String NAME_SUBSYSTEM = "transferprocess";

private static final String DATASOURCE_SETTING_NAME = "edc.datasource.transferprocess.name";

protected String getDataSourceNameConfigurationKey() {
return DATASOURCE_SETTING_NAME;
}

protected String getSubsystemName() {
return NAME_SUBSYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ org.eclipse.tractusx.edc.postgresql.migration.AssetPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.ContractDefinitionPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.ContractNegotiationPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.PolicyPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.PolicyMonitorPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.TransferProcessPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.EdrPostgresqlMigrationExtension
org.eclipse.tractusx.edc.postgresql.migration.BusinessGroupPostgresMigrationExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--
-- Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
--
-- This program and the accompanying materials are made available under the
-- terms of the Apache License, Version 2.0 which is available at
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- SPDX-License-Identifier: Apache-2.0
--
-- Contributors:
-- Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
--

-- Statements are designed for and tested with Postgres only!


CREATE TABLE IF NOT EXISTS edc_lease
(
leased_by VARCHAR NOT NULL,
leased_at BIGINT,
lease_duration INTEGER NOT NULL,
lease_id VARCHAR NOT NULL
CONSTRAINT lease_pk
PRIMARY KEY
);

COMMENT ON COLUMN edc_lease.leased_at IS 'posix timestamp of lease';
COMMENT ON COLUMN edc_lease.lease_duration IS 'duration of lease in milliseconds';

CREATE TABLE IF NOT EXISTS edc_policy_monitor
(
entry_id VARCHAR NOT NULL PRIMARY KEY,
state INTEGER NOT NULL ,
created_at BIGINT NOT NULL ,
updated_at BIGINT NOT NULL ,
state_count INTEGER DEFAULT 0 NOT NULL,
state_time_stamp BIGINT,
trace_context JSON,
error_detail VARCHAR,
lease_id VARCHAR
CONSTRAINT policy_monitor_lease_lease_id_fk
REFERENCES edc_lease
ON DELETE SET NULL,
properties JSON,
contract_id VARCHAR
);
Loading

0 comments on commit d016a5d

Please sign in to comment.