automaticaly set schema version based on app version
This commit is contained in:
parent
fdbe59c610
commit
cbb9d621a7
@ -15,8 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.service.install;
|
package org.thingsboard.server.service.install;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.info.BuildProperties;
|
||||||
import org.springframework.context.annotation.Profile;
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.thingsboard.server.dao.util.SqlTsDao;
|
import org.thingsboard.server.dao.util.SqlTsDao;
|
||||||
|
|
||||||
@ -25,8 +27,11 @@ import org.thingsboard.server.dao.util.SqlTsDao;
|
|||||||
@Profile("install")
|
@Profile("install")
|
||||||
public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService implements TsDatabaseSchemaService {
|
public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService implements TsDatabaseSchemaService {
|
||||||
|
|
||||||
@Value("${sql.postgres.ts_key_value_partitioning:MONTHS}")
|
@Autowired
|
||||||
private String partitionType;
|
private BuildProperties buildProperties;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
public SqlTsDatabaseSchemaService() {
|
public SqlTsDatabaseSchemaService() {
|
||||||
super("schema-ts-psql.sql", null);
|
super("schema-ts-psql.sql", null);
|
||||||
@ -36,5 +41,21 @@ public class SqlTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaService
|
|||||||
public void createDatabaseSchema() throws Exception {
|
public void createDatabaseSchema() throws Exception {
|
||||||
super.createDatabaseSchema();
|
super.createDatabaseSchema();
|
||||||
executeQuery("CREATE TABLE IF NOT EXISTS ts_kv_indefinite PARTITION OF ts_kv DEFAULT;");
|
executeQuery("CREATE TABLE IF NOT EXISTS ts_kv_indefinite PARTITION OF ts_kv DEFAULT;");
|
||||||
|
|
||||||
|
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() + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -20,18 +20,6 @@ CREATE TABLE IF NOT EXISTS tb_schema_settings
|
|||||||
CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version)
|
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 (
|
CREATE TABLE IF NOT EXISTS admin_settings (
|
||||||
id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
|
id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
|
||||||
tenant_id uuid NOT NULL,
|
tenant_id uuid NOT NULL,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user