Skip to content

Commit

Permalink
KAA-1297: Add log schema migration support
Browse files Browse the repository at this point in the history
  • Loading branch information
sashadidukh committed Aug 12, 2016
1 parent 46a721c commit 67964c3
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 81 deletions.
@@ -1,7 +1,21 @@
package org.kaaproject.data_migration;
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.kaaproject.data_migration.model.Schema;
Expand Down Expand Up @@ -29,7 +43,7 @@ public AbstractCTLMigration(Connection connection) {

public void beforeTransform() throws SQLException {
// delete relation between <feature>_schems to schems
dd.dropUnnamedFK(getName() + "_schems", "schems");
dd.dropUnnamedFK(getPrefixTableName() + "_schems", "schems");
}


Expand All @@ -38,25 +52,25 @@ protected List<Schema> transform() throws SQLException {
List<Schema> schemas = runner.query(connection, "select " +
"f.id as id, created_time as createdTime, created_username as createdUsername, " +
"description, name, schems, version, application_id as appId " +
"from " + getName() + "_schems f join schems s on f.id = s.id", new BeanListHandler<>(Schema.class));
"from " + getPrefixTableName() + "_schems f join schems s on f.id = s.id", new BeanListHandler<>(Schema.class));

// delete the fetched ids from schema table
String toDelete = schemas.stream().map(s -> s.getId().toString()).collect(joining(", "));
runner.update(connection, "delete from schems where id in (" + toDelete + ")");

// shift ids in order to avoid PK constraint violation during adding record to base_schema
Long shift = runner.query(connection, "select max(id) as max_id from "+ getName() + "_schems", rs -> rs.next() ? rs.getLong("max_id") : null);
Long shift = runner.query(connection, "select max(id) as max_id from "+ getPrefixTableName() + "_schems", rs -> rs.next() ? rs.getLong("max_id") : null);
Long idShift = BaseSchemaIdCounter.getInstance().getAndShift(shift);
runner.update(connection, "update " + getName() + "_schems set id = id + " + idShift + " order by id desc");
runner.update(connection, "update " + getPrefixTableName() + "_schems set id = id + " + idShift + " order by id desc");
schemas.forEach(s -> s.setId(s.getId() + idShift));

return schemas;
}


public void afterTransform() throws SQLException {
dd.alterTable(getName() + "_schems")
.add(constraint("FK_" + getName() + "_base_schems_id")
dd.alterTable(getPrefixTableName() + "_schems")
.add(constraint("FK_" + getPrefixTableName() + "_base_schems_id")
.foreignKey("id")
.references("base_schems", "id")
.onDelete(CASCADE)
Expand All @@ -65,6 +79,6 @@ public void afterTransform() throws SQLException {
.execute();
}

protected abstract String getName();
protected abstract String getPrefixTableName();

}
@@ -1,5 +1,20 @@
package org.kaaproject.data_migration;
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import org.apache.commons.dbutils.QueryRunner;
import org.kaaproject.data_migration.model.Ctl;
Expand Down
@@ -1,5 +1,20 @@
package org.kaaproject.data_migration;
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
Expand All @@ -19,11 +34,15 @@
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Arrays.binarySearch;

public class CTLAggregation {
private Connection connection;
Expand Down
@@ -1,26 +1,24 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.kaaproject.data_migration.model.Schema;
import org.kaaproject.data_migration.model.Ctl;
import org.kaaproject.data_migration.model.CtlMetaInfo;
import org.kaaproject.data_migration.utils.datadefinition.DataDefinition;
import org.kaaproject.kaa.server.common.core.algorithms.generation.ConfigurationGenerationException;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithm;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithmImpl;
import org.kaaproject.kaa.server.common.core.configuration.RawData;
import org.kaaproject.kaa.server.common.core.configuration.RawDataFactory;
import org.kaaproject.kaa.server.common.core.schema.RawSchema;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

import static java.util.stream.Collectors.joining;
import static org.kaaproject.data_migration.utils.datadefinition.Constraint.constraint;
import static org.kaaproject.data_migration.utils.datadefinition.ReferenceOptions.CASCADE;

Expand All @@ -37,7 +35,7 @@ public void beforeTransform() throws SQLException {

// change FK constraint between table that contains data and appropriate <feature>_schems table
dd.dropUnnamedFK("configuration", "configuration_schems");
dd.alterTable(getName())
dd.alterTable(getPrefixTableName())
.add(constraint("FK_configuration_schems_id")
.foreignKey("configuration_schems_id")
.references("configuration_schems", "id")
Expand All @@ -48,7 +46,7 @@ public void beforeTransform() throws SQLException {
}

@Override
protected String getName() {
protected String getPrefixTableName() {
return "configuration";
}

Expand Down
@@ -1,3 +1,19 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -32,7 +48,7 @@ public CTLEventsMigration(Connection connection) {

//actually not needed here
@Override
protected String getName() {
protected String getPrefixTableName() {
return null;
}

Expand Down
@@ -0,0 +1,33 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import java.sql.Connection;

public class CtlLogMigration extends AbstractCTLMigration {

public static final String LOG_SCHEMA_PREFIX_TABLE_NAME = "log";

public CtlLogMigration(Connection connection) {
super(connection);
}

@Override
protected String getPrefixTableName() {
return LOG_SCHEMA_PREFIX_TABLE_NAME;
}
}
@@ -1,3 +1,19 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import org.apache.commons.dbutils.DbUtils;
Expand Down Expand Up @@ -31,6 +47,7 @@ public static void main(String[] args) {
List<AbstractCTLMigration> migrationList = new ArrayList<>();
migrationList.add(new CTLConfigurationMigration(conn));
migrationList.add(new CTLEventsMigration(conn));
migrationList.add(new CtlLogMigration(conn));

CTLAggregation aggregation = new CTLAggregation(conn);
BaseSchemaRecordsCreation recordsCreation = new BaseSchemaRecordsCreation(conn);
Expand Down
@@ -1,5 +1,20 @@
package org.kaaproject.data_migration;
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

public class MigrationException extends RuntimeException {

Expand Down
@@ -1,7 +1,21 @@
package org.kaaproject.data_migration;
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
Expand Down
@@ -1,3 +1,19 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration.model;

import java.io.Serializable;
Expand Down
@@ -1,5 +1,20 @@
package org.kaaproject.data_migration.model;
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration.model;

public class Ctl {
private final Long id;
Expand Down
@@ -1,3 +1,19 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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 org.kaaproject.data_migration.model;

public class CtlMetaInfo {
Expand Down

0 comments on commit 67964c3

Please sign in to comment.