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 8c48a17
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 113 deletions.
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,73 @@

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";

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.getBoolean(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 datasourceConfiguration = config.getConfig("%s.%s".formatted(EDC_DATASOURCE_PREFIX, subSystemName));

if (!enabled) {
var dataSourceName = config.getString(datasourceConfiguration + ".name", null);
if (dataSourceName == null) {
context.getMonitor().warning("No name for datasource %s setting found, no schema migrations will run for subsystem %s"
.formatted(datasourceConfiguration, 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();
var jdbcUrl = Objects.requireNonNull(datasourceConfiguration.getString("url"));
var jdbcProperties = new Properties();
jdbcProperties.putAll(datasourceConfiguration.getRelativeEntries());

final DriverManagerConnectionFactory driverManagerConnectionFactory =
new DriverManagerConnectionFactory(jdbcUrl, jdbcProperties);
final ConnectionFactoryDataSource dataSource =
new ConnectionFactoryDataSource(driverManagerConnectionFactory);

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 +96,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,17 +14,9 @@

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;
}

@Override
protected String getSubsystemName() {
return NAME;
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,29 @@
/*
* Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
* Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

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
);
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static java.lang.String.format;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.DB_SCHEMA_NAME;
Expand Down Expand Up @@ -80,34 +81,14 @@ public Map<String, String> postgresqlConfiguration(String name) {
var jdbcUrl = jdbcUrl(name);
return new HashMap<>() {
{
put("edc.datasource.asset.name", "asset");
put("edc.datasource.asset.url", jdbcUrl);
put("edc.datasource.asset.user", USER);
put("edc.datasource.asset.password", PASSWORD);
put("edc.datasource.contractdefinition.name", "contractdefinition");
put("edc.datasource.contractdefinition.url", jdbcUrl);
put("edc.datasource.contractdefinition.user", USER);
put("edc.datasource.contractdefinition.password", PASSWORD);
put("edc.datasource.contractnegotiation.name", "contractnegotiation");
put("edc.datasource.contractnegotiation.url", jdbcUrl);
put("edc.datasource.contractnegotiation.user", USER);
put("edc.datasource.contractnegotiation.password", PASSWORD);
put("edc.datasource.policy.name", "policy");
put("edc.datasource.policy.url", jdbcUrl);
put("edc.datasource.policy.user", USER);
put("edc.datasource.policy.password", PASSWORD);
put("edc.datasource.transferprocess.name", "transferprocess");
put("edc.datasource.transferprocess.url", jdbcUrl);
put("edc.datasource.transferprocess.user", USER);
put("edc.datasource.transferprocess.password", PASSWORD);
put("edc.datasource.edr.name", "edr");
put("edc.datasource.edr.url", jdbcUrl);
put("edc.datasource.edr.user", USER);
put("edc.datasource.edr.password", PASSWORD);
put("edc.datasource.bpn.name", "bpn");
put("edc.datasource.bpn.url", jdbcUrl);
put("edc.datasource.bpn.user", USER);
put("edc.datasource.bpn.password", PASSWORD);
Stream.of("asset", "contractdefinition", "contractnegotiation", "policy", "transferprocess", "edr", "bpn", "policy-monitor")
.forEach(context -> {
var group = "edc.datasource." + context;
put(group + ".name", context);
put(group + ".url", jdbcUrl);
put(group + ".user", USER);
put(group + ".password", PASSWORD);
});
// use non-default schema name to test usage of non-default schema
put("org.eclipse.tractusx.edc.postgresql.migration.schema", DB_SCHEMA_NAME);
}
Expand Down

0 comments on commit 8c48a17

Please sign in to comment.