DB upgrade script for JSON support feature
* Added upgrade * Added upgrade call for attributes from ThingsboardInstallService * refactored cassandra upgrade services
This commit is contained in:
		
							parent
							
								
									436d37ff42
								
							
						
					
					
						commit
						a2b7e1c098
					
				@ -136,6 +136,7 @@ public class ThingsboardInstallService {
 | 
			
		||||
                        log.info("Upgrading ThingsBoard from version 2.4.3 to 2.5 ...");
 | 
			
		||||
 | 
			
		||||
                        databaseTsUpgradeService.upgradeDatabase("2.4.3");
 | 
			
		||||
                        databaseEntitiesUpgradeService.upgradeDatabase("2.4.3");
 | 
			
		||||
 | 
			
		||||
                        log.info("Updating system data...");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,48 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2020 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * 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.thingsboard.server.service.install;
 | 
			
		||||
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Qualifier;
 | 
			
		||||
import org.thingsboard.server.dao.cassandra.CassandraCluster;
 | 
			
		||||
import org.thingsboard.server.dao.cassandra.CassandraInstallCluster;
 | 
			
		||||
import org.thingsboard.server.service.install.cql.CQLStatementsParser;
 | 
			
		||||
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
public abstract class AbstractCassandraDatabaseUpgradeService {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    protected CassandraCluster cluster;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    @Qualifier("CassandraInstallCluster")
 | 
			
		||||
    private CassandraInstallCluster installCluster;
 | 
			
		||||
 | 
			
		||||
    protected void loadCql(Path cql) throws Exception {
 | 
			
		||||
        List<String> statements = new CQLStatementsParser(cql).getStatements();
 | 
			
		||||
        statements.forEach(statement -> {
 | 
			
		||||
            installCluster.getSession().execute(statement);
 | 
			
		||||
            try {
 | 
			
		||||
                Thread.sleep(2500);
 | 
			
		||||
            } catch (InterruptedException e) {
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        Thread.sleep(5000);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -19,20 +19,15 @@ import com.datastax.driver.core.KeyspaceMetadata;
 | 
			
		||||
import com.datastax.driver.core.exceptions.InvalidQueryException;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Qualifier;
 | 
			
		||||
import org.springframework.context.annotation.Profile;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.server.dao.cassandra.CassandraCluster;
 | 
			
		||||
import org.thingsboard.server.dao.cassandra.CassandraInstallCluster;
 | 
			
		||||
import org.thingsboard.server.dao.dashboard.DashboardService;
 | 
			
		||||
import org.thingsboard.server.dao.util.NoSqlDao;
 | 
			
		||||
import org.thingsboard.server.service.install.cql.CQLStatementsParser;
 | 
			
		||||
import org.thingsboard.server.service.install.cql.CassandraDbHelper;
 | 
			
		||||
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.nio.file.Paths;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.server.service.install.DatabaseHelper.ADDITIONAL_INFO;
 | 
			
		||||
import static org.thingsboard.server.service.install.DatabaseHelper.ASSET;
 | 
			
		||||
@ -59,17 +54,10 @@ import static org.thingsboard.server.service.install.DatabaseHelper.TYPE;
 | 
			
		||||
@NoSqlDao
 | 
			
		||||
@Profile("install")
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class CassandraDatabaseUpgradeService implements DatabaseEntitiesUpgradeService {
 | 
			
		||||
public class CassandraDatabaseUpgradeService extends AbstractCassandraDatabaseUpgradeService implements DatabaseEntitiesUpgradeService {
 | 
			
		||||
 | 
			
		||||
    private static final String SCHEMA_UPDATE_CQL = "schema_update.cql";
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private CassandraCluster cluster;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    @Qualifier("CassandraInstallCluster")
 | 
			
		||||
    private CassandraInstallCluster installCluster;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private DashboardService dashboardService;
 | 
			
		||||
 | 
			
		||||
@ -264,7 +252,8 @@ public class CassandraDatabaseUpgradeService implements DatabaseEntitiesUpgradeS
 | 
			
		||||
                try {
 | 
			
		||||
                    cluster.getSession().execute(updateDeviceTableStmt);
 | 
			
		||||
                    Thread.sleep(2500);
 | 
			
		||||
                } catch (InvalidQueryException e) {}
 | 
			
		||||
                } catch (InvalidQueryException e) {
 | 
			
		||||
                }
 | 
			
		||||
                log.info("Schema updated.");
 | 
			
		||||
                break;
 | 
			
		||||
            case "2.4.1":
 | 
			
		||||
@ -275,7 +264,8 @@ public class CassandraDatabaseUpgradeService implements DatabaseEntitiesUpgradeS
 | 
			
		||||
                    cluster.getSession().execute(updateAssetTableStmt);
 | 
			
		||||
                    Thread.sleep(2500);
 | 
			
		||||
                    log.info("Assets updated.");
 | 
			
		||||
                } catch (InvalidQueryException e) {}
 | 
			
		||||
                } catch (InvalidQueryException e) {
 | 
			
		||||
                }
 | 
			
		||||
                log.info("Schema updated.");
 | 
			
		||||
                break;
 | 
			
		||||
            case "2.4.2":
 | 
			
		||||
@ -286,24 +276,25 @@ public class CassandraDatabaseUpgradeService implements DatabaseEntitiesUpgradeS
 | 
			
		||||
                    cluster.getSession().execute(updateAlarmTableStmt);
 | 
			
		||||
                    Thread.sleep(2500);
 | 
			
		||||
                    log.info("Alarms updated.");
 | 
			
		||||
                } catch (InvalidQueryException e) {}
 | 
			
		||||
                } catch (InvalidQueryException e) {
 | 
			
		||||
                }
 | 
			
		||||
                log.info("Schema updated.");
 | 
			
		||||
                break;
 | 
			
		||||
            case "2.4.3":
 | 
			
		||||
                log.info("Updating schema ...");
 | 
			
		||||
                String updateAttributeKvTableStmt = "alter table attributes_kv_cf add json_v text";
 | 
			
		||||
                try {
 | 
			
		||||
                    log.info("Updating attributes ...");
 | 
			
		||||
                    cluster.getSession().execute(updateAttributeKvTableStmt);
 | 
			
		||||
                    Thread.sleep(2500);
 | 
			
		||||
                    log.info("Attributes updated.");
 | 
			
		||||
                } catch (InvalidQueryException e) {
 | 
			
		||||
                }
 | 
			
		||||
                log.info("Schema updated.");
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void loadCql(Path cql) throws Exception {
 | 
			
		||||
        List<String> statements = new CQLStatementsParser(cql).getStatements();
 | 
			
		||||
        statements.forEach(statement -> {
 | 
			
		||||
            installCluster.getSession().execute(statement);
 | 
			
		||||
            try {
 | 
			
		||||
                Thread.sleep(2500);
 | 
			
		||||
            } catch (InterruptedException e) {}
 | 
			
		||||
        });
 | 
			
		||||
        Thread.sleep(5000);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,56 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2020 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * 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.thingsboard.server.service.install;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.core.exceptions.InvalidQueryException;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.context.annotation.Profile;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.server.dao.util.NoSqlTsDao;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
@NoSqlTsDao
 | 
			
		||||
@Profile("install")
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class CassandraTsDatabaseUpgradeService extends AbstractCassandraDatabaseUpgradeService implements DatabaseTsUpgradeService {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void upgradeDatabase(String fromVersion) throws Exception {
 | 
			
		||||
        switch (fromVersion) {
 | 
			
		||||
            case "2.4.3":
 | 
			
		||||
                log.info("Updating schema ...");
 | 
			
		||||
                String updateTsKvTableStmt = "alter table ts_kv_cf add json_v text";
 | 
			
		||||
                String updateTsKvLatestTableStmt = "alter table ts_kv_latest_cf add json_v text";
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    log.info("Updating ts ...");
 | 
			
		||||
                    cluster.getSession().execute(updateTsKvTableStmt);
 | 
			
		||||
                    Thread.sleep(2500);
 | 
			
		||||
                    log.info("Ts updated.");
 | 
			
		||||
                    log.info("Updating ts latest ...");
 | 
			
		||||
                    cluster.getSession().execute(updateTsKvLatestTableStmt);
 | 
			
		||||
                    Thread.sleep(2500);
 | 
			
		||||
                    log.info("Ts latest updated.");
 | 
			
		||||
                } catch (InvalidQueryException e) {
 | 
			
		||||
                }
 | 
			
		||||
                log.info("Schema updated.");
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -101,6 +101,9 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
 | 
			
		||||
                        executeDropStatement(conn, DROP_FUNCTION_CREATE_NEW_TS_KV_LATEST_TABLE);
 | 
			
		||||
                        executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST);
 | 
			
		||||
 | 
			
		||||
                        executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN json_v json;");
 | 
			
		||||
                        executeQuery(conn, "ALTER TABLE ts_kv_latest ADD COLUMN json_v json;");
 | 
			
		||||
 | 
			
		||||
                        log.info("schema timeseries updated!");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@ -207,6 +207,16 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
 | 
			
		||||
                    log.info("Schema updated.");
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case "2.4.3":
 | 
			
		||||
                try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
 | 
			
		||||
                    log.info("Updating schema ...");
 | 
			
		||||
                    try {
 | 
			
		||||
                        conn.createStatement().execute("ALTER TABLE attribute_kv ADD COLUMN json_v json;");
 | 
			
		||||
                    } catch (Exception e) {
 | 
			
		||||
                    }
 | 
			
		||||
                    log.info("Schema updated.");
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -104,6 +104,9 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
 | 
			
		||||
                        executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TENANT_TS_KV);
 | 
			
		||||
                        executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST);
 | 
			
		||||
 | 
			
		||||
                        executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN json_v json;");
 | 
			
		||||
                        executeQuery(conn, "ALTER TABLE ts_kv_latest ADD COLUMN json_v json;");
 | 
			
		||||
 | 
			
		||||
                        log.info("schema timeseries updated!");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user