|
3 | 3 | import com.github.cloudyrock.mongock.ChangeLog;
|
4 | 4 | import com.github.cloudyrock.mongock.ChangeSet;
|
5 | 5 | import com.github.cloudyrock.mongock.driver.mongodb.springdata.v4.decorator.impl.MongockTemplate;
|
| 6 | +import com.github.f4b6a3.uuid.UuidCreator; |
6 | 7 | import lombok.extern.slf4j.Slf4j;
|
| 8 | +import org.bson.Document; |
7 | 9 | import org.lowcoder.domain.application.model.Application;
|
8 | 10 | import org.lowcoder.domain.datasource.model.Datasource;
|
9 | 11 | import org.lowcoder.domain.datasource.model.DatasourceStructureDO;
|
|
26 | 28 | import org.springframework.context.annotation.Profile;
|
27 | 29 | import org.springframework.data.domain.Sort;
|
28 | 30 | import org.springframework.data.mongodb.UncategorizedMongoDbException;
|
| 31 | +import org.springframework.data.mongodb.core.DocumentCallbackHandler; |
29 | 32 | import org.springframework.data.mongodb.core.index.CompoundIndexDefinition;
|
30 | 33 | import org.springframework.data.mongodb.core.index.Index;
|
31 | 34 | import org.springframework.data.mongodb.core.index.IndexOperations;
|
| 35 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 36 | +import org.springframework.data.mongodb.core.query.Query; |
| 37 | +import org.springframework.data.mongodb.core.query.Update; |
| 38 | + |
| 39 | +import java.util.Set; |
32 | 40 |
|
33 | 41 | import static org.lowcoder.domain.util.QueryDslUtils.fieldName;
|
34 | 42 | import static org.lowcoder.sdk.util.IDUtils.generate;
|
@@ -194,6 +202,55 @@ public void addPtmFieldsToApplicatgions(AddPtmFieldsJob addPtmFieldsJob) {
|
194 | 202 | addPtmFieldsJob.migrateApplicationsToInitPtmFields();
|
195 | 203 | }
|
196 | 204 |
|
| 205 | + @ChangeSet(order = "022", id = "add-gid", author = "") |
| 206 | + public void addGidToDBObjects(MongockTemplate mongoTemplate) { |
| 207 | + // Define an array of collection names |
| 208 | + String[] collectionNames = {"application", "bundle", "datasource", "libraryQuery", "folder"}; |
| 209 | + |
| 210 | + // Get the list of existing collections |
| 211 | + Set<String> existingCollections = mongoTemplate.getCollectionNames(); |
| 212 | + |
| 213 | + for (String collectionName : collectionNames) { |
| 214 | + if (existingCollections.contains(collectionName)) { |
| 215 | + addGidField(mongoTemplate, collectionName); |
| 216 | + } else { |
| 217 | + System.out.println("Collection " + collectionName + " does not exist."); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private void addGidField(MongockTemplate mongoTemplate, String collectionName) { |
| 223 | + // Create a query to match all documents |
| 224 | + Query query = new Query(); |
| 225 | + |
| 226 | + // Use a DocumentCallbackHandler to iterate through each document |
| 227 | + mongoTemplate.executeQuery(query, collectionName, new DocumentCallbackHandler() { |
| 228 | + @Override |
| 229 | + public void processDocument(Document document) { |
| 230 | + // Generate a random UUID and ensure it is unique within the collection |
| 231 | + String uniqueGid; |
| 232 | + do { |
| 233 | + uniqueGid = UuidCreator.getTimeOrderedEpoch().toString(); |
| 234 | + } while (isGidPresent(mongoTemplate, collectionName, uniqueGid)); |
| 235 | + |
| 236 | + // Create an update object to add the 'gid' field |
| 237 | + Update update = new Update(); |
| 238 | + update.set("gid", uniqueGid); |
| 239 | + |
| 240 | + // Create a query to match the current document by its _id |
| 241 | + Query idQuery = new Query(Criteria.where("_id").is(document.getObjectId("_id"))); |
| 242 | + |
| 243 | + // Update the document with the new 'gid' field |
| 244 | + mongoTemplate.updateFirst(idQuery, update, collectionName); |
| 245 | + } |
| 246 | + }); |
| 247 | + } |
| 248 | + |
| 249 | + private boolean isGidPresent(MongockTemplate mongoTemplate, String collectionName, String gid) { |
| 250 | + Query query = new Query(Criteria.where("gid").is(gid)); |
| 251 | + return mongoTemplate.exists(query, collectionName); |
| 252 | + } |
| 253 | + |
197 | 254 | public static Index makeIndex(String... fields) {
|
198 | 255 | if (fields.length == 1) {
|
199 | 256 | return new Index(fields[0], Sort.Direction.ASC).named(fields[0]);
|
|
0 commit comments