-
Notifications
You must be signed in to change notification settings - Fork 65
feat: sql audit store #720
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
611dac2
feat: sql audit store
davidfrigolet 9589155
feat: sql audit store
davidfrigolet ac2780a
Merge branch 'develop' of github.com:flamingock/flamingock-java into …
davidfrigolet a841a97
feat: add sql dialect helpers in sql audit store
davidfrigolet 641fdb4
feat: update sql dialect helpers for sqlserver, postgresql and oracle…
davidfrigolet 5ac12ac
feat: update sql audit store test with mariadb
davidfrigolet ca110b3
fix: changes after code review
davidfrigolet 04ac3be
fix: changes after code review
davidfrigolet e55845e
Merge branch 'develop' of github.com:flamingock/flamingock-java into …
davidfrigolet 4108d47
feat: update sql audit store test with h2
davidfrigolet 60af2a4
feat: add sqlite test in sql audit store
davidfrigolet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,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()) | ||
| } |
83 changes: 83 additions & 0 deletions
83
...ingock-auditstore-sql/src/main/java/io/flamingock/community/sql/driver/SqlAuditStore.java
This file contains hidden or 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,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; | ||
| 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; | ||
| } | ||
| } | ||
|
davidfrigolet marked this conversation as resolved.
|
||
59 changes: 59 additions & 0 deletions
59
...uditstore-sql/src/main/java/io/flamingock/community/sql/internal/SqlAuditPersistence.java
This file contains hidden or 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,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); | ||
| } | ||
| } |
120 changes: 120 additions & 0 deletions
120
...mingock-auditstore-sql/src/main/java/io/flamingock/community/sql/internal/SqlAuditor.java
This file contains hidden or 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,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; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.