Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
33 changes: 33 additions & 0 deletions community/flamingock-auditstore-sql/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
dependencies {
api(project(":core:flamingock-core"))
api(project(":core:target-systems:sql-target-system"))
implementation(project(":utils:sql-util"))

testImplementation("mysql:mysql-connector-java:8.0.33")
testImplementation("com.microsoft.sqlserver:mssql-jdbc:12.4.2.jre8")
testImplementation("com.oracle.database.jdbc:ojdbc8:21.9.0.0")
testImplementation("org.postgresql:postgresql:42.7.3")
testImplementation("org.mariadb.jdbc:mariadb-java-client:3.3.2")
testImplementation("org.testcontainers:mysql:1.21.3")
testImplementation("org.testcontainers:mssqlserver:1.21.3")
testImplementation("org.testcontainers:oracle-xe:1.21.3")
testImplementation("org.testcontainers:postgresql:1.21.3")
testImplementation("org.testcontainers:mariadb:1.21.3")
testImplementation(project(":utils:test-util"))
testImplementation("com.zaxxer:HikariCP:3.4.5")
testImplementation("org.testcontainers:junit-jupiter:1.21.3")
testImplementation("com.h2database:h2:2.2.224")
testImplementation("org.mockito:mockito-inline:4.11.0")
}

description = "SQL audit store implementation for distributed change auditing"

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}

configurations.testImplementation {
extendsFrom(configurations.compileOnly.get())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://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.
*/
package io.flamingock.community.sql.driver;

import io.flamingock.internal.core.store.CommunityAuditStore;
import io.flamingock.internal.core.store.audit.community.CommunityAuditPersistence;
import io.flamingock.internal.core.store.lock.community.CommunityLockService;
import io.flamingock.internal.common.core.context.ContextResolver;
import io.flamingock.internal.core.configuration.community.CommunityConfigurable;
import io.flamingock.internal.util.constants.CommunityPersistenceConstants;
import io.flamingock.internal.util.id.RunnerId;
import io.flamingock.community.sql.internal.SqlAuditPersistence;
import io.flamingock.community.sql.internal.SqlLockService;

import javax.sql.DataSource;

public class SqlAuditStore implements CommunityAuditStore {

private final DataSource dataSource;
private CommunityConfigurable communityConfiguration;
private RunnerId runnerId;
private SqlAuditPersistence persistence;
private SqlLockService lockService;
private String auditRepositoryName = CommunityPersistenceConstants.DEFAULT_AUDIT_STORE_NAME;
Comment thread
davidfrigolet marked this conversation as resolved.
private String lockRepositoryName = CommunityPersistenceConstants.DEFAULT_LOCK_STORE_NAME;
private boolean autoCreate = true;

public SqlAuditStore(DataSource dataSource) {
this.dataSource = dataSource;
}

public SqlAuditStore withAuditRepositoryName(String auditRepositoryName) {
this.auditRepositoryName = auditRepositoryName;
return this;
}

public SqlAuditStore withLockRepositoryName(String lockRepositoryName) {
this.lockRepositoryName = lockRepositoryName;
return this;
}

public SqlAuditStore withAutoCreate(boolean autoCreate) {
this.autoCreate = autoCreate;
return this;
}

@Override
public void initialize(ContextResolver baseContext) {
runnerId = baseContext.getRequiredDependencyValue(RunnerId.class);
communityConfiguration = baseContext.getRequiredDependencyValue(CommunityConfigurable.class);
}

@Override
public synchronized CommunityAuditPersistence getPersistence() {
if (persistence == null) {
persistence = new SqlAuditPersistence(communityConfiguration, dataSource, auditRepositoryName, autoCreate);
persistence.initialize(runnerId);
}
return persistence;
}


@Override
public synchronized CommunityLockService getLockService() {
if (lockService == null) {
lockService = new SqlLockService(dataSource, lockRepositoryName, autoCreate);
}
return lockService;
}
}
Comment thread
davidfrigolet marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://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.
*/
package io.flamingock.community.sql.internal;

import io.flamingock.internal.core.configuration.community.CommunityConfigurable;
import io.flamingock.internal.core.store.audit.community.AbstractCommunityAuditPersistence;
import io.flamingock.internal.common.core.audit.AuditEntry;
import io.flamingock.internal.util.Result;
import io.flamingock.internal.util.id.RunnerId;

import javax.sql.DataSource;
import java.util.List;

public class SqlAuditPersistence extends AbstractCommunityAuditPersistence {

private final DataSource dataSource;
private final String auditRepositoryName;
private final boolean autoCreate;
private SqlAuditor auditor;

public SqlAuditPersistence(CommunityConfigurable localConfiguration,
DataSource dataSource,
String auditRepositoryName,
boolean autoCreate) {
super(localConfiguration);
this.dataSource = dataSource;
this.auditRepositoryName = auditRepositoryName;
this.autoCreate = autoCreate;
}

@Override
protected void doInitialize(RunnerId runnerId) {
auditor = new SqlAuditor(dataSource, auditRepositoryName, autoCreate);
auditor.initialize();
}

@Override
public List<AuditEntry> getAuditHistory() {
return auditor.getAuditHistory();
}

@Override
public Result writeEntry(AuditEntry auditEntry) {
return auditor.writeEntry(auditEntry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://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.
*/
package io.flamingock.community.sql.internal;

import io.flamingock.internal.common.core.audit.AuditEntry;
import io.flamingock.internal.common.core.audit.AuditReader;
import io.flamingock.internal.common.core.audit.AuditTxType;
import io.flamingock.internal.core.store.audit.LifecycleAuditWriter;
import io.flamingock.internal.util.Result;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SqlAuditor implements LifecycleAuditWriter, AuditReader {

private final DataSource dataSource;
private final String auditTableName;
private final boolean autoCreate;
private final SqlAuditorDialectHelper dialectHelper;

public SqlAuditor(DataSource dataSource, String auditTableName, boolean autoCreate) {
this.dataSource = dataSource;
this.auditTableName = auditTableName;
this.autoCreate = autoCreate;
this.dialectHelper = new SqlAuditorDialectHelper(dataSource);
}

public void initialize() {
if (autoCreate) {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
stmt.executeUpdate(dialectHelper.getCreateTableSqlString(auditTableName));
} catch (SQLException e) {
throw new RuntimeException("Failed to initialize audit table", e);
}
}
}

@Override
public Result writeEntry(AuditEntry auditEntry) {
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(
dialectHelper.getInsertSqlString(auditTableName))) {
ps.setString(1, auditEntry.getExecutionId());
ps.setString(2, auditEntry.getStageId());
ps.setString(3, auditEntry.getTaskId());
ps.setString(4, auditEntry.getAuthor());
ps.setTimestamp(5, Timestamp.valueOf(auditEntry.getCreatedAt()));
ps.setString(6, auditEntry.getState() != null ? auditEntry.getState().name() : null);
ps.setString(7, auditEntry.getClassName());
ps.setString(8, auditEntry.getMethodName());
ps.setString(9, auditEntry.getMetadata() != null ? auditEntry.getMetadata().toString() : null);
ps.setLong(10, auditEntry.getExecutionMillis());
ps.setString(11, auditEntry.getExecutionHostname());
ps.setString(12, auditEntry.getErrorTrace());
ps.setString(13, auditEntry.getType() != null ? auditEntry.getType().name() : null);
ps.setString(14, auditEntry.getTxType() != null ? auditEntry.getTxType().name() : null);
ps.setString(15, auditEntry.getTargetSystemId());
ps.setString(16, auditEntry.getOrder());
ps.setString(17, auditEntry.getRecoveryStrategy() != null ? auditEntry.getRecoveryStrategy().name() : null);
ps.setObject(18, auditEntry.getTransactionFlag());
ps.setObject(19, auditEntry.getSystemChange());
ps.executeUpdate();
return Result.OK();
} catch (SQLException e) {
return new Result.Error(e);
}
}

@Override
public List<AuditEntry> getAuditHistory() {
List<AuditEntry> entries = new ArrayList<>();
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(dialectHelper.getSelectHistorySqlString(auditTableName))) {
while (rs.next()) {
AuditEntry entry = new AuditEntry(
rs.getString("execution_id"),
rs.getString("stage_id"),
rs.getString("task_id"),
rs.getString("author"),
rs.getTimestamp("created_at").toLocalDateTime(),
rs.getString("state") != null ? AuditEntry.Status.valueOf(rs.getString("state")) : null,
rs.getString("type") != null ? AuditEntry.ExecutionType.valueOf(rs.getString("type")) : null,
rs.getString("class_name"),
rs.getString("method_name"),
rs.getLong("execution_millis"),
rs.getString("execution_hostname"),
rs.getString("metadata"),
rs.getBoolean("system_change"),
rs.getString("error_trace"),
AuditTxType.fromString(rs.getString("tx_type")),
rs.getString("target_system_id"),
rs.getString("order_col"),
rs.getString("recovery_strategy") != null ? io.flamingock.api.RecoveryStrategy.valueOf(rs.getString("recovery_strategy")) : null,
rs.getObject("transaction_flag") != null ? rs.getBoolean("transaction_flag") : null
);
entries.add(entry);
}
} catch (SQLException e) {
throw new RuntimeException("Failed to read audit history", e);
}
return entries;
}
}
Loading
Loading