-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1024 from Think-iT-Labs/1003-fix-asset-properties…
…-transformation fix: migrate asset json properties from array to map
- Loading branch information
Showing
7 changed files
with
177 additions
and
67 deletions.
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
45 changes: 45 additions & 0 deletions
45
...-migration/src/main/java/org/eclipse/tractusx/edc/postgresql/migration/FlywayManager.java
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,45 @@ | ||
/* | ||
* Copyright (c) 2024 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; | ||
|
||
import org.flywaydb.core.Flyway; | ||
import org.flywaydb.core.api.MigrationVersion; | ||
import org.flywaydb.core.api.output.MigrateResult; | ||
|
||
import javax.sql.DataSource; | ||
|
||
|
||
public class FlywayManager { | ||
|
||
private static final String MIGRATION_LOCATION_BASE = | ||
String.format("classpath:%s", FlywayManager.class.getPackageName().replace(".", "/")); | ||
|
||
public static MigrateResult migrate(DataSource dataSource, String subSystemName, String defaultSchema, MigrationVersion target) { | ||
var flyway = | ||
Flyway.configure() | ||
.baselineVersion(MigrationVersion.fromVersion("0.0.0")) | ||
.failOnMissingLocations(true) | ||
.dataSource(dataSource) | ||
.table("flyway_schema_history_%s".formatted(subSystemName)) | ||
.locations("%s/%s".formatted(MIGRATION_LOCATION_BASE, subSystemName)) | ||
.defaultSchema(defaultSchema) | ||
.target(target) | ||
.load(); | ||
|
||
flyway.baseline(); | ||
|
||
return flyway.migrate(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...lipse/tractusx/edc/postgresql/migration/asset/V0_0_6__Transform_Property_Array_To_Map.sql
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,16 @@ | ||
-- | ||
-- Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft | ||
-- | ||
-- 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 - Migration into one table | ||
-- | ||
|
||
UPDATE edc_asset SET | ||
properties = (select json_object_agg(json_array_elements->>'property_name', json_array_elements->>'property_value') from json_array_elements(properties) as json_array_elements), | ||
private_properties = (select json_object_agg(json_array_elements->>'property_name', json_array_elements->>'property_value') from json_array_elements(private_properties) as json_array_elements) |
78 changes: 78 additions & 0 deletions
78
.../org/eclipse/tractusx/edc/postgresql/migration/AssetPostgresqlMigrationExtensionTest.java
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,78 @@ | ||
/* | ||
* Copyright (c) 2024 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; | ||
|
||
import org.eclipse.edc.connector.store.sql.assetindex.SqlAssetIndex; | ||
import org.eclipse.edc.connector.store.sql.assetindex.schema.postgres.PostgresDialectStatements; | ||
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest; | ||
import org.eclipse.edc.spi.types.TypeManager; | ||
import org.eclipse.edc.spi.types.domain.asset.Asset; | ||
import org.eclipse.edc.sql.QueryExecutor; | ||
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension; | ||
import org.flywaydb.core.api.MigrationVersion; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@PostgresqlDbIntegrationTest | ||
@ExtendWith(PostgresqlStoreSetupExtension.class) | ||
class AssetPostgresqlMigrationExtensionTest { | ||
|
||
private SqlAssetIndex store; | ||
|
||
@BeforeEach | ||
void setUp(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) { | ||
store = new SqlAssetIndex(extension.getDataSourceRegistry(), extension.getDatasourceName(), | ||
extension.getTransactionContext(), new TypeManager().getMapper(), new PostgresDialectStatements(), | ||
queryExecutor); | ||
} | ||
|
||
@Test // bugfix https://github.com/eclipse-tractusx/tractusx-edc/issues/1003 | ||
void version006shouldTransformPropertiesListToMap(PostgresqlStoreSetupExtension extension) { | ||
var dataSource = extension.getDataSourceRegistry().resolve(extension.getDatasourceName()); | ||
FlywayManager.migrate(dataSource, "asset", "public", MigrationVersion.fromVersion("0.0.5")); | ||
|
||
insertAsset(extension, "1"); | ||
insertAsset(extension, "2"); | ||
|
||
FlywayManager.migrate(dataSource, "asset", "public", MigrationVersion.fromVersion("0.0.6")); | ||
|
||
var result = store.findById("1"); | ||
|
||
assertThat(result).isNotNull(); | ||
assertThat(result.getProperties()).containsExactlyInAnyOrderEntriesOf( | ||
Map.of(Asset.PROPERTY_ID, "1", "key", "value1", "anotherKey", "anotherValue1")); | ||
assertThat(result.getPrivateProperties()).containsExactlyInAnyOrderEntriesOf( | ||
Map.of("privateKey", "privateValue1", "anotherPrivateKey", "anotherPrivateValue1")); | ||
} | ||
|
||
private void insertAsset(PostgresqlStoreSetupExtension extension, String id) { | ||
var propertiesArray = "[ %s, %s ]".formatted(propertyJsonMap("key", "value" + id), propertyJsonMap("anotherKey", "anotherValue" + id)); | ||
var privatePropertiesArray = "[ %s, %s ]".formatted(propertyJsonMap("privateKey", "privateValue" + id), propertyJsonMap("anotherPrivateKey", "anotherPrivateValue" + id)); | ||
|
||
extension.runQuery(("insert into edc_asset (asset_id, properties, private_properties, data_address) " + | ||
"values ('%s', '%s'::json, '%s'::json, '{\"type\":\"type\"}'::json)") | ||
.formatted(id, propertiesArray, privatePropertiesArray)); | ||
} | ||
|
||
private String propertyJsonMap(String key, String value) { | ||
return "{\"property_name\" : \"%s\", \"property_value\" : \"%s\", \"property_type\" : \"java.lang.String\"}".formatted(key, value); | ||
} | ||
|
||
} |
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