Skip to content

Commit

Permalink
Small fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Aug 12, 2016
1 parent e0ba288 commit bae8807
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Expand Up @@ -61,7 +61,7 @@ public Map<Ctl, List<Schema>> aggregate(List<Schema> schemas) throws SQLExceptio
Ctl ctl = new Ctl(currentCtlId, new CtlMetaInfo(currentCTLMetaId, fqn, schema.getAppId(), tenantId), defaultRecord);

if (ctls.isEmpty()) {
confSchemasToCTL.put(ctl, asList(schema));
confSchemasToCTL.put(ctl, new ArrayList<>(asList(schema)));
ctls.add(ctl);
} else {
Ctl ctlToCompare = sameFqn(ctls, ctl);
Expand Down Expand Up @@ -132,7 +132,7 @@ private Ctl sameBody(Set<Ctl> set, Ctl ctl) {
}

private boolean bothAppIdNull(Ctl c1, Ctl c2) {
return c1.getMetaInfo().getAppId() != null && c2.getMetaInfo().getAppId() != null;
return c1.getMetaInfo().getAppId() == null && c2.getMetaInfo().getAppId() == null;
}


Expand All @@ -150,7 +150,7 @@ private boolean sameTenant(Ctl c1, Ctl c2) {
}


///TODO in future add npromotion if needed
///TODO in future add promotion if needed
private void putToMapSchema(Ctl c1, Ctl newCtl, Schema schema, String scope) {
if (!sameBody(c1, newCtl)) {
LOG.warn("Schemas in different %ss' scopes have different bodies {} and {} but the same fqn {}", scope, newCtl.getDefaultRecord(), c1.getDefaultRecord(), newCtl.getMetaInfo().getFqn());
Expand Down
Expand Up @@ -20,40 +20,48 @@ public class MigrateData {

private static Connection conn;

public static void main(String[] args) {
public static void main(String[] args) {
try {
List<Schema> schemas = new ArrayList<>();
conn = MARIADB.getDs().getConnection();
QueryRunner runner = new QueryRunner();
Long maxId = runner.query(conn, "select max(id) as max_id from base_schems", rs -> rs.next() ? rs.getLong("max_id") : null);
BaseSchemaIdCounter.setInitValue(maxId);
CTLConfigurationMigration configurationMigration = new CTLConfigurationMigration(conn);
CTLEventsMigration eventsMigration = new CTLEventsMigration(conn);

List<AbstractCTLMigration> migrationList = new ArrayList<>();
migrationList.add(new CTLConfigurationMigration(conn));
// migrationList.add(new CTLEventsMigration(conn));

CTLAggregation aggregation = new CTLAggregation(conn);
BaseSchemaRecordsCreation recordsCreation = new BaseSchemaRecordsCreation(conn);


//before phase
configurationMigration.beforeTransform();
eventsMigration.beforeTransform();
for (AbstractCTLMigration m : migrationList) {
m.beforeTransform();
}

// transform phase
schemas.addAll(configurationMigration.transform());
schemas.addAll(eventsMigration.transform());
for (AbstractCTLMigration m : migrationList) {
schemas.addAll(m.transform());
}

//aggregation phase
Map<Ctl, List<Schema>> ctlToSchemas = aggregation.aggregate(schemas);

//base schema records creation phase
recordsCreation.create(ctlToSchemas);


//after phase
configurationMigration.afterTransform();
eventsMigration.afterTransform();
for (AbstractCTLMigration m : migrationList) {
m.afterTransform();
}
;

} catch (SQLException | IOException | ConfigurationGenerationException e) {
DbUtils.rollbackAndCloseQuietly(conn);
} finally {
} finally {
DbUtils.closeQuietly(conn);
}
}
Expand Down
Expand Up @@ -8,12 +8,12 @@ public class BaseSchemaIdCounter {

// can be called only once
public static void setInitValue(Long value) {
BaseSchemaIdCounter i = getInstance();
getInstance();
if(isInitMethodCalled) {
return;
}
isInitMethodCalled = true;
i.value = value;
instance.value = value;
}

public Long getAndShift(Long shift) {
Expand All @@ -27,7 +27,7 @@ private BaseSchemaIdCounter() {
}

public static BaseSchemaIdCounter getInstance() {
if(instance != null) {
if(instance == null) {
instance = new BaseSchemaIdCounter();
}
return instance;
Expand Down

0 comments on commit bae8807

Please sign in to comment.