Merge pull request #11877 from YevhenBondarenko/fix/install-schema-version
automaticaly set schema version based on app version
This commit is contained in:
commit
e8c3f94e91
@ -170,6 +170,7 @@ public class ThingsboardInstallService {
|
||||
log.info("Installing DataBase schema for entities...");
|
||||
|
||||
entityDatabaseSchemaService.createDatabaseSchema();
|
||||
entityDatabaseSchemaService.createSchemaVersion();
|
||||
|
||||
entityDatabaseSchemaService.createOrUpdateViewsAndFunctions();
|
||||
entityDatabaseSchemaService.createOrUpdateDeviceInfoView(persistToTelemetry);
|
||||
|
||||
@ -23,4 +23,6 @@ public interface EntityDatabaseSchemaService extends DatabaseSchemaService {
|
||||
|
||||
void createCustomerTitleUniqueConstraintIfNotExists();
|
||||
|
||||
void createSchemaVersion();
|
||||
|
||||
}
|
||||
|
||||
@ -16,7 +16,10 @@
|
||||
package org.thingsboard.server.service.install;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.info.BuildProperties;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@ -29,6 +32,11 @@ public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaSer
|
||||
public static final String SCHEMA_ENTITIES_IDX_PSQL_ADDON_SQL = "schema-entities-idx-psql-addon.sql";
|
||||
public static final String SCHEMA_VIEWS_AND_FUNCTIONS_SQL = "schema-views-and-functions.sql";
|
||||
|
||||
@Autowired
|
||||
private BuildProperties buildProperties;
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public SqlEntityDatabaseSchemaService() {
|
||||
super(SCHEMA_ENTITIES_SQL, SCHEMA_ENTITIES_IDX_SQL);
|
||||
}
|
||||
@ -59,4 +67,26 @@ public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaSer
|
||||
"ALTER TABLE customer ADD CONSTRAINT customer_title_unq_key UNIQUE(tenant_id, title); END IF; END; $$;",
|
||||
"create 'customer_title_unq_key' constraint if it doesn't already exist!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createSchemaVersion() {
|
||||
try {
|
||||
Long schemaVersion = jdbcTemplate.queryForList("SELECT schema_version FROM tb_schema_settings", Long.class).stream().findFirst().orElse(null);
|
||||
if (schemaVersion == null) {
|
||||
jdbcTemplate.execute("INSERT INTO tb_schema_settings (schema_version) VALUES (" + getSchemaVersion() + ")");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to create schema version [{}]!", buildProperties.getVersion(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private int getSchemaVersion() {
|
||||
String[] versionParts = buildProperties.getVersion().replaceAll("[^\\d.]", "").split("\\.");
|
||||
|
||||
int major = Integer.parseInt(versionParts[0]);
|
||||
int minor = Integer.parseInt(versionParts[1]);
|
||||
int patch = versionParts.length > 2 ? Integer.parseInt(versionParts[2]) : 0;
|
||||
|
||||
return major * 1000000 + minor * 1000 + patch;
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.thingsboard.server.service.install;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.dao.util.SqlTsDao;
|
||||
@ -25,9 +24,6 @@ import org.thingsboard.server.dao.util.SqlTsDao;
|
||||
@Profile("install")
|
||||
public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService implements TsDatabaseSchemaService {
|
||||
|
||||
@Value("${sql.postgres.ts_key_value_partitioning:MONTHS}")
|
||||
private String partitionType;
|
||||
|
||||
public SqlTsDatabaseSchemaService() {
|
||||
super("schema-ts-psql.sql", null);
|
||||
}
|
||||
|
||||
@ -20,18 +20,6 @@ CREATE TABLE IF NOT EXISTS tb_schema_settings
|
||||
CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version)
|
||||
);
|
||||
|
||||
CREATE OR REPLACE PROCEDURE insert_tb_schema_settings()
|
||||
LANGUAGE plpgsql AS
|
||||
$$
|
||||
BEGIN
|
||||
IF (SELECT COUNT(*) FROM tb_schema_settings) = 0 THEN
|
||||
INSERT INTO tb_schema_settings (schema_version) VALUES (3006004);
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
call insert_tb_schema_settings();
|
||||
|
||||
CREATE TABLE IF NOT EXISTS admin_settings (
|
||||
id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
|
||||
tenant_id uuid NOT NULL,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user