Skip to content

Commit 870b836

Browse files
committed
feat: Mongock import to FlamingockCommunity(WIP: dynamodb test passing and refactored)
1 parent d7c9e0b commit 870b836

File tree

2 files changed

+155
-89
lines changed

2 files changed

+155
-89
lines changed

legacy/mongock-importer-dynamodb/src/test/java/io/flamingock/importer/mongock/dynamodb/DynamoDBImporterTest.java

Lines changed: 5 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.flamingock.community.dynamodb.driver.DynamoDBAuditStore;
2121
import io.flamingock.core.kit.TestKit;
2222
import io.flamingock.core.kit.audit.AuditTestHelper;
23+
import io.flamingock.dynamodb.kit.DynamoDBTableFactory;
2324
import io.flamingock.dynamodb.kit.DynamoDBTestKit;
2425
import io.flamingock.internal.common.core.error.FlamingockException;
2526
import io.flamingock.internal.core.runner.Runner;
@@ -37,15 +38,9 @@
3738
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
3839
import software.amazon.awssdk.regions.Region;
3940
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
40-
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
41-
import software.amazon.awssdk.services.dynamodb.model.BillingMode;
42-
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
4341
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
4442
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
45-
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
4643
import software.amazon.awssdk.services.dynamodb.model.KeyType;
47-
import software.amazon.awssdk.services.dynamodb.model.ResourceInUseException;
48-
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
4944

5045
import java.net.URI;
5146

@@ -86,12 +81,10 @@ void setUp() {
8681
)
8782
.build();
8883

89-
// Create Mongock origin table for migration
90-
createMongockTable(DEFAULT_MONGOCK_ORIGIN);
91-
92-
// Create Flamingock audit and lock tables
93-
createAuditTable(DEFAULT_AUDIT_STORE_NAME);
94-
createLockTable(DEFAULT_LOCK_STORE_NAME);
84+
// Create required tables using DynamoDBTableFactory
85+
DynamoDBTableFactory.createMongockTable(client, DEFAULT_MONGOCK_ORIGIN);
86+
// DynamoDBTableFactory.createAuditTable(client, DEFAULT_AUDIT_STORE_NAME);
87+
// DynamoDBTableFactory.createLockTable(client, DEFAULT_LOCK_STORE_NAME);
9588

9689
mongockTestHelper = new DynamoDBMongockTestHelper(client, DEFAULT_MONGOCK_ORIGIN);
9790

@@ -100,83 +93,6 @@ void setUp() {
10093
auditHelper = testKit.getAuditHelper();
10194
}
10295

103-
private void createMongockTable(String tableName) {
104-
try {
105-
client.createTable(CreateTableRequest.builder()
106-
.tableName(tableName)
107-
.keySchema(
108-
KeySchemaElement.builder()
109-
.attributeName("executionId")
110-
.keyType(KeyType.HASH)
111-
.build(),
112-
KeySchemaElement.builder()
113-
.attributeName("changeId")
114-
.keyType(KeyType.RANGE)
115-
.build()
116-
)
117-
.attributeDefinitions(
118-
AttributeDefinition.builder()
119-
.attributeName("executionId")
120-
.attributeType(ScalarAttributeType.S)
121-
.build(),
122-
AttributeDefinition.builder()
123-
.attributeName("changeId")
124-
.attributeType(ScalarAttributeType.S)
125-
.build()
126-
)
127-
.billingMode(BillingMode.PAY_PER_REQUEST)
128-
.build());
129-
} catch (ResourceInUseException ignored) {
130-
// Table already exists, ignore
131-
}
132-
}
133-
134-
private void createAuditTable(String tableName) {
135-
try {
136-
client.createTable(CreateTableRequest.builder()
137-
.tableName(tableName)
138-
.keySchema(
139-
KeySchemaElement.builder()
140-
.attributeName("partitionKey")
141-
.keyType(KeyType.HASH)
142-
.build()
143-
)
144-
.attributeDefinitions(
145-
AttributeDefinition.builder()
146-
.attributeName("partitionKey")
147-
.attributeType(ScalarAttributeType.S)
148-
.build()
149-
)
150-
.billingMode(BillingMode.PAY_PER_REQUEST)
151-
.build());
152-
} catch (ResourceInUseException ignored) {
153-
// Table already exists, ignore
154-
}
155-
}
156-
157-
private void createLockTable(String tableName) {
158-
try {
159-
client.createTable(CreateTableRequest.builder()
160-
.tableName(tableName)
161-
.keySchema(
162-
KeySchemaElement.builder()
163-
.attributeName("partitionKey")
164-
.keyType(KeyType.HASH)
165-
.build()
166-
)
167-
.attributeDefinitions(
168-
AttributeDefinition.builder()
169-
.attributeName("partitionKey")
170-
.attributeType(ScalarAttributeType.S)
171-
.build()
172-
)
173-
.billingMode(BillingMode.PAY_PER_REQUEST)
174-
.build());
175-
} catch (ResourceInUseException ignored) {
176-
// Table already exists, ignore
177-
}
178-
}
179-
18096
@AfterEach
18197
void tearDown() {
18298
// DynamoDB local doesn't need explicit cleanup between tests
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright 2025 Flamingock (https://www.flamingock.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.flamingock.dynamodb.kit;
17+
18+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
19+
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
20+
import software.amazon.awssdk.services.dynamodb.model.BillingMode;
21+
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
22+
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
23+
import software.amazon.awssdk.services.dynamodb.model.KeyType;
24+
import software.amazon.awssdk.services.dynamodb.model.ResourceInUseException;
25+
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
26+
27+
/**
28+
* Factory class for creating DynamoDB tables in test environments.
29+
* <p>
30+
* This utility provides methods to create common Flamingock-related tables
31+
* with appropriate schemas for testing purposes. All tables are created with
32+
* PAY_PER_REQUEST billing mode to simplify test setup.
33+
* <p>
34+
* All methods are idempotent - they safely handle cases where tables already exist.
35+
*/
36+
public class DynamoDBTableFactory {
37+
38+
/**
39+
* Creates a Mongock origin table for migration testing.
40+
* <p>
41+
* This table uses a composite key structure matching Mongock's audit format:
42+
* <ul>
43+
* <li>Partition key: executionId (String)</li>
44+
* <li>Sort key: changeId (String)</li>
45+
* </ul>
46+
*
47+
* @param client the DynamoDB client
48+
* @param tableName the name of the Mongock table to create
49+
*/
50+
public static void createMongockTable(DynamoDbClient client, String tableName) {
51+
createTableSafe(client, CreateTableRequest.builder()
52+
.tableName(tableName)
53+
.keySchema(
54+
KeySchemaElement.builder()
55+
.attributeName("executionId")
56+
.keyType(KeyType.HASH)
57+
.build(),
58+
KeySchemaElement.builder()
59+
.attributeName("changeId")
60+
.keyType(KeyType.RANGE)
61+
.build()
62+
)
63+
.attributeDefinitions(
64+
AttributeDefinition.builder()
65+
.attributeName("executionId")
66+
.attributeType(ScalarAttributeType.S)
67+
.build(),
68+
AttributeDefinition.builder()
69+
.attributeName("changeId")
70+
.attributeType(ScalarAttributeType.S)
71+
.build()
72+
)
73+
.billingMode(BillingMode.PAY_PER_REQUEST)
74+
.build());
75+
}
76+
77+
/**
78+
* Creates a Flamingock audit table for storing change execution history.
79+
* <p>
80+
* This table uses a simple key structure:
81+
* <ul>
82+
* <li>Partition key: partitionKey (String)</li>
83+
* </ul>
84+
*
85+
* @param client the DynamoDB client
86+
* @param tableName the name of the audit table to create
87+
*/
88+
public static void createAuditTable(DynamoDbClient client, String tableName) {
89+
createTableSafe(client, CreateTableRequest.builder()
90+
.tableName(tableName)
91+
.keySchema(
92+
KeySchemaElement.builder()
93+
.attributeName("partitionKey")
94+
.keyType(KeyType.HASH)
95+
.build()
96+
)
97+
.attributeDefinitions(
98+
AttributeDefinition.builder()
99+
.attributeName("partitionKey")
100+
.attributeType(ScalarAttributeType.S)
101+
.build()
102+
)
103+
.billingMode(BillingMode.PAY_PER_REQUEST)
104+
.build());
105+
}
106+
107+
/**
108+
* Creates a Flamingock lock table for distributed locking.
109+
* <p>
110+
* This table uses a simple key structure:
111+
* <ul>
112+
* <li>Partition key: partitionKey (String)</li>
113+
* </ul>
114+
*
115+
* @param client the DynamoDB client
116+
* @param tableName the name of the lock table to create
117+
*/
118+
public static void createLockTable(DynamoDbClient client, String tableName) {
119+
createTableSafe(client, CreateTableRequest.builder()
120+
.tableName(tableName)
121+
.keySchema(
122+
KeySchemaElement.builder()
123+
.attributeName("partitionKey")
124+
.keyType(KeyType.HASH)
125+
.build()
126+
)
127+
.attributeDefinitions(
128+
AttributeDefinition.builder()
129+
.attributeName("partitionKey")
130+
.attributeType(ScalarAttributeType.S)
131+
.build()
132+
)
133+
.billingMode(BillingMode.PAY_PER_REQUEST)
134+
.build());
135+
}
136+
137+
/**
138+
* Creates a table safely, ignoring ResourceInUseException if the table already exists.
139+
*
140+
* @param client the DynamoDB client
141+
* @param request the table creation request
142+
*/
143+
private static void createTableSafe(DynamoDbClient client, CreateTableRequest request) {
144+
try {
145+
client.createTable(request);
146+
} catch (ResourceInUseException ignored) {
147+
// Table already exists, which is fine for idempotent test setup
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)