Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db): add SQL Schema Bootstrapper #410

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,6 @@ class InMemory extends Tests {
@PostgresqlIntegrationTest
@ExtendWith(IdentityHubEndToEndExtension.Postgres.class)
class Postgres extends Tests {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -543,21 +544,23 @@ class Postgres extends Tests {
private static final Integer DB_PORT = getFreePort();

@RegisterExtension
@Order(1)
static IdentityHubCustomizableEndToEndExtension runtime;
static PostgresSqlService server = new PostgresSqlService(DB_NAME, DB_PORT);

@Order(0) // must be the first extension to be evaluated since it starts the DB server
@RegisterExtension
paullatzelsperger marked this conversation as resolved.
Show resolved Hide resolved
static final BeforeAllCallback POSTGRES_CONTAINER_STARTER = context -> {
server.start();
};

static {
var ctx = IdentityHubEndToEndExtension.Postgres.context(DB_NAME, DB_PORT);
ctx.getRuntime().registerServiceMock(DidPublicKeyResolver.class, DID_PUBLIC_KEY_RESOLVER);
ctx.getRuntime().registerServiceMock(RevocationListService.class, REVOCATION_LIST_SERVICE);
runtime = new IdentityHubCustomizableEndToEndExtension(ctx);
}

@BeforeAll
static void beforeAll() {
server.start();
}

@AfterAll
static void afterAll() {
server.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Map<String, String> config() {
put("web.http.identity.path", identityEndpoint.getUrl().getPath());
put("edc.runtime.id", name);
put("edc.ih.iam.id", "did:web:consumer");
put("edc.sql.schema.autocreate", "true");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.sql.testfixtures.PostgresqlEndToEndInstance;
import org.eclipse.edc.sql.testfixtures.PostgresqlLocalInstance;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.stream.Stream;

import static java.lang.String.format;

/**
Expand Down Expand Up @@ -67,7 +60,7 @@ public static String baseJdbcUrl(Integer hostPort) {
public static String jdbcUrl(String name, Integer hostPort) {
return baseJdbcUrl(hostPort) + name;
}

public void start() {
postgreSqlContainer.start();
postgreSqlContainer.waitingFor(Wait.forHealthcheck());
Expand All @@ -79,38 +72,8 @@ public void stop() {
postgreSqlContainer.close();
}

public String jdbcUrl() {
return jdbcUrl(dbName, hostPort);
}

private void createDatabase() {
var postgres = new PostgresqlLocalInstance(PostgresqlEndToEndInstance.USER, PostgresqlEndToEndInstance.PASSWORD, baseJdbcUrl(hostPort), dbName);
postgres.createDatabase();

var extensionsFolder = TestUtils.findBuildRoot().toPath().resolve("extensions");
var scripts = Stream.of(
"store/sql/identity-hub-credentials-store-sql",
"store/sql/identity-hub-did-store-sql",
"store/sql/identity-hub-keypair-store-sql",
"store/sql/identity-hub-participantcontext-store-sql"
)
.map(extensionsFolder::resolve)
.map(it -> it.resolve("docs"))
.map(it -> it.resolve("schema.sql"))
.toList();

try (var connection = postgres.getConnection(dbName)) {
for (var script : scripts) {
var sql = Files.readString(script);

try (var statement = connection.createStatement()) {
statement.execute(sql);
} catch (Exception exception) {
throw new EdcPersistenceException(exception.getMessage(), exception);
}
}
} catch (SQLException | IOException e) {
throw new EdcPersistenceException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
dependencies {
api(project(":spi:identity-hub-store-spi"))
implementation(libs.edc.core.sql) // for the SqlStatements
implementation(libs.edc.sql.bootstrapper) // for the schema bootstrapper
implementation(libs.edc.spi.transaction.datasource)
implementation(libs.edc.lib.util)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.bootstrapper.SqlSchemaBootstrapper;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;

Expand All @@ -46,6 +47,13 @@ public class SqlCredentialStoreExtension implements ServiceExtension {
@Inject(required = false)
private CredentialStoreStatements statements;

@Inject
private SqlSchemaBootstrapper sqlSchemaBootstrapper;

@Override
public void initialize(ServiceExtensionContext context) {
sqlSchemaBootstrapper.addStatementFromResource(getDataSourceName(context), "credentials-schema.sql");
}

@Provider
public CredentialStore createSqlStore(ServiceExtensionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
import org.eclipse.edc.identityhub.store.test.CredentialStoreTestBase;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class SqlCredentialsStoreTest extends CredentialStoreTestBase {
Expand All @@ -37,12 +34,12 @@ class SqlCredentialsStoreTest extends CredentialStoreTestBase {
private SqlCredentialStore store;

@BeforeEach
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) throws IOException {
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) {
var typeManager = new JacksonTypeManager();
store = new SqlCredentialStore(extension.getDataSourceRegistry(), extension.getDatasourceName(),
extension.getTransactionContext(), typeManager.getMapper(), queryExecutor, statements);

var schema = Files.readString(Paths.get("./docs/schema.sql"));
var schema = TestUtils.getResourceFileContentAsString("credentials-schema.sql");
extension.runQuery(schema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
dependencies {
api(project(":spi:did-spi"))
implementation(libs.edc.core.sql) // for the SqlStatements
implementation(libs.edc.sql.bootstrapper)
implementation(libs.edc.spi.transaction.datasource)

testImplementation(testFixtures(project(":spi:did-spi")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.bootstrapper.SqlSchemaBootstrapper;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;

Expand All @@ -46,6 +47,13 @@ public class SqlDidResourceStoreExtension implements ServiceExtension {
@Inject(required = false)
private DidResourceStatements statements;

@Inject
private SqlSchemaBootstrapper sqlSchemaBootstrapper;

@Override
public void initialize(ServiceExtensionContext context) {
sqlSchemaBootstrapper.addStatementFromResource(getDataSourceName(context), "did-schema.sql");
}

@Provider
public DidResourceStore createSqlStore(ServiceExtensionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
import org.eclipse.edc.identityhub.did.store.test.DidResourceStoreTestBase;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class SqlDidResourceStoreTest extends DidResourceStoreTestBase {
Expand All @@ -37,12 +34,12 @@ class SqlDidResourceStoreTest extends DidResourceStoreTestBase {
private SqlDidResourceStore store;

@BeforeEach
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) throws IOException {
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) {
var typeManager = new JacksonTypeManager();
store = new SqlDidResourceStore(extension.getDataSourceRegistry(), extension.getDatasourceName(),
extension.getTransactionContext(), typeManager.getMapper(), queryExecutor, statements);

var schema = Files.readString(Paths.get("./docs/schema.sql"));
var schema = TestUtils.getResourceFileContentAsString("did-schema.sql");
extension.runQuery(schema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
dependencies {
api(project(":spi:identity-hub-store-spi"))
implementation(libs.edc.core.sql) // for the SqlStatements
implementation(libs.edc.sql.bootstrapper)
implementation(libs.edc.spi.transaction.datasource)

testImplementation(testFixtures(project(":spi:identity-hub-store-spi")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.bootstrapper.SqlSchemaBootstrapper;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;

Expand Down Expand Up @@ -51,11 +52,19 @@ public class SqlKeyPairResourceStoreExtension implements ServiceExtension {
@Inject(required = false)
private KeyPairResourceStoreStatements statements;

@Inject
private SqlSchemaBootstrapper sqlSchemaBootstrapper;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
sqlSchemaBootstrapper.addStatementFromResource(getDataSourceName(context), "keypairs-schema.sql");
}

@Provider
public KeyPairResourceStore createSqlStore(ServiceExtensionContext context) {
return new SqlKeyPairResourceStore(dataSourceRegistry, getDataSourceName(context), transactionContext, typemanager.getMapper(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,26 @@
import org.eclipse.edc.identityhub.store.test.KeyPairResourceStoreTestBase;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class SqlKeyPairResourceStoreTest extends KeyPairResourceStoreTestBase {
private final KeyPairResourceStoreStatements statements = new PostgresDialectStatements();
private SqlKeyPairResourceStore store;

@BeforeEach
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) throws IOException {
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) {
var typeManager = new JacksonTypeManager();
store = new SqlKeyPairResourceStore(extension.getDataSourceRegistry(), extension.getDatasourceName(),
extension.getTransactionContext(), typeManager.getMapper(), queryExecutor, statements);

var schema = Files.readString(Paths.get("./docs/schema.sql"));
var schema = TestUtils.getResourceFileContentAsString("keypairs-schema.sql");
extension.runQuery(schema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
dependencies {
api(project(":spi:identity-hub-store-spi"))
implementation(libs.edc.core.sql) // for the SqlStatements
implementation(libs.edc.sql.bootstrapper)
implementation(libs.edc.spi.transaction.datasource)

testImplementation(testFixtures(project(":spi:identity-hub-store-spi")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.bootstrapper.SqlSchemaBootstrapper;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;

Expand All @@ -46,6 +47,13 @@ public class SqlParticipantContextStoreExtension implements ServiceExtension {
@Inject(required = false)
private ParticipantContextStoreStatements statements;

@Inject
private SqlSchemaBootstrapper sqlSchemaBootstrapper;

@Override
public void initialize(ServiceExtensionContext context) {
sqlSchemaBootstrapper.addStatementFromResource(getDataSourceName(context), "participant-schema.sql");
}

@Provider
public ParticipantContextStore createSqlStore(ServiceExtensionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
import org.eclipse.edc.identityhub.store.test.ParticipantContextStoreTestBase;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class SqlParticipantContextStoreTest extends ParticipantContextStoreTestBase {
Expand All @@ -39,12 +36,12 @@ class SqlParticipantContextStoreTest extends ParticipantContextStoreTestBase {
private SqlParticipantContextStore store;

@BeforeEach
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) throws IOException {
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) {
var typeManager = new JacksonTypeManager();
store = new SqlParticipantContextStore(extension.getDataSourceRegistry(), extension.getDatasourceName(),
extension.getTransactionContext(), typeManager.getMapper(), queryExecutor, statements);

var schema = Files.readString(Paths.get("./docs/schema.sql"));
var schema = TestUtils.getResourceFileContentAsString("participant-schema.sql");
extension.runQuery(schema);
}

Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ edc-ext-observability = { module = "org.eclipse.edc:api-observability", version.
edc-ext-transaction-local = { module = "org.eclipse.edc:transaction-local", version.ref = "edc" }
edc-testfixtures-managementapi = { module = "org.eclipse.edc:management-api-test-fixtures", version.ref = "edc" }
edc-sql-pool = { module = "org.eclipse.edc:sql-pool-apache-commons", version.ref = "edc" }
edc-sql-bootstrapper = { module = "org.eclipse.edc:sql-bootstrapper", version.ref = "edc" }

# EDC libs
edc-lib-keys = { "module" = "org.eclipse.edc:keys-lib", version.ref = "edc" }
Expand Down
Loading