Search in sources :

Example 71 with SQLWarning

use of java.sql.SQLWarning in project iaf by ibissource.

the class JdbcTestBase method prepareDatabase.

protected void prepareDatabase() throws Exception {
    connection = getConnection();
    dbmsSupport = dbmsSupportFactory.getDbmsSupport(dataSource);
    try (Connection connection = getConnection()) {
        if (dbmsSupport.isTablePresent(connection, "TEMP")) {
            JdbcUtil.executeStatement(connection, "DROP TABLE TEMP");
            SQLWarning warnings = connection.getWarnings();
            if (warnings != null) {
                log.warn(JdbcUtil.warningsToString(warnings));
            }
        }
        JdbcUtil.executeStatement(connection, "CREATE TABLE TEMP(TKEY " + dbmsSupport.getNumericKeyFieldType() + " PRIMARY KEY, TVARCHAR " + dbmsSupport.getTextFieldType() + "(100), TINT INT, TNUMBER NUMERIC(10,5), " + "TDATE DATE, TDATETIME " + dbmsSupport.getTimestampFieldType() + ", TBOOLEAN " + dbmsSupport.getBooleanFieldType() + ", " + "TCLOB " + dbmsSupport.getClobFieldType() + ", TBLOB " + dbmsSupport.getBlobFieldType() + ")");
        SQLWarning warnings = connection.getWarnings();
        if (warnings != null) {
            log.warn(JdbcUtil.warningsToString(warnings));
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 72 with SQLWarning

use of java.sql.SQLWarning in project sqlite-jna by gwenn.

the class JDBC method setup.

private static SQLWarning setup(org.sqlite.Conn conn, Properties info) throws ConnException {
    if (info == null) {
        return null;
    }
    final String encoding = info.getProperty(ENCODING.name);
    if (encoding != null && !encoding.isEmpty()) {
        Pragma pragma = new Pragma(new QualifiedName(null, "encoding"), LiteralExpr.string(encoding));
        conn.fastExec(pragma.toSql());
    }
    final String fks = info.getProperty(FOREIGN_KEYS.name);
    SQLWarning warnings = null;
    if ("on".equals(fks)) {
        if (!conn.enableForeignKeys(true)) {
            warnings = addWarning(warnings, new SQLWarning("cannot enable the enforcement of foreign key constraints.", null, ErrCodes.WRAPPER_SPECIFIC));
            SQLite.sqlite3_log(-1, "cannot enable the enforcement of foreign key constraints.");
        }
    } else if ("off".equals(fks)) {
        if (conn.enableForeignKeys(false)) {
            warnings = addWarning(warnings, new SQLWarning("cannot disable the enforcement of foreign key constraints.", null, ErrCodes.WRAPPER_SPECIFIC));
            SQLite.sqlite3_log(-1, "cannot disable the enforcement of foreign key constraints.");
        }
    }
    final String triggers = info.getProperty(ENABLE_TRIGGERS.name);
    if ("on".equals(triggers)) {
        if (!conn.enableTriggers(true)) {
            warnings = addWarning(warnings, new SQLWarning("cannot enable triggers.", null, ErrCodes.WRAPPER_SPECIFIC));
            SQLite.sqlite3_log(-1, "cannot enable triggers.");
        }
    } else if ("off".equals(fks)) {
        if (conn.enableTriggers(false)) {
            warnings = addWarning(warnings, new SQLWarning("cannot disable triggers.", null, ErrCodes.WRAPPER_SPECIFIC));
            SQLite.sqlite3_log(-1, "cannot disable triggers.");
        }
    }
    if ("on".equals(info.getProperty(ENABLE_LOAD_EXTENSION.name))) {
        conn.enableLoadExtension(true);
    }
    // disabled by default
    return warnings;
}
Also used : SQLWarning(java.sql.SQLWarning) QualifiedName(org.sqlite.parser.ast.QualifiedName) Pragma(org.sqlite.parser.ast.Pragma)

Example 73 with SQLWarning

use of java.sql.SQLWarning in project sqlite-jna by gwenn.

the class SqliteConnectionTest method testWarnings.

@Test(expected = SQLFeatureNotSupportedException.class)
public void testWarnings() throws Exception {
    assertNull(conn.getWarnings());
    try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM test_table", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
        SQLWarning warnings = conn.getWarnings();
        Set<String> msgs = new HashSet<>();
        assertEquals(ResultSet.TYPE_FORWARD_ONLY, stmt.getResultSetType());
        assertEquals(ResultSet.CONCUR_READ_ONLY, stmt.getResultSetConcurrency());
        assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, stmt.getResultSetHoldability());
        assertNotNull(warnings);
        while (warnings != null) {
            msgs.add(warnings.getMessage());
            warnings = warnings.getNextWarning();
        }
        assertEquals(new HashSet<>(Arrays.asList(EXPECTED_WARNING_MSGS)), msgs);
    }
}
Also used : SQLWarning(java.sql.SQLWarning) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 74 with SQLWarning

use of java.sql.SQLWarning in project sqlite-jna by gwenn.

the class ConnectionTest method addWarning.

@Test
public void addWarning() throws Exception {
    Connection conn = DriverManager.getConnection(JDBC.MEMORY);
    ((Conn) conn).addWarning(new SQLWarning("test"));
    ((Conn) conn).addWarning(null);
}
Also used : SQLWarning(java.sql.SQLWarning) Connection(java.sql.Connection) Test(org.junit.Test)

Example 75 with SQLWarning

use of java.sql.SQLWarning in project thingsboard by thingsboard.

the class SqlDatabaseUpgradeService method upgradeDatabase.

@Override
public void upgradeDatabase(String fromVersion) throws Exception {
    switch(fromVersion) {
        case "1.3.0":
            log.info("Updating schema ...");
            Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "1.3.1", SCHEMA_UPDATE_SQL);
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                loadSql(schemaUpdateFile, conn);
            }
            log.info("Schema updated.");
            break;
        case "1.3.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Dumping dashboards ...");
                Path dashboardsDump = SqlDbHelper.dumpTableIfExists(conn, DASHBOARD, new String[] { ID, TENANT_ID, CUSTOMER_ID, TITLE, SEARCH_TEXT, ASSIGNED_CUSTOMERS, CONFIGURATION }, new String[] { "", "", "", "", "", "", "" }, "tb-dashboards", true);
                log.info("Dashboards dumped.");
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "1.4.0", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
                log.info("Restoring dashboards ...");
                if (dashboardsDump != null) {
                    SqlDbHelper.loadTable(conn, DASHBOARD, new String[] { ID, TENANT_ID, TITLE, SEARCH_TEXT, CONFIGURATION }, dashboardsDump, true);
                    DatabaseHelper.upgradeTo40_assignDashboards(dashboardsDump, dashboardService, true);
                    Files.deleteIfExists(dashboardsDump);
                }
                log.info("Dashboards restored.");
            }
            break;
        case "1.4.0":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.0.0", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
            }
            break;
        case "2.0.0":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.1.1", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
            }
            break;
        case "2.1.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Dumping entity views ...");
                Path entityViewsDump = SqlDbHelper.dumpTableIfExists(conn, ENTITY_VIEWS, new String[] { ID, ENTITY_ID, ENTITY_TYPE, TENANT_ID, CUSTOMER_ID, TYPE, NAME, KEYS, START_TS, END_TS, SEARCH_TEXT, ADDITIONAL_INFO }, new String[] { "", "", "", "", "", "default", "", "", "0", "0", "", "" }, "tb-entity-views", true);
                log.info("Entity views dumped.");
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.1.2", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
                log.info("Restoring entity views ...");
                if (entityViewsDump != null) {
                    SqlDbHelper.loadTable(conn, ENTITY_VIEW, new String[] { ID, ENTITY_ID, ENTITY_TYPE, TENANT_ID, CUSTOMER_ID, TYPE, NAME, KEYS, START_TS, END_TS, SEARCH_TEXT, ADDITIONAL_INFO }, entityViewsDump, true);
                    Files.deleteIfExists(entityViewsDump);
                }
                log.info("Entity views restored.");
            }
            break;
        case "2.1.3":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.2.0", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
            }
            break;
        case "2.3.0":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.3.1", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
            }
            break;
        case "2.3.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.0", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE device ADD COLUMN label varchar(255)");
                } catch (Exception e) {
                }
                log.info("Schema updated.");
            }
            break;
        case "2.4.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE asset ADD COLUMN label varchar(255)");
                } catch (Exception e) {
                }
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.4.2", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE device ADD CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name)");
                } catch (Exception e) {
                }
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE device_credentials ADD CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id)");
                } catch (Exception e) {
                }
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE asset ADD CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name)");
                } catch (Exception e) {
                }
                log.info("Schema updated.");
            }
            break;
        case "2.4.2":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE alarm ADD COLUMN propagate_relation_types varchar");
                } catch (Exception e) {
                }
                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) {
                    if (e instanceof SQLSyntaxErrorException) {
                        try {
                            conn.createStatement().execute("ALTER TABLE attribute_kv ADD COLUMN json_v varchar(10000000);");
                        } catch (Exception e1) {
                        }
                    }
                }
                try {
                    conn.createStatement().execute("ALTER TABLE tenant ADD COLUMN isolated_tb_core boolean DEFAULT (false), ADD COLUMN isolated_tb_rule_engine boolean DEFAULT (false)");
                } catch (Exception e) {
                }
                try {
                    long ts = System.currentTimeMillis();
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE event ADD COLUMN ts bigint DEFAULT " + ts + ";");
                } catch (Exception e) {
                }
                log.info("Schema updated.");
            }
            break;
        case "3.0.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                if (isOldSchema(conn, 3000001)) {
                    String[] tables = new String[] { "admin_settings", "alarm", "asset", "audit_log", "attribute_kv", "component_descriptor", "customer", "dashboard", "device", "device_credentials", "event", "relation", "tb_user", "tenant", "user_credentials", "widget_type", "widgets_bundle", "rule_chain", "rule_node", "entity_view" };
                    schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.0.1", "schema_update_to_uuid.sql");
                    loadSql(schemaUpdateFile, conn);
                    conn.createStatement().execute("call drop_all_idx()");
                    log.info("Optimizing alarm relations...");
                    conn.createStatement().execute("DELETE from relation WHERE relation_type_group = 'ALARM' AND relation_type <> 'ALARM_ANY';");
                    conn.createStatement().execute("DELETE from relation WHERE relation_type_group = 'ALARM' AND relation_type = 'ALARM_ANY' " + "AND exists(SELECT * FROM alarm WHERE alarm.id = relation.to_id AND alarm.originator_id = relation.from_id)");
                    log.info("Alarm relations optimized.");
                    for (String table : tables) {
                        log.info("Updating table {}.", table);
                        Statement statement = conn.createStatement();
                        statement.execute("call update_" + table + "();");
                        SQLWarning warnings = statement.getWarnings();
                        if (warnings != null) {
                            log.info("{}", warnings.getMessage());
                            SQLWarning nextWarning = warnings.getNextWarning();
                            while (nextWarning != null) {
                                log.info("{}", nextWarning.getMessage());
                                nextWarning = nextWarning.getNextWarning();
                            }
                        }
                        conn.createStatement().execute("DROP PROCEDURE update_" + table);
                        log.info("Table {} updated.", table);
                    }
                    conn.createStatement().execute("call create_all_idx()");
                    conn.createStatement().execute("DROP PROCEDURE drop_all_idx");
                    conn.createStatement().execute("DROP PROCEDURE create_all_idx");
                    conn.createStatement().execute("DROP FUNCTION column_type_to_uuid");
                    log.info("Updating alarm relations...");
                    conn.createStatement().execute("UPDATE relation SET relation_type = 'ANY' WHERE relation_type_group = 'ALARM' AND relation_type = 'ALARM_ANY';");
                    log.info("Alarm relations updated.");
                    conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3001000;");
                    conn.createStatement().execute("VACUUM FULL");
                }
                log.info("Schema updated.");
            } catch (Exception e) {
                log.error("Failed updating schema!!!", e);
            }
            break;
        case "3.1.0":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.1.0", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Schema updated.");
            }
            break;
        case "3.1.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                if (isOldSchema(conn, 3001000)) {
                    try {
                        conn.createStatement().execute("ALTER TABLE device ADD COLUMN device_profile_id uuid, ADD COLUMN device_data jsonb");
                    } catch (Exception e) {
                    }
                    try {
                        conn.createStatement().execute("ALTER TABLE tenant ADD COLUMN tenant_profile_id uuid");
                    } catch (Exception e) {
                    }
                    try {
                        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS rule_node_state (" + " id uuid NOT NULL CONSTRAINT rule_node_state_pkey PRIMARY KEY," + " created_time bigint NOT NULL," + " rule_node_id uuid NOT NULL," + " entity_type varchar(32) NOT NULL," + " entity_id uuid NOT NULL," + " state_data varchar(16384) NOT NULL," + " CONSTRAINT rule_node_state_unq_key UNIQUE (rule_node_id, entity_id)," + " CONSTRAINT fk_rule_node_state_node_id FOREIGN KEY (rule_node_id) REFERENCES rule_node(id) ON DELETE CASCADE)");
                    } catch (Exception e) {
                    }
                    try {
                        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS api_usage_state (" + " id uuid NOT NULL CONSTRAINT usage_record_pkey PRIMARY KEY," + " created_time bigint NOT NULL," + " tenant_id uuid," + " entity_type varchar(32)," + " entity_id uuid," + " transport varchar(32)," + " db_storage varchar(32)," + " re_exec varchar(32)," + " js_exec varchar(32)," + " email_exec varchar(32)," + " sms_exec varchar(32)," + " CONSTRAINT api_usage_state_unq_key UNIQUE (tenant_id, entity_id)\n" + ");");
                    } catch (Exception e) {
                    }
                    schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.1.1", "schema_update_before.sql");
                    loadSql(schemaUpdateFile, conn);
                    log.info("Creating default tenant profiles...");
                    systemDataLoaderService.createDefaultTenantProfiles();
                    log.info("Updating tenant profiles...");
                    conn.createStatement().execute("call update_tenant_profiles()");
                    log.info("Creating default device profiles...");
                    PageLink pageLink = new PageLink(100);
                    PageData<Tenant> pageData;
                    do {
                        pageData = tenantService.findTenants(pageLink);
                        for (Tenant tenant : pageData.getData()) {
                            try {
                                apiUsageStateService.createDefaultApiUsageState(tenant.getId(), null);
                            } catch (Exception e) {
                            }
                            List<EntitySubtype> deviceTypes = deviceService.findDeviceTypesByTenantId(tenant.getId()).get();
                            try {
                                deviceProfileService.createDefaultDeviceProfile(tenant.getId());
                            } catch (Exception e) {
                            }
                            for (EntitySubtype deviceType : deviceTypes) {
                                try {
                                    deviceProfileService.findOrCreateDeviceProfile(tenant.getId(), deviceType.getType());
                                } catch (Exception e) {
                                }
                            }
                        }
                        pageLink = pageLink.nextPageLink();
                    } while (pageData.hasNext());
                    log.info("Updating device profiles...");
                    conn.createStatement().execute("call update_device_profiles()");
                    schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.1.1", "schema_update_after.sql");
                    loadSql(schemaUpdateFile, conn);
                    conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3002000;");
                }
                log.info("Schema updated.");
            } catch (Exception e) {
                log.error("Failed updating schema!!!", e);
            }
            break;
        case "3.2.0":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                try {
                    conn.createStatement().execute("CREATE INDEX IF NOT EXISTS idx_device_device_profile_id ON device(tenant_id, device_profile_id);");
                    conn.createStatement().execute("ALTER TABLE dashboard ALTER COLUMN configuration TYPE varchar;");
                    conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3002001;");
                } catch (Exception e) {
                    log.error("Failed updating schema!!!", e);
                }
                log.info("Schema updated.");
            }
            break;
        case "3.2.1":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                conn.createStatement().execute("CREATE INDEX IF NOT EXISTS idx_audit_log_tenant_id_and_created_time ON audit_log(tenant_id, created_time);");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.2.1", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3002002;");
                log.info("Schema updated.");
            } catch (Exception e) {
                log.error("Failed updating schema!!!", e);
            }
            break;
        case "3.2.2":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE rule_chain ADD COLUMN type varchar(255) DEFAULT 'CORE'");
                } catch (Exception ignored) {
                }
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.2.2", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                log.info("Load Edge TTL functions ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.2.2", "schema_update_ttl.sql");
                loadSql(schemaUpdateFile, conn);
                log.info("Edge TTL functions successfully loaded!");
                log.info("Updating indexes and TTL procedure for event table...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.2.2", "schema_update_event.sql");
                loadSql(schemaUpdateFile, conn);
                log.info("Updating schema settings...");
                conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3003000;");
                log.info("Schema updated.");
            } catch (Exception e) {
                log.error("Failed updating schema!!!", e);
            }
            break;
        case "3.3.2":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.2", SCHEMA_UPDATE_SQL);
                loadSql(schemaUpdateFile, conn);
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE alarm ADD COLUMN propagate_to_owner boolean DEFAULT false;");
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE alarm ADD COLUMN propagate_to_tenant boolean DEFAULT false;");
                } catch (Exception ignored) {
                }
                try {
                    conn.createStatement().execute("insert into entity_alarm(tenant_id, entity_id, created_time, alarm_type, customer_id, alarm_id)" + " select tenant_id, originator_id, created_time, type, customer_id, id from alarm ON CONFLICT DO NOTHING;");
                    conn.createStatement().execute("insert into entity_alarm(tenant_id, entity_id, created_time, alarm_type, customer_id, alarm_id)" + " select a.tenant_id, r.from_id, created_time, type, customer_id, id" + " from alarm a inner join relation r on r.relation_type_group = 'ALARM' and r.relation_type = 'ANY' and a.id = r.to_id ON CONFLICT DO NOTHING;");
                    conn.createStatement().execute("delete from relation r where r.relation_type_group = 'ALARM';");
                } catch (Exception e) {
                    log.error("Failed to update alarm relations!!!", e);
                }
                log.info("Updating lwm2m device profiles ...");
                try {
                    schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.2", "schema_update_lwm2m_bootstrap.sql");
                    loadSql(schemaUpdateFile, conn);
                    log.info("Updating server`s public key from HexDec to Base64 in profile for LWM2M...");
                    conn.createStatement().execute("call update_profile_bootstrap();");
                    log.info("Server`s public key from HexDec to Base64 in profile for LWM2M updated.");
                    log.info("Updating client`s public key and secret key from HexDec to Base64 for LWM2M...");
                    conn.createStatement().execute("call update_device_credentials_to_base64_and_bootstrap();");
                    log.info("Client`s public key and secret key from HexDec to Base64 for LWM2M updated.");
                } catch (Exception e) {
                    log.error("Failed to update lwm2m profiles!!!", e);
                }
                log.info("Updating schema settings...");
                conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3003003;");
                log.info("Schema updated.");
            } catch (Exception e) {
                log.error("Failed updating schema!!!", e);
            }
            break;
        case "3.3.3":
            try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
                log.info("Updating schema ...");
                try {
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE edge DROP COLUMN edge_license_key;");
                    // NOSONAR, ignoring because method used to execute thingsboard database upgrade script
                    conn.createStatement().execute("ALTER TABLE edge DROP COLUMN cloud_endpoint;");
                } catch (Exception ignored) {
                }
                log.info("Updating TTL cleanup procedure for the event table...");
                schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.3", "schema_event_ttl_procedure.sql");
                loadSql(schemaUpdateFile, conn);
                log.info("Updating schema settings...");
                conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3003004;");
                log.info("Schema updated.");
            } catch (Exception e) {
                log.error("Failed updating schema!!!", e);
            }
            break;
        default:
            throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
    }
}
Also used : Path(java.nio.file.Path) SQLWarning(java.sql.SQLWarning) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLException(java.sql.SQLException) Tenant(org.thingsboard.server.common.data.Tenant) PageLink(org.thingsboard.server.common.data.page.PageLink)

Aggregations

SQLWarning (java.sql.SQLWarning)153 SQLException (java.sql.SQLException)49 Statement (java.sql.Statement)37 PreparedStatement (java.sql.PreparedStatement)30 Connection (java.sql.Connection)26 ResultSet (java.sql.ResultSet)23 Test (org.testng.annotations.Test)19 Test (org.junit.Test)17 BaseTest (util.BaseTest)15 CallableStatement (java.sql.CallableStatement)13 ArrayList (java.util.ArrayList)9 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)8 ResultSetMetaData (java.sql.ResultSetMetaData)6 Expectations (org.jmock.Expectations)6 IOException (java.io.IOException)4 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)3 PrestoWarning (com.facebook.presto.spi.PrestoWarning)3 Properties (java.util.Properties)3 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)3