diff --git a/application/src/main/data/upgrade/3.5.0/schema_update.sql b/application/src/main/data/upgrade/3.5.0/schema_update.sql deleted file mode 100644 index 2e2ca1d347..0000000000 --- a/application/src/main/data/upgrade/3.5.0/schema_update.sql +++ /dev/null @@ -1,23 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - --- FIX DASHBOARD TEMPLATES AFTER ANGULAR MIGRATION TO VER.15 - -UPDATE dashboard SET configuration = REPLACE(configuration, 'mat-button mat-icon-button', 'mat-icon-button') - WHERE configuration like '%mat-button mat-icon-button%'; - -UPDATE widget_type SET descriptor = REPLACE(descriptor, 'mat-button mat-icon-button', 'mat-icon-button') - WHERE descriptor like '%mat-button mat-icon-button%'; diff --git a/application/src/main/data/upgrade/3.5.1/schema_update.sql b/application/src/main/data/upgrade/3.5.1/schema_update.sql deleted file mode 100644 index b18abb4cfc..0000000000 --- a/application/src/main/data/upgrade/3.5.1/schema_update.sql +++ /dev/null @@ -1,196 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - -ALTER TABLE notification_rule ADD COLUMN IF NOT EXISTS enabled BOOLEAN NOT NULL DEFAULT true; - --- NOTIFICATION CONFIGS VERSION CONTROL START - -ALTER TABLE notification_template - ADD COLUMN IF NOT EXISTS external_id UUID; -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'uq_notification_template_external_id') THEN - ALTER TABLE notification_template ADD CONSTRAINT uq_notification_template_external_id UNIQUE (tenant_id, external_id); - END IF; - END; -$$; - -ALTER TABLE notification_target - ADD COLUMN IF NOT EXISTS external_id UUID; -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'uq_notification_target_external_id') THEN - ALTER TABLE notification_target ADD CONSTRAINT uq_notification_target_external_id UNIQUE (tenant_id, external_id); - END IF; - END; -$$; - -ALTER TABLE notification_rule - ADD COLUMN IF NOT EXISTS external_id UUID; -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'uq_notification_rule_external_id') THEN - ALTER TABLE notification_rule ADD CONSTRAINT uq_notification_rule_external_id UNIQUE (tenant_id, external_id); - END IF; - END; -$$; - --- NOTIFICATION CONFIGS VERSION CONTROL END - --- EDGE EVENTS MIGRATION START -DO -$$ - DECLARE table_partition RECORD; - BEGIN - -- in case of running the upgrade script a second time: - IF NOT (SELECT exists(SELECT FROM pg_tables WHERE tablename = 'old_edge_event')) THEN - ALTER TABLE edge_event RENAME TO old_edge_event; - CREATE INDEX IF NOT EXISTS idx_old_edge_event_created_time_tmp ON old_edge_event(created_time); - ALTER INDEX IF EXISTS idx_edge_event_tenant_id_and_created_time RENAME TO idx_old_edge_event_tenant_id_and_created_time; - - FOR table_partition IN SELECT tablename AS name, split_part(tablename, '_', 3) AS partition_ts - FROM pg_tables WHERE tablename LIKE 'edge_event_%' - LOOP - EXECUTE format('ALTER TABLE %s RENAME TO old_edge_event_%s', table_partition.name, table_partition.partition_ts); - END LOOP; - ELSE - RAISE NOTICE 'Table old_edge_event already exists, leaving as is'; - END IF; - END; -$$; - -CREATE TABLE IF NOT EXISTS edge_event ( - seq_id INT GENERATED ALWAYS AS IDENTITY, - id uuid NOT NULL, - created_time bigint NOT NULL, - edge_id uuid, - edge_event_type varchar(255), - edge_event_uid varchar(255), - entity_id uuid, - edge_event_action varchar(255), - body varchar(10000000), - tenant_id uuid, - ts bigint NOT NULL -) PARTITION BY RANGE (created_time); -CREATE INDEX IF NOT EXISTS idx_edge_event_tenant_id_and_created_time ON edge_event(tenant_id, created_time DESC); -CREATE INDEX IF NOT EXISTS idx_edge_event_id ON edge_event(id); -ALTER TABLE IF EXISTS edge_event ALTER COLUMN seq_id SET CYCLE; - -CREATE OR REPLACE PROCEDURE migrate_edge_event(IN start_time_ms BIGINT, IN end_time_ms BIGINT, IN partition_size_ms BIGINT) - LANGUAGE plpgsql AS -$$ -DECLARE - p RECORD; - partition_end_ts BIGINT; -BEGIN - IF (SELECT exists(SELECT FROM pg_tables WHERE tablename = 'old_edge_event')) THEN - FOR p IN SELECT DISTINCT (created_time - created_time % partition_size_ms) AS partition_ts FROM old_edge_event - WHERE created_time >= start_time_ms AND created_time < end_time_ms - LOOP - partition_end_ts = p.partition_ts + partition_size_ms; - RAISE NOTICE '[edge_event] Partition to create : [%-%]', p.partition_ts, partition_end_ts; - EXECUTE format('CREATE TABLE IF NOT EXISTS edge_event_%s PARTITION OF edge_event ' || - 'FOR VALUES FROM ( %s ) TO ( %s )', p.partition_ts, p.partition_ts, partition_end_ts); - END LOOP; - INSERT INTO edge_event (id, created_time, edge_id, edge_event_type, edge_event_uid, entity_id, edge_event_action, body, tenant_id, ts) - SELECT id, created_time, edge_id, edge_event_type, edge_event_uid, entity_id, edge_event_action, body, tenant_id, ts - FROM old_edge_event - WHERE created_time >= start_time_ms AND created_time < end_time_ms; - ELSE - RAISE NOTICE 'Table old_edge_event does not exists, skipping migration'; - END IF; -END; -$$; --- EDGE EVENTS MIGRATION END - -ALTER TABLE resource - ADD COLUMN IF NOT EXISTS etag varchar; - -UPDATE resource - SET etag = encode(sha256(decode(resource.data, 'base64')),'hex') WHERE resource.data is not null; - -ALTER TABLE notification_request ALTER COLUMN info SET DATA TYPE varchar(1000000); - -DELETE FROM alarm WHERE tenant_id NOT IN (SELECT id FROM tenant); - -CREATE TABLE IF NOT EXISTS alarm_types ( - tenant_id uuid NOT NULL, - type varchar(255) NOT NULL, - CONSTRAINT tenant_id_type_unq_key UNIQUE (tenant_id, type), - CONSTRAINT fk_entity_tenant_id FOREIGN KEY (tenant_id) REFERENCES tenant(id) ON DELETE CASCADE -); - -INSERT INTO alarm_types (tenant_id, type) SELECT DISTINCT tenant_id, type FROM alarm ON CONFLICT (tenant_id, type) DO NOTHING; - -ALTER TABLE widgets_bundle ALTER COLUMN description SET DATA TYPE varchar(1024); -ALTER TABLE widget_type ALTER COLUMN description SET DATA TYPE varchar(1024); - -ALTER TABLE widget_type - ADD COLUMN IF NOT EXISTS fqn varchar(512); -ALTER TABLE widget_type - ADD COLUMN IF NOT EXISTS deprecated boolean NOT NULL DEFAULT false; -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'uq_widget_type_fqn') THEN - UPDATE widget_type SET fqn = concat(widget_type.bundle_alias, '.', widget_type.alias); - ALTER TABLE widget_type ADD CONSTRAINT uq_widget_type_fqn UNIQUE (tenant_id, fqn); - ALTER TABLE widget_type DROP COLUMN IF EXISTS alias; - END IF; - END; -$$; - -ALTER TABLE widget_type - ADD COLUMN IF NOT EXISTS external_id UUID; -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'widget_type_external_id_unq_key') THEN - ALTER TABLE widget_type ADD CONSTRAINT widget_type_external_id_unq_key UNIQUE (tenant_id, external_id); - END IF; - END; -$$; - -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'uq_widgets_bundle_alias') THEN - ALTER TABLE widgets_bundle ADD CONSTRAINT uq_widgets_bundle_alias UNIQUE (tenant_id, alias); - END IF; - END; -$$; - -CREATE TABLE IF NOT EXISTS widgets_bundle_widget ( - widgets_bundle_id uuid NOT NULL, - widget_type_id uuid NOT NULL, - widget_type_order int NOT NULL DEFAULT 0, - CONSTRAINT widgets_bundle_widget_pkey PRIMARY KEY (widgets_bundle_id, widget_type_id), - CONSTRAINT fk_widgets_bundle FOREIGN KEY (widgets_bundle_id) REFERENCES widgets_bundle(id) ON DELETE CASCADE, - CONSTRAINT fk_widget_type FOREIGN KEY (widget_type_id) REFERENCES widget_type(id) ON DELETE CASCADE -); - -DO -$$ - BEGIN - IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'widget_type' and column_name='bundle_alias') THEN - INSERT INTO widgets_bundle_widget SELECT wb.id as widgets_bundle_id, wt.id as widget_type_id from widget_type wt left join widgets_bundle wb ON wt.bundle_alias = wb.alias AND wt.tenant_id = wb.tenant_id ON CONFLICT (widgets_bundle_id, widget_type_id) DO NOTHING; - ALTER TABLE widget_type DROP COLUMN IF EXISTS bundle_alias; - END IF; - END; -$$; diff --git a/application/src/main/data/upgrade/3.6.0/schema_update.sql b/application/src/main/data/upgrade/3.6.0/schema_update.sql deleted file mode 100644 index d2af8abc78..0000000000 --- a/application/src/main/data/upgrade/3.6.0/schema_update.sql +++ /dev/null @@ -1,36 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - -ALTER TABLE widget_type - ADD COLUMN IF NOT EXISTS tags text[]; - -ALTER TABLE widgets_bundle - ADD COLUMN IF NOT EXISTS widgets_bundle_order int; - -ALTER TABLE api_usage_state ADD COLUMN IF NOT EXISTS tbel_exec varchar(32); -UPDATE api_usage_state SET tbel_exec = js_exec WHERE tbel_exec IS NULL; - -ALTER TABLE notification DROP CONSTRAINT IF EXISTS fk_notification_request_id; -ALTER TABLE notification DROP CONSTRAINT IF EXISTS fk_notification_recipient_id; -CREATE INDEX IF NOT EXISTS idx_notification_notification_request_id ON notification(request_id); -CREATE INDEX IF NOT EXISTS idx_notification_request_tenant_id ON notification_request(tenant_id); - --- DELETE invalid records from M:N widgets_bundle_widget table caused by the bug in previous upgrade script; -DELETE -FROM widgets_bundle_widget wbw -WHERE (SELECT tenant_id FROM widgets_bundle wb WHERE wb.id = wbw.widgets_bundle_id) != - (SELECT tenant_id FROM widget_type wt WHERE wt.id = wbw.widget_type_id); - diff --git a/application/src/main/data/upgrade/3.6.1/save_attributes_node_update.sql b/application/src/main/data/upgrade/3.6.1/save_attributes_node_update.sql deleted file mode 100644 index 7774fefbe2..0000000000 --- a/application/src/main/data/upgrade/3.6.1/save_attributes_node_update.sql +++ /dev/null @@ -1,26 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - -UPDATE rule_node SET - configuration = (configuration::jsonb || jsonb_build_object( - 'notifyDevice', - CASE WHEN configuration::jsonb ->> 'notifyDevice' = 'false' THEN false ELSE true END, - 'sendAttributesUpdatedNotification', - CASE WHEN configuration::jsonb ->> 'sendAttributesUpdatedNotification' = 'true' THEN true ELSE false END, - 'updateAttributesOnlyOnValueChange', - CASE WHEN configuration::jsonb ->> 'updateAttributesOnlyOnValueChange' = 'false' THEN false ELSE true END)::jsonb)::varchar, - configuration_version = 2 -WHERE type = 'org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode' AND configuration_version = 1; diff --git a/application/src/main/data/upgrade/3.6.1/schema_update.sql b/application/src/main/data/upgrade/3.6.1/schema_update.sql deleted file mode 100644 index 8285d28831..0000000000 --- a/application/src/main/data/upgrade/3.6.1/schema_update.sql +++ /dev/null @@ -1,59 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - --- RESOURCES UPDATE START - -ALTER TABLE resource ADD COLUMN IF NOT EXISTS descriptor varchar; -ALTER TABLE resource ADD COLUMN IF NOT EXISTS preview bytea; -ALTER TABLE resource ADD COLUMN IF NOT EXISTS external_id uuid; -ALTER TABLE resource ADD COLUMN IF NOT EXISTS is_public boolean default true; -ALTER TABLE resource ADD COLUMN IF NOT EXISTS public_resource_key varchar(32) unique; - -CREATE INDEX IF NOT EXISTS idx_resource_etag ON resource(tenant_id, etag); -CREATE INDEX IF NOT EXISTS idx_resource_type_public_resource_key ON resource(resource_type, public_resource_key); - -CREATE OR REPLACE FUNCTION generate_resource_public_key() -RETURNS text AS $$ -DECLARE - chars text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - result text := ''; -BEGIN - FOR i IN 1..32 LOOP - result := result || substr(chars, floor(random()*62)::int + 1, 1); - END LOOP; - RETURN result; -END; -$$ LANGUAGE plpgsql; - -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'resource' AND column_name = 'data' AND data_type = 'bytea') THEN - ALTER TABLE resource RENAME COLUMN data TO base64_data; - ALTER TABLE resource ADD COLUMN data bytea; - UPDATE resource SET data = decode(base64_data, 'base64') WHERE base64_data IS NOT NULL; - ALTER TABLE resource DROP COLUMN base64_data; - ELSE - UPDATE resource SET public_resource_key = generate_resource_public_key() WHERE resource_type = 'IMAGE' AND public_resource_key IS NULL; - END IF; - END; -$$; - -DROP FUNCTION generate_resource_public_key; - --- RESOURCES UPDATE END - -CREATE INDEX IF NOT EXISTS idx_edge_event_tenant_id_edge_id_created_time ON edge_event(tenant_id, edge_id, created_time DESC); \ No newline at end of file diff --git a/application/src/main/data/upgrade/3.6.2/schema_update.sql b/application/src/main/data/upgrade/3.6.2/schema_update.sql deleted file mode 100644 index c2df4f35a2..0000000000 --- a/application/src/main/data/upgrade/3.6.2/schema_update.sql +++ /dev/null @@ -1,43 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - --- RULE NODE INDEXES UPDATE START - -DROP INDEX IF EXISTS idx_rule_node_type; -DROP INDEX IF EXISTS idx_rule_node_type_configuration_version; -CREATE INDEX IF NOT EXISTS idx_rule_node_type_id_configuration_version ON rule_node(type, id, configuration_version); - --- RULE NODE INDEXES UPDATE END - --- RULE NODE QUEUE UPDATE START - -ALTER TABLE rule_node ADD COLUMN IF NOT EXISTS queue_name varchar(255); -ALTER TABLE component_descriptor ADD COLUMN IF NOT EXISTS has_queue_name boolean DEFAULT false; - --- RULE NODE QUEUE UPDATE END - - -DO -$$ - BEGIN - IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'user_settings' AND column_name = 'settings' AND data_type = 'jsonb') THEN - ALTER TABLE user_settings RENAME COLUMN settings to old_settings; - ALTER TABLE user_settings ADD COLUMN settings jsonb; - UPDATE user_settings SET settings = old_settings::jsonb WHERE old_settings IS NOT NULL; - ALTER TABLE user_settings DROP COLUMN old_settings; - END IF; - END; -$$; diff --git a/application/src/main/data/upgrade/3.6.3/schema_update.sql b/application/src/main/data/upgrade/3.6.3/schema_update.sql deleted file mode 100644 index 3e0fdc3b4b..0000000000 --- a/application/src/main/data/upgrade/3.6.3/schema_update.sql +++ /dev/null @@ -1,27 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - --- NOTIFICATIONS UPDATE START - -ALTER TABLE notification ADD COLUMN IF NOT EXISTS delivery_method VARCHAR(50) NOT NULL default 'WEB'; - -DROP INDEX IF EXISTS idx_notification_recipient_id_created_time; -DROP INDEX IF EXISTS idx_notification_recipient_id_unread; - -CREATE INDEX IF NOT EXISTS idx_notification_delivery_method_recipient_id_created_time ON notification(delivery_method, recipient_id, created_time DESC); -CREATE INDEX IF NOT EXISTS idx_notification_delivery_method_recipient_id_unread ON notification(delivery_method, recipient_id) WHERE status <> 'READ'; - --- NOTIFICATIONS UPDATE END diff --git a/application/src/main/data/upgrade/3.6.4/schema_update.sql b/application/src/main/data/upgrade/3.6.4/schema_update.sql deleted file mode 100644 index 6fb1e35156..0000000000 --- a/application/src/main/data/upgrade/3.6.4/schema_update.sql +++ /dev/null @@ -1,162 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - --- UPDATE PUBLIC CUSTOMERS START - -DO -$$ - BEGIN - IF NOT EXISTS ( - SELECT FROM information_schema.columns - WHERE table_name = 'customer' AND column_name = 'is_public' - ) THEN - ALTER TABLE customer ADD COLUMN is_public boolean DEFAULT false; - UPDATE customer SET is_public = true WHERE title = 'Public'; - END IF; - END; -$$; - --- UPDATE PUBLIC CUSTOMERS END - --- create new attribute_kv table schema -DO -$$ - BEGIN - -- in case of running the upgrade script a second time: - IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'attribute_kv' and column_name='entity_type') THEN - DROP VIEW IF EXISTS device_info_view; - DROP VIEW IF EXISTS device_info_active_attribute_view; - ALTER INDEX IF EXISTS idx_attribute_kv_by_key_and_last_update_ts RENAME TO idx_attribute_kv_by_key_and_last_update_ts_old; - IF EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'attribute_kv_pkey') THEN - ALTER TABLE attribute_kv RENAME CONSTRAINT attribute_kv_pkey TO attribute_kv_pkey_old; - END IF; - ALTER TABLE attribute_kv RENAME TO attribute_kv_old; - CREATE TABLE IF NOT EXISTS attribute_kv - ( - entity_id uuid, - attribute_type int, - attribute_key int, - bool_v boolean, - str_v varchar(10000000), - long_v bigint, - dbl_v double precision, - json_v json, - last_update_ts bigint, - CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_id, attribute_type, attribute_key) - ); - END IF; - END; -$$; - --- rename ts_kv_dictionary table to key_dictionary or create table if not exists -DO -$$ - BEGIN - IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'ts_kv_dictionary') THEN - ALTER TABLE ts_kv_dictionary RENAME CONSTRAINT ts_key_id_pkey TO key_dictionary_id_pkey; - ALTER TABLE ts_kv_dictionary RENAME TO key_dictionary; - ELSE CREATE TABLE IF NOT EXISTS key_dictionary( - key varchar(255) NOT NULL, - key_id serial UNIQUE, - CONSTRAINT key_dictionary_id_pkey PRIMARY KEY (key) - ); - END IF; - END; -$$; - --- insert keys into key_dictionary -DO -$$ - BEGIN - IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'attribute_kv_old') THEN - INSERT INTO key_dictionary(key) SELECT DISTINCT attribute_key FROM attribute_kv_old ON CONFLICT DO NOTHING; - END IF; - END; -$$; - --- migrate attributes from attribute_kv_old to attribute_kv -DO -$$ -DECLARE - row_num_old integer; - row_num integer; -BEGIN - IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'attribute_kv_old') THEN - INSERT INTO attribute_kv(entity_id, attribute_type, attribute_key, bool_v, str_v, long_v, dbl_v, json_v, last_update_ts) - SELECT a.entity_id, CASE - WHEN a.attribute_type = 'CLIENT_SCOPE' THEN 1 - WHEN a.attribute_type = 'SERVER_SCOPE' THEN 2 - WHEN a.attribute_type = 'SHARED_SCOPE' THEN 3 - ELSE 0 - END, - k.key_id, a.bool_v, a.str_v, a.long_v, a.dbl_v, a.json_v, a.last_update_ts - FROM attribute_kv_old a INNER JOIN key_dictionary k ON (a.attribute_key = k.key); - SELECT COUNT(*) INTO row_num_old FROM attribute_kv_old; - SELECT COUNT(*) INTO row_num FROM attribute_kv; - RAISE NOTICE 'Migrated % of % rows', row_num, row_num_old; - DROP TABLE IF EXISTS attribute_kv_old; - CREATE INDEX IF NOT EXISTS idx_attribute_kv_by_key_and_last_update_ts ON attribute_kv(entity_id, attribute_key, last_update_ts desc); - END IF; -EXCEPTION - WHEN others THEN - ROLLBACK; - RAISE EXCEPTION 'Error during COPY: %', SQLERRM; -END -$$; - --- OAUTH2 PARAMS ALTER TABLE START - -ALTER TABLE oauth2_params - ADD COLUMN IF NOT EXISTS edge_enabled boolean DEFAULT false; - --- OAUTH2 PARAMS ALTER TABLE END - --- QUEUE STATS UPDATE START - -CREATE TABLE IF NOT EXISTS queue_stats ( - id uuid NOT NULL CONSTRAINT queue_stats_pkey PRIMARY KEY, - created_time bigint NOT NULL, - tenant_id uuid NOT NULL, - queue_name varchar(255) NOT NULL, - service_id varchar(255) NOT NULL, - CONSTRAINT queue_stats_name_unq_key UNIQUE (tenant_id, queue_name, service_id) -); - -INSERT INTO queue_stats - SELECT id, created_time, tenant_id, substring(name FROM 1 FOR position('_' IN name) - 1) AS queue_name, - substring(name FROM position('_' IN name) + 1) AS service_id - FROM asset - WHERE type = 'TbServiceQueue' and name LIKE '%\_%'; - -DELETE FROM asset WHERE type='TbServiceQueue'; -DELETE FROM asset_profile WHERE name ='TbServiceQueue'; - --- QUEUE STATS UPDATE END - --- MOBILE APP SETTINGS TABLE CREATE START - -CREATE TABLE IF NOT EXISTS mobile_app_settings ( - id uuid NOT NULL CONSTRAINT mobile_app_settings_pkey PRIMARY KEY, - created_time bigint NOT NULL, - tenant_id uuid NOT NULL, - use_default_app boolean, - android_config VARCHAR(1000), - ios_config VARCHAR(1000), - qr_code_config VARCHAR(100000), - CONSTRAINT mobile_app_settings_tenant_id_unq_key UNIQUE (tenant_id) -); - --- MOBILE APP SETTINGS TABLE CREATE END \ No newline at end of file diff --git a/application/src/main/data/upgrade/3.7.0/schema_update.sql b/application/src/main/data/upgrade/3.7.0/schema_update.sql deleted file mode 100644 index 05f7423003..0000000000 --- a/application/src/main/data/upgrade/3.7.0/schema_update.sql +++ /dev/null @@ -1,215 +0,0 @@ --- --- Copyright © 2016-2024 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. --- - --- UPDATE RESOURCE SUB TYPE START - -DO -$$ - BEGIN - IF NOT EXISTS ( - SELECT FROM information_schema.columns - WHERE table_name = 'resource' AND column_name = 'resource_sub_type' - ) THEN - ALTER TABLE resource ADD COLUMN resource_sub_type varchar(32); - UPDATE resource SET resource_sub_type = 'IMAGE' WHERE resource_type = 'IMAGE'; - END IF; - END; -$$; - --- UPDATE RESOURCE SUB TYPE END - --- UPDATE WIDGETS BUNDLE START - -DO -$$ - BEGIN - IF NOT EXISTS ( - SELECT FROM information_schema.columns - WHERE table_name = 'widgets_bundle' AND column_name = 'scada' - ) THEN - ALTER TABLE widgets_bundle ADD COLUMN scada boolean NOT NULL DEFAULT false; - END IF; - END; -$$; - --- UPDATE WIDGETS BUNDLE END - --- UPDATE WIDGET TYPE START - -DO -$$ - BEGIN - IF NOT EXISTS ( - SELECT FROM information_schema.columns - WHERE table_name = 'widget_type' AND column_name = 'scada' - ) THEN - ALTER TABLE widget_type ADD COLUMN scada boolean NOT NULL DEFAULT false; - END IF; - END; -$$; - --- UPDATE WIDGET TYPE END - --- KV VERSIONING UPDATE START - -CREATE SEQUENCE IF NOT EXISTS attribute_kv_version_seq cache 1; -CREATE SEQUENCE IF NOT EXISTS ts_kv_latest_version_seq cache 1; - -ALTER TABLE attribute_kv ADD COLUMN IF NOT EXISTS version bigint default 0; -ALTER TABLE ts_kv_latest ADD COLUMN IF NOT EXISTS version bigint default 0; - --- KV VERSIONING UPDATE END - --- RELATION VERSIONING UPDATE START - -CREATE SEQUENCE IF NOT EXISTS relation_version_seq cache 1; -ALTER TABLE relation ADD COLUMN IF NOT EXISTS version bigint default 0; - --- RELATION VERSIONING UPDATE END - - --- ENTITIES VERSIONING UPDATE START - -ALTER TABLE device ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE device_profile ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE device_credentials ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE asset ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE asset_profile ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE entity_view ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE tb_user ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE customer ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE edge ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE rule_chain ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE dashboard ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE widget_type ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE widgets_bundle ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; -ALTER TABLE tenant ADD COLUMN IF NOT EXISTS version BIGINT DEFAULT 1; - --- ENTITIES VERSIONING UPDATE END - --- OAUTH2 UPDATE START - -ALTER TABLE IF EXISTS oauth2_mobile RENAME TO mobile_app; -ALTER TABLE IF EXISTS oauth2_domain RENAME TO domain; -ALTER TABLE IF EXISTS oauth2_registration RENAME TO oauth2_client; - -ALTER TABLE domain ADD COLUMN IF NOT EXISTS oauth2_enabled boolean, - ADD COLUMN IF NOT EXISTS edge_enabled boolean, - ADD COLUMN IF NOT EXISTS tenant_id uuid DEFAULT '13814000-1dd2-11b2-8080-808080808080', - DROP COLUMN IF EXISTS domain_scheme; - --- rename column domain_name to name -DO -$$ - BEGIN - IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='domain' and column_name='domain_name') THEN - ALTER TABLE domain RENAME COLUMN domain_name TO name; - END IF; - END -$$; - --- delete duplicated domains -DELETE FROM domain d1 USING ( - SELECT MIN(ctid) as ctid, name - FROM domain - GROUP BY name HAVING COUNT(*) > 1 -) d2 WHERE d1.name = d2.name AND d1.ctid <> d2.ctid; - -ALTER TABLE mobile_app ADD COLUMN IF NOT EXISTS oauth2_enabled boolean, - ADD COLUMN IF NOT EXISTS tenant_id uuid DEFAULT '13814000-1dd2-11b2-8080-808080808080'; - --- delete duplicated apps -DELETE FROM mobile_app m1 USING ( - SELECT MIN(ctid) as ctid, pkg_name - FROM mobile_app - GROUP BY pkg_name HAVING COUNT(*) > 1 -) m2 WHERE m1.pkg_name = m2.pkg_name AND m1.ctid <> m2.ctid; - -ALTER TABLE oauth2_client ADD COLUMN IF NOT EXISTS tenant_id uuid DEFAULT '13814000-1dd2-11b2-8080-808080808080', - ADD COLUMN IF NOT EXISTS title varchar(100); -UPDATE oauth2_client SET title = additional_info::jsonb->>'providerName' WHERE additional_info IS NOT NULL; - -CREATE TABLE IF NOT EXISTS domain_oauth2_client ( - domain_id uuid NOT NULL, - oauth2_client_id uuid NOT NULL, - CONSTRAINT fk_domain FOREIGN KEY (domain_id) REFERENCES domain(id) ON DELETE CASCADE, - CONSTRAINT fk_oauth2_client FOREIGN KEY (oauth2_client_id) REFERENCES oauth2_client(id) ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS mobile_app_oauth2_client ( - mobile_app_id uuid NOT NULL, - oauth2_client_id uuid NOT NULL, - CONSTRAINT fk_domain FOREIGN KEY (mobile_app_id) REFERENCES mobile_app(id) ON DELETE CASCADE, - CONSTRAINT fk_oauth2_client FOREIGN KEY (oauth2_client_id) REFERENCES oauth2_client(id) ON DELETE CASCADE -); - --- migrate oauth2_params table -DO -$$ - BEGIN - IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'oauth2_params') THEN - UPDATE domain SET oauth2_enabled = p.enabled, - edge_enabled = p.edge_enabled - FROM oauth2_params p WHERE p.id = domain.oauth2_params_id; - - UPDATE mobile_app SET oauth2_enabled = p.enabled - FROM oauth2_params p WHERE p.id = mobile_app.oauth2_params_id; - - INSERT INTO domain_oauth2_client(domain_id, oauth2_client_id) - (SELECT d.id, r.id FROM domain d LEFT JOIN oauth2_client r on d.oauth2_params_id = r.oauth2_params_id - WHERE r.platforms IS NULL OR r.platforms IN ('','WEB')); - - INSERT INTO mobile_app_oauth2_client(mobile_app_id, oauth2_client_id) - (SELECT m.id, r.id FROM mobile_app m LEFT JOIN oauth2_client r on m.oauth2_params_id = r.oauth2_params_id - WHERE r.platforms IS NULL OR r.platforms IN ('','ANDROID','IOS')); - - ALTER TABLE mobile_app RENAME CONSTRAINT oauth2_mobile_pkey TO mobile_app_pkey; - ALTER TABLE domain RENAME CONSTRAINT oauth2_domain_pkey TO domain_pkey; - ALTER TABLE oauth2_client RENAME CONSTRAINT oauth2_registration_pkey TO oauth2_client_pkey; - - ALTER TABLE domain DROP COLUMN oauth2_params_id; - ALTER TABLE mobile_app DROP COLUMN oauth2_params_id; - ALTER TABLE oauth2_client DROP COLUMN oauth2_params_id; - - ALTER TABLE mobile_app ADD CONSTRAINT mobile_app_unq_key UNIQUE (pkg_name); - ALTER TABLE domain ADD CONSTRAINT domain_unq_key UNIQUE (name); - - DROP TABLE IF EXISTS oauth2_params; - -- drop deprecated tables - DROP TABLE IF EXISTS oauth2_client_registration_info; - DROP TABLE IF EXISTS oauth2_client_registration; - END IF; - END -$$; - --- OAUTH2 UPDATE END - --- USER CREDENTIALS UPDATE START - -ALTER TABLE user_credentials ADD COLUMN IF NOT EXISTS activate_token_exp_time BIGINT; --- Setting 24-hour TTL for existing activation tokens -UPDATE user_credentials SET activate_token_exp_time = cast(extract(EPOCH FROM NOW()) * 1000 AS BIGINT) + 86400000 - WHERE activate_token IS NOT NULL AND activate_token_exp_time IS NULL; - -ALTER TABLE user_credentials ADD COLUMN IF NOT EXISTS reset_token_exp_time BIGINT; --- Setting 24-hour TTL for existing password reset tokens -UPDATE user_credentials SET reset_token_exp_time = cast(extract(EPOCH FROM NOW()) * 1000 AS BIGINT) + 86400000 - WHERE reset_token IS NOT NULL AND reset_token_exp_time IS NULL; - -UPDATE admin_settings SET json_value = (json_value::jsonb || '{"userActivationTokenTtl":24,"passwordResetTokenTtl":24}'::jsonb)::varchar - WHERE key = 'securitySettings'; - --- USER CREDENTIALS UPDATE END diff --git a/application/src/main/data/upgrade/3.8.1/schema_update.sql b/application/src/main/data/upgrade/basic/schema_update.sql similarity index 100% rename from application/src/main/data/upgrade/3.8.1/schema_update.sql rename to application/src/main/data/upgrade/basic/schema_update.sql diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java index b3a2cd4fd0..661eab6aac 100644 --- a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java +++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java @@ -24,6 +24,7 @@ import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import org.thingsboard.server.service.component.ComponentDiscoveryService; import org.thingsboard.server.service.install.DatabaseEntitiesUpgradeService; +import org.thingsboard.server.service.install.DatabaseSchemaSettingsService; import org.thingsboard.server.service.install.EntityDatabaseSchemaService; import org.thingsboard.server.service.install.InstallScripts; import org.thingsboard.server.service.install.NoSqlKeyspaceService; @@ -34,8 +35,6 @@ import org.thingsboard.server.service.install.migrate.TsLatestMigrateService; import org.thingsboard.server.service.install.update.CacheCleanupService; import org.thingsboard.server.service.install.update.DataUpdateService; -import static org.thingsboard.server.service.install.update.DefaultDataUpdateService.getEnv; - @Service @Profile("install") @Slf4j @@ -44,7 +43,7 @@ public class ThingsboardInstallService { @Value("${install.upgrade:false}") private Boolean isUpgrade; - @Value("${install.upgrade.from_version:1.2.3}") + @Value("${install.upgrade.from_version:}") private String upgradeFromVersion; @Value("${install.load_demo:false}") @@ -89,70 +88,32 @@ public class ThingsboardInstallService { @Autowired private InstallScripts installScripts; + @Autowired + private DatabaseSchemaSettingsService databaseSchemaVersionService; + public void performInstall() { try { if (isUpgrade) { log.info("Starting ThingsBoard Upgrade from version {} ...", upgradeFromVersion); - cacheCleanupService.clearCache(upgradeFromVersion); - if ("cassandra-latest-to-postgres".equals(upgradeFromVersion)) { log.info("Migrating ThingsBoard latest timeseries data from cassandra to SQL database ..."); latestMigrateService.migrate(); - } else if (upgradeFromVersion.equals("3.6.2-images")) { - installScripts.updateImages(); } else { - switch (upgradeFromVersion) { - case "3.5.0": - log.info("Upgrading ThingsBoard from version 3.5.0 to 3.5.1 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.5.0"); - case "3.5.1": - log.info("Upgrading ThingsBoard from version 3.5.1 to 3.6.0 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.5.1"); - dataUpdateService.updateData("3.5.1"); - systemDataLoaderService.updateDefaultNotificationConfigs(true); - case "3.6.0": - log.info("Upgrading ThingsBoard from version 3.6.0 to 3.6.1 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.6.0"); - dataUpdateService.updateData("3.6.0"); - case "3.6.1": - log.info("Upgrading ThingsBoard from version 3.6.1 to 3.6.2 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.6.1"); - if (!getEnv("SKIP_IMAGES_MIGRATION", false)) { - installScripts.setUpdateImages(true); - } else { - log.info("Skipping images migration. Run the upgrade with fromVersion as '3.6.2-images' to migrate"); - } - case "3.6.2": - log.info("Upgrading ThingsBoard from version 3.6.2 to 3.6.3 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.6.2"); - systemDataLoaderService.updateDefaultNotificationConfigs(true); - case "3.6.3": - log.info("Upgrading ThingsBoard from version 3.6.3 to 3.6.4 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.6.3"); - case "3.6.4": - log.info("Upgrading ThingsBoard from version 3.6.4 to 3.7.0 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.6.4"); - dataUpdateService.updateData("3.6.4"); - entityDatabaseSchemaService.createCustomerTitleUniqueConstraintIfNotExists(); - systemDataLoaderService.updateDefaultNotificationConfigs(false); - systemDataLoaderService.updateSecuritySettings(); - case "3.7.0": - log.info("Upgrading ThingsBoard from version 3.7.0 to 3.8.0 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.7.0"); - case "3.8.0": - log.info("Upgrading ThingsBoard from version 3.8.0 to 3.8.1 ..."); - case "3.8.1": - log.info("Upgrading ThingsBoard from version 3.8.1 to 3.9.0 ..."); - databaseEntitiesUpgradeService.upgradeDatabase("3.8.1"); - installScripts.updateResourcesUsage(); - //TODO DON'T FORGET to update switch statement in the CacheCleanupService if you need to clear the cache - break; - default: - throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion); - } + databaseSchemaVersionService.validateSchemaSettings(); + //TODO DON'T FORGET to update SUPPORTED_VERSIONS_FROM in DatabaseSchemaVersionService, + // this list should include last version and can include previous versions without upgrade + String fromVersion = databaseSchemaVersionService.getDbSchemaVersion(); + String toVersion = databaseSchemaVersionService.getPackageSchemaVersion(); + log.info("Upgrading ThingsBoard from version {} to {} ...", fromVersion, toVersion); + cacheCleanupService.clearCache(); + entityDatabaseSchemaService.createDatabaseSchema(false); + databaseEntitiesUpgradeService.upgradeDatabase(); + dataUpdateService.updateData(); //TODO: update data should be cleaned after each release + entityDatabaseSchemaService.createOrUpdateViewsAndFunctions(); entityDatabaseSchemaService.createOrUpdateDeviceInfoView(persistToTelemetry); + entityDatabaseSchemaService.createDatabaseIndexes(); log.info("Updating system data..."); dataUpdateService.upgradeRuleNodes(); systemDataLoaderService.loadSystemWidgets(); @@ -161,6 +122,7 @@ public class ThingsboardInstallService { if (installScripts.isUpdateImages()) { installScripts.updateImages(); } + databaseSchemaVersionService.updateSchemaVersion(); } log.info("Upgrade finished successfully!"); @@ -171,7 +133,7 @@ public class ThingsboardInstallService { log.info("Installing DataBase schema for entities..."); entityDatabaseSchemaService.createDatabaseSchema(); - entityDatabaseSchemaService.createSchemaVersion(); + databaseSchemaVersionService.createSchemaSettings(); entityDatabaseSchemaService.createOrUpdateViewsAndFunctions(); entityDatabaseSchemaService.createOrUpdateDeviceInfoView(persistToTelemetry); @@ -212,8 +174,6 @@ public class ThingsboardInstallService { } log.info("Installation finished successfully!"); } - - } catch (Exception e) { log.error("Unexpected error during ThingsBoard installation!", e); throw new ThingsboardInstallException("Unexpected error during ThingsBoard installation!", e); @@ -223,4 +183,3 @@ public class ThingsboardInstallService { } } - diff --git a/application/src/main/java/org/thingsboard/server/service/install/AbstractSqlTsDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/AbstractSqlTsDatabaseUpgradeService.java deleted file mode 100644 index 1d90620fbd..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/AbstractSqlTsDatabaseUpgradeService.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Copyright © 2016-2024 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.Value; - -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.SQLWarning; -import java.sql.Statement; - -@Slf4j -public abstract class AbstractSqlTsDatabaseUpgradeService { - - protected static final String CALL_REGEX = "call "; - protected static final String DROP_TABLE = "DROP TABLE "; - protected static final String DROP_PROCEDURE_IF_EXISTS = "DROP PROCEDURE IF EXISTS "; - protected static final String TS_KV_SQL = "ts_kv.sql"; - protected static final String PATH_TO_USERS_PUBLIC_FOLDER = "C:\\Users\\Public"; - protected static final String THINGSBOARD_WINDOWS_UPGRADE_DIR = "THINGSBOARD_WINDOWS_UPGRADE_DIR"; - - @Value("${spring.datasource.url}") - protected String dbUrl; - - @Value("${spring.datasource.username}") - protected String dbUserName; - - @Value("${spring.datasource.password}") - protected String dbPassword; - - @Autowired - protected InstallScripts installScripts; - - protected abstract void loadSql(Connection conn, String fileName, String version); - - protected void loadFunctions(Path sqlFile, Connection conn) throws Exception { - String sql = new String(Files.readAllBytes(sqlFile), StandardCharsets.UTF_8); - conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script - } - - protected boolean checkVersion(Connection conn) { - boolean versionValid = false; - try { - Statement statement = conn.createStatement(); - ResultSet resultSet = statement.executeQuery("SELECT current_setting('server_version_num')"); - resultSet.next(); - if(resultSet.getLong(1) > 110000) { - versionValid = true; - } - statement.close(); - } catch (Exception e) { - log.info("Failed to check current PostgreSQL version due to: {}", e.getMessage()); - } - return versionValid; - } - - protected boolean isOldSchema(Connection conn, long fromVersion) { - boolean isOldSchema = true; - try { - Statement statement = conn.createStatement(); - statement.execute("CREATE TABLE IF NOT EXISTS tb_schema_settings ( schema_version bigint NOT NULL, CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version));"); - Thread.sleep(1000); - ResultSet resultSet = statement.executeQuery("SELECT schema_version FROM tb_schema_settings;"); - if (resultSet.next()) { - isOldSchema = resultSet.getLong(1) <= fromVersion; - } else { - resultSet.close(); - statement.execute("INSERT INTO tb_schema_settings (schema_version) VALUES (" + fromVersion + ")"); - } - statement.close(); - } catch (InterruptedException | SQLException e) { - log.info("Failed to check current PostgreSQL schema due to: {}", e.getMessage()); - } - return isOldSchema; - } - - protected void executeQuery(Connection conn, String query) { - try { - Statement statement = conn.createStatement(); - statement.execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script - 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(); - } - } - Thread.sleep(2000); - log.info("Successfully executed query: {}", query); - } catch (InterruptedException | SQLException e) { - log.error("Failed to execute query: {} due to: {}", query, e.getMessage()); - throw new RuntimeException("Failed to execute query:" + query + " due to: ", e); - } - } - -} \ No newline at end of file diff --git a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseUpgradeService.java deleted file mode 100644 index 2937b71974..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/CassandraTsDatabaseUpgradeService.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright © 2016-2024 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.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) { - default: - throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion); - } - } - -} diff --git a/application/src/main/java/org/thingsboard/server/service/install/DatabaseEntitiesUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/DatabaseEntitiesUpgradeService.java index 560d0d5b62..f46aa7c08e 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/DatabaseEntitiesUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/DatabaseEntitiesUpgradeService.java @@ -17,6 +17,6 @@ package org.thingsboard.server.service.install; public interface DatabaseEntitiesUpgradeService { - void upgradeDatabase(String fromVersion) throws Exception; + void upgradeDatabase() throws Exception; } diff --git a/application/src/main/java/org/thingsboard/server/service/install/DatabaseTsUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/DatabaseSchemaSettingsService.java similarity index 74% rename from application/src/main/java/org/thingsboard/server/service/install/DatabaseTsUpgradeService.java rename to application/src/main/java/org/thingsboard/server/service/install/DatabaseSchemaSettingsService.java index 95da9119d2..49548a077d 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/DatabaseTsUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/DatabaseSchemaSettingsService.java @@ -15,8 +15,14 @@ */ package org.thingsboard.server.service.install; -public interface DatabaseTsUpgradeService { +public interface DatabaseSchemaSettingsService { + void validateSchemaSettings(); - void upgradeDatabase(String fromVersion) throws Exception; + void createSchemaSettings(); -} \ No newline at end of file + void updateSchemaVersion(); + + String getPackageSchemaVersion(); + + String getDbSchemaVersion(); +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/DefaultDatabaseSchemaSettingsService.java b/application/src/main/java/org/thingsboard/server/service/install/DefaultDatabaseSchemaSettingsService.java new file mode 100644 index 0000000000..e3cc1d9356 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/DefaultDatabaseSchemaSettingsService.java @@ -0,0 +1,140 @@ +/** + * Copyright © 2016-2024 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.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.info.BuildProperties; +import org.springframework.context.annotation.Profile; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.thingsboard.server.service.install.update.DefaultDataUpdateService; + +import java.util.List; + +@Service +@Profile("install") +@Slf4j +@RequiredArgsConstructor +public class DefaultDatabaseSchemaSettingsService implements DatabaseSchemaSettingsService { + + private static final String CURRENT_PRODUCT = "CE"; + private static final List SUPPORTED_VERSIONS_FOR_UPGRADE = List.of("3.8.0", "3.8.1"); + + private final BuildProperties buildProperties; + private final JdbcTemplate jdbcTemplate; + + private String packageSchemaVersion; + private String schemaVersionFromDb; + + @Override + public void validateSchemaSettings() { + //TODO: remove after release (3.9.0) + createProductIfNotExists(); + + String dbSchemaVersion = getDbSchemaVersion(); + + if (DefaultDataUpdateService.getEnv("SKIP_SCHEMA_VERSION_CHECK", false)) { + log.info("Skipped DB schema version check due to SKIP_SCHEMA_VERSION_CHECK set to 'true'."); + return; + } + + String product = getProductFromDb(); + if (!CURRENT_PRODUCT.equals(product)) { + onSchemaSettingsError(String.format("Upgrade failed: can't upgrade ThingsBoard %s database using ThingsBoard %s.", product, CURRENT_PRODUCT)); + } + + if (dbSchemaVersion.equals(getPackageSchemaVersion())) { + onSchemaSettingsError("Upgrade failed: database already upgraded to current version. You can set SKIP_SCHEMA_VERSION_CHECK to 'true' if force re-upgrade needed."); + } + + if (!SUPPORTED_VERSIONS_FOR_UPGRADE.contains(dbSchemaVersion)) { + onSchemaSettingsError(String.format("Upgrade failed: database version '%s' is not supported for upgrade. Supported versions are: %s.", + dbSchemaVersion, SUPPORTED_VERSIONS_FOR_UPGRADE + )); + } + } + + @Deprecated(forRemoval = true, since = "3.9.0") + private void createProductIfNotExists() { + boolean isCommunityEdition = jdbcTemplate.queryForList( + "SELECT 1 FROM information_schema.tables WHERE table_name = 'integration'", Integer.class).isEmpty(); + String product = isCommunityEdition ? "CE" : "PE"; + jdbcTemplate.execute("ALTER TABLE tb_schema_settings ADD COLUMN IF NOT EXISTS product varchar(2) DEFAULT '" + product + "'"); + } + + @Override + public void createSchemaSettings() { + Long schemaVersion = getSchemaVersionFromDb(); + if (schemaVersion == null) { + jdbcTemplate.execute("INSERT INTO tb_schema_settings (schema_version, product) VALUES (" + getPackageSchemaVersionForDb() + ", '" + CURRENT_PRODUCT + "')"); + } + } + + @Override + public void updateSchemaVersion() { + jdbcTemplate.execute("UPDATE tb_schema_settings SET schema_version = " + getPackageSchemaVersionForDb()); + } + + @Override + public String getPackageSchemaVersion() { + if (packageSchemaVersion == null) { + packageSchemaVersion = buildProperties.getVersion().replaceAll("[^\\d.]", ""); + } + return packageSchemaVersion; + } + + @Override + public String getDbSchemaVersion() { + if (schemaVersionFromDb == null) { + Long version = getSchemaVersionFromDb(); + if (version == null) { + onSchemaSettingsError("Upgrade failed: the database schema version is missing."); + } + + @SuppressWarnings("DataFlowIssue") + long major = version / 1000000; + long minor = (version % 1000000) / 1000; + long patch = version % 1000; + + schemaVersionFromDb = major + "." + minor + "." + patch; + } + return schemaVersionFromDb; + } + + private Long getSchemaVersionFromDb() { + return jdbcTemplate.queryForList("SELECT schema_version FROM tb_schema_settings", Long.class).stream().findFirst().orElse(null); + } + + private String getProductFromDb() { + return jdbcTemplate.queryForList("SELECT product FROM tb_schema_settings", String.class).stream().findFirst().orElse(null); + } + + private long getPackageSchemaVersionForDb() { + String[] versionParts = getPackageSchemaVersion().split("\\."); + + long major = Integer.parseInt(versionParts[0]); + long minor = Integer.parseInt(versionParts[1]); + long patch = versionParts.length > 2 ? Integer.parseInt(versionParts[2]) : 0; + + return major * 1000000 + minor * 1000 + patch; + } + + private void onSchemaSettingsError(String message) { + Runtime.getRuntime().addShutdownHook(new Thread(() -> log.error(message))); + throw new RuntimeException(message); + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java index 02241367a1..5d7988d9e5 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/EntityDatabaseSchemaService.java @@ -21,8 +21,4 @@ public interface EntityDatabaseSchemaService extends DatabaseSchemaService { void createOrUpdateViewsAndFunctions() throws Exception; - void createCustomerTitleUniqueConstraintIfNotExists(); - - void createSchemaVersion(); - } diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java index 86d03d65f5..5780057340 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java @@ -23,7 +23,6 @@ import org.springframework.jdbc.core.StatementCallback; import org.springframework.stereotype.Service; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.TransactionTemplate; -import org.thingsboard.server.service.install.update.DefaultDataUpdateService; import java.io.IOException; import java.io.UncheckedIOException; @@ -52,80 +51,10 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService } @Override - public void upgradeDatabase(String fromVersion) { - switch (fromVersion) { - case "3.5.0" -> updateSchema("3.5.0", 3005000, "3.5.1", 3005001); - case "3.5.1" -> { - updateSchema("3.5.1", 3005001, "3.6.0", 3006000); - - String[] tables = new String[]{"device", "component_descriptor", "customer", "dashboard", "rule_chain", "rule_node", "ota_package", - "asset_profile", "asset", "device_profile", "tb_user", "tenant_profile", "tenant", "widgets_bundle", "entity_view", "edge"}; - for (String table : tables) { - execute("ALTER TABLE " + table + " DROP COLUMN IF EXISTS search_text CASCADE"); - } - execute( - "ALTER TABLE component_descriptor ADD COLUMN IF NOT EXISTS configuration_version int DEFAULT 0;", - "ALTER TABLE rule_node ADD COLUMN IF NOT EXISTS configuration_version int DEFAULT 0;", - "CREATE INDEX IF NOT EXISTS idx_rule_node_type_configuration_version ON rule_node(type, configuration_version);", - "UPDATE rule_node SET " + - "configuration = (configuration::jsonb || '{\"updateAttributesOnlyOnValueChange\": \"false\"}'::jsonb)::varchar, " + - "configuration_version = 1 " + - "WHERE type = 'org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode' AND configuration_version < 1;", - "CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_unread ON notification(recipient_id) WHERE status <> 'READ';" - ); - } - case "3.6.0" -> updateSchema("3.6.0", 3006000, "3.6.1", 3006001); - case "3.6.1" -> { - updateSchema("3.6.1", 3006001, "3.6.2", 3006002); - - try { - Path saveAttributesNodeUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.6.1", "save_attributes_node_update.sql"); - loadSql(saveAttributesNodeUpdateFile); - } catch (Exception e) { - log.warn("Failed to execute update script for save attributes rule nodes due to: ", e); - } - execute("CREATE INDEX IF NOT EXISTS idx_asset_profile_id ON asset(tenant_id, asset_profile_id);"); - } - case "3.6.2" -> updateSchema("3.6.2", 3006002, "3.6.3", 3006003); - case "3.6.3" -> updateSchema("3.6.3", 3006003, "3.6.4", 3006004); - case "3.6.4" -> updateSchema("3.6.4", 3006004, "3.7.0", 3007000); - case "3.7.0" -> { - updateSchema("3.7.0", 3007000, "3.8.0", 3008000); - - try { - execute("UPDATE rule_node SET " + - "configuration = CASE " + - " WHEN (configuration::jsonb ->> 'persistAlarmRulesState') = 'false'" + - " THEN (configuration::jsonb || '{\"fetchAlarmRulesStateOnStart\": \"false\"}'::jsonb)::varchar " + - " ELSE configuration " + - "END, " + - "configuration_version = 1 " + - "WHERE type = 'org.thingsboard.rule.engine.profile.TbDeviceProfileNode' " + - "AND configuration_version < 1;", false); - } catch (Exception e) { - log.warn("Failed to execute update script for device profile rule nodes due to: ", e); - } - } - case "3.8.1" -> updateSchema("3.8.1", 3008001, "3.9.0", 3009000); - default -> throw new RuntimeException("Unsupported fromVersion '" + fromVersion + "'"); - } - } - - private void updateSchema(String oldVersionStr, int oldVersion, String newVersionStr, int newVersion) { - try { - transactionTemplate.executeWithoutResult(ts -> { - log.info("Updating schema ..."); - if (isOldSchema(oldVersion)) { - loadSql(getSchemaUpdateFile(oldVersionStr)); - jdbcTemplate.execute("UPDATE tb_schema_settings SET schema_version = " + newVersion); - log.info("Schema updated to version {}", newVersionStr); - } else { - log.info("Skip schema re-update to version {}. Use env flag 'SKIP_SCHEMA_VERSION_CHECK' to force the re-update.", newVersionStr); - } - }); - } catch (Exception e) { - throw new RuntimeException("Failed to update schema", e); - } + public void upgradeDatabase() { + log.info("Updating schema ..."); + loadSql(getSchemaUpdateFile("basic")); + log.info("Schema updated."); } private Path getSchemaUpdateFile(String version) { @@ -173,20 +102,4 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService } } - private boolean isOldSchema(long fromVersion) { - if (DefaultDataUpdateService.getEnv("SKIP_SCHEMA_VERSION_CHECK", false)) { - log.info("Skipped DB schema version check due to SKIP_SCHEMA_VERSION_CHECK set to true!"); - return true; - } - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS tb_schema_settings (schema_version bigint NOT NULL, CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version))"); - Long schemaVersion = jdbcTemplate.queryForList("SELECT schema_version FROM tb_schema_settings", Long.class).stream().findFirst().orElse(null); - boolean isOldSchema = true; - if (schemaVersion != null) { - isOldSchema = schemaVersion <= fromVersion; - } else { - jdbcTemplate.execute("INSERT INTO tb_schema_settings (schema_version) VALUES (" + fromVersion + ")"); - } - return isOldSchema; - } - } diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java index 3bb8b0e58f..e83f438083 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlEntityDatabaseSchemaService.java @@ -16,10 +16,7 @@ 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 @@ -32,11 +29,6 @@ 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); } @@ -61,32 +53,4 @@ public class SqlEntityDatabaseSchemaService extends SqlAbstractDatabaseSchemaSer executeQueryFromFile(SCHEMA_VIEWS_AND_FUNCTIONS_SQL); } - @Override - public void createCustomerTitleUniqueConstraintIfNotExists() { - executeQuery("DO $$ BEGIN IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'customer_title_unq_key') THEN " + - "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; - } } diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseUpgradeService.java deleted file mode 100644 index ebe4cb8ef5..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlTsDatabaseUpgradeService.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright © 2016-2024 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.context.annotation.Profile; -import org.springframework.stereotype.Service; -import org.thingsboard.server.dao.util.SqlTsDao; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.sql.Connection; - -@Service -@Profile("install") -@Slf4j -@SqlTsDao -public class SqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeService implements DatabaseTsUpgradeService { - - @Override - public void upgradeDatabase(String fromVersion) throws Exception { - switch (fromVersion) { - default: - throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); - } - } - - @Override - protected void loadSql(Connection conn, String fileName, String version) { - Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", version, fileName); - try { - loadFunctions(schemaUpdateFile, conn); - log.info("Functions successfully loaded!"); - } catch (Exception e) { - log.info("Failed to load PostgreSQL upgrade functions due to: {}", e.getMessage()); - } - } -} diff --git a/application/src/main/java/org/thingsboard/server/service/install/TimescaleTsDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/TimescaleTsDatabaseUpgradeService.java deleted file mode 100644 index 43b0b37d71..0000000000 --- a/application/src/main/java/org/thingsboard/server/service/install/TimescaleTsDatabaseUpgradeService.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright © 2016-2024 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.context.annotation.Profile; -import org.springframework.stereotype.Service; -import org.thingsboard.server.dao.util.TimescaleDBTsDao; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.sql.Connection; - -@Service -@Profile("install") -@Slf4j -@TimescaleDBTsDao -public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeService implements DatabaseTsUpgradeService { - - @Autowired - private InstallScripts installScripts; - - @Override - public void upgradeDatabase(String fromVersion) throws Exception { - switch (fromVersion) { - default: - throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); - } - } - - @Override - protected void loadSql(Connection conn, String fileName, String version) { - Path schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", version, fileName); - try { - loadFunctions(schemaUpdateFile, conn); - log.info("Functions successfully loaded!"); - } catch (Exception e) { - log.info("Failed to load PostgreSQL upgrade functions due to: {}", e.getMessage()); - } - } -} diff --git a/application/src/main/java/org/thingsboard/server/service/install/update/CacheCleanupService.java b/application/src/main/java/org/thingsboard/server/service/install/update/CacheCleanupService.java index 0bc48fd9a7..f0c700337d 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/update/CacheCleanupService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/update/CacheCleanupService.java @@ -17,6 +17,6 @@ package org.thingsboard.server.service.install.update; public interface CacheCleanupService { - void clearCache(String fromVersion) throws Exception; + void clearCache() throws Exception; } diff --git a/application/src/main/java/org/thingsboard/server/service/install/update/DataUpdateService.java b/application/src/main/java/org/thingsboard/server/service/install/update/DataUpdateService.java index 0920e21264..37b2724f14 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/update/DataUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/update/DataUpdateService.java @@ -17,7 +17,7 @@ package org.thingsboard.server.service.install.update; public interface DataUpdateService { - void updateData(String fromVersion) throws Exception; + void updateData() throws Exception; void upgradeRuleNodes(); } diff --git a/application/src/main/java/org/thingsboard/server/service/install/update/DefaultCacheCleanupService.java b/application/src/main/java/org/thingsboard/server/service/install/update/DefaultCacheCleanupService.java index 1542133764..4fc4f8d40d 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/update/DefaultCacheCleanupService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/update/DefaultCacheCleanupService.java @@ -27,9 +27,6 @@ import org.springframework.stereotype.Service; import java.util.Objects; import java.util.Optional; -import static org.thingsboard.server.common.data.CacheConstants.RESOURCE_INFO_CACHE; -import static org.thingsboard.server.common.data.CacheConstants.SECURITY_SETTINGS_CACHE; - @RequiredArgsConstructor @Service @Profile("install") @@ -39,39 +36,15 @@ public class DefaultCacheCleanupService implements CacheCleanupService { private final CacheManager cacheManager; private final Optional> redisTemplate; - /** * Cleanup caches that can not deserialize anymore due to schema upgrade or data update using sql scripts. * Refer to SqlDatabaseUpgradeService and /data/upgrage/*.sql * to discover which tables were changed * */ @Override - public void clearCache(String fromVersion) throws Exception { - switch (fromVersion) { - case "3.6.1": - log.info("Clearing cache to upgrade from version 3.6.1 to 3.6.2"); - clearCacheByName(SECURITY_SETTINGS_CACHE); - clearCacheByName(RESOURCE_INFO_CACHE); - break; - case "3.6.3": - log.info("Clearing cache to upgrade from version 3.6.3 to 3.6.4"); - clearAll(); - break; - case "3.6.4": - log.info("Clearing cache to upgrade from version 3.6.4 to 3.7.0"); - clearAll(); - break; - case "3.7.0": - log.info("Clearing cache to upgrade from version 3.7.0 to 3.8.0"); - clearAll(); - break; - case "3.8.1": - log.info("Clearing cache to upgrade from version 3.8.1 to 3.9.0"); - clearAll(); - break; - default: - //Do nothing, since cache cleanup is optional. - } + public void clearCache() throws Exception { + log.info("Clearing cache to upgrade."); + clearAll(); } void clearAllCaches() { diff --git a/application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java b/application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java index 6636de3ff1..dd69533779 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java @@ -24,29 +24,21 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; -import org.thingsboard.common.util.JacksonUtil; -import org.thingsboard.server.common.data.AdminSettings; -import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.alarm.AlarmSeverity; import org.thingsboard.server.common.data.id.RuleNodeId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageDataIterable; import org.thingsboard.server.common.data.query.DynamicValue; import org.thingsboard.server.common.data.query.FilterPredicateValue; -import org.thingsboard.server.dao.customer.CustomerDao; -import org.thingsboard.server.dao.customer.CustomerService; -import org.thingsboard.server.dao.device.DeviceConnectivityConfiguration; import org.thingsboard.server.dao.rule.RuleChainService; -import org.thingsboard.server.dao.settings.AdminSettingsService; import org.thingsboard.server.dao.sql.JpaExecutorService; -import org.thingsboard.server.dao.tenant.TenantProfileService; import org.thingsboard.server.service.component.ComponentDiscoveryService; import org.thingsboard.server.service.component.RuleNodeClassInfo; +import org.thingsboard.server.service.install.InstallScripts; import org.thingsboard.server.utils.TbNodeUpgradeUtils; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.concurrent.ExecutionException; @Service @@ -67,156 +59,14 @@ public class DefaultDataUpdateService implements DataUpdateService { JpaExecutorService jpaExecutorService; @Autowired - AdminSettingsService adminSettingsService; - - @Autowired - DeviceConnectivityConfiguration connectivityConfiguration; - - @Autowired - private CustomerDao customerDao; - - @Autowired - private CustomerService customerService; - - @Autowired - private TenantProfileService tenantProfileService; + private InstallScripts installScripts; @Override - public void updateData(String fromVersion) throws Exception { - switch (fromVersion) { - case "3.6.0": - log.info("Updating data from version 3.6.0 to 3.6.1 ..."); - migrateDeviceConnectivity(); - break; - case "3.6.4": - log.info("Updating data from version 3.6.4 to 3.7.0 ..."); - updateCustomersWithTheSameTitle(); - updateMaxRuleNodeExecsPerMessage(); - updateGatewayRateLimits(); - break; - default: - throw new RuntimeException("Unable to update data, unsupported fromVersion: " + fromVersion); - } - } - - private void updateGatewayRateLimits() { - var tenantProfiles = new PageDataIterable<>(link -> tenantProfileService.findTenantProfiles(TenantId.SYS_TENANT_ID, link), DEFAULT_PAGE_SIZE); - tenantProfiles.forEach(tenantProfile -> { - var configurationOpt = tenantProfile.getProfileConfiguration(); - configurationOpt.ifPresent(configuration -> { - boolean updated = false; - if (configuration.getTransportDeviceMsgRateLimit() != null) { - if (configuration.getTransportGatewayMsgRateLimit() == null) { - configuration.setTransportGatewayMsgRateLimit(configuration.getTransportDeviceMsgRateLimit()); - updated = true; - } - if (configuration.getTransportGatewayDeviceMsgRateLimit() == null) { - configuration.setTransportGatewayDeviceMsgRateLimit(configuration.getTransportDeviceMsgRateLimit()); - updated = true; - } - } - if (configuration.getTransportDeviceTelemetryMsgRateLimit() != null) { - if (configuration.getTransportGatewayTelemetryMsgRateLimit() == null) { - configuration.setTransportGatewayTelemetryMsgRateLimit(configuration.getTransportDeviceTelemetryMsgRateLimit()); - updated = true; - } - if (configuration.getTransportGatewayDeviceTelemetryMsgRateLimit() == null) { - configuration.setTransportGatewayDeviceTelemetryMsgRateLimit(configuration.getTransportDeviceTelemetryMsgRateLimit()); - updated = true; - } - } - if (configuration.getTransportDeviceTelemetryDataPointsRateLimit() != null) { - if (configuration.getTransportGatewayTelemetryDataPointsRateLimit() == null) { - configuration.setTransportGatewayTelemetryDataPointsRateLimit(configuration.getTransportDeviceTelemetryDataPointsRateLimit()); - updated = true; - } - if (configuration.getTransportGatewayDeviceTelemetryDataPointsRateLimit() == null) { - configuration.setTransportGatewayDeviceTelemetryDataPointsRateLimit(configuration.getTransportDeviceTelemetryDataPointsRateLimit()); - updated = true; - } - } - if (updated) { - try { - tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); - } catch (Exception e) { - log.error("Failed to update tenant profile with id: {} due to: ", tenantProfile.getId(), e); - } - } - }); - }); - } - - private void updateMaxRuleNodeExecsPerMessage() { - var tenantProfiles = new PageDataIterable<>( - link -> tenantProfileService.findTenantProfiles(TenantId.SYS_TENANT_ID, link), DEFAULT_PAGE_SIZE); - tenantProfiles.forEach(tenantProfile -> { - var configurationOpt = tenantProfile.getProfileConfiguration(); - configurationOpt.ifPresent(configuration -> { - if (configuration.getMaxRuleNodeExecsPerMessage() == 0) { - configuration.setMaxRuleNodeExecutionsPerMessage(1000); - try { - tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); - } catch (Exception e) { - log.error("Failed to update tenant profile with id: {} due to: ", tenantProfile.getId(), e); - } - } - }); - }); - } - - private void updateCustomersWithTheSameTitle() { - var customers = new ArrayList(); - new PageDataIterable<>(pageLink -> - customerDao.findCustomersWithTheSameTitle(pageLink), DEFAULT_PAGE_SIZE - ).forEach(customers::add); - if (customers.isEmpty()) { - return; - } - var firstCustomer = customers.get(0); - var titleToDeduplicate = firstCustomer.getTitle(); - var tenantIdToDeduplicate = firstCustomer.getTenantId(); - int duplicateCounter = 1; - - for (int i = 1; i < customers.size(); i++) { - var currentCustomer = customers.get(i); - if (currentCustomer.getTitle().equals(titleToDeduplicate) && currentCustomer.getTenantId().equals(tenantIdToDeduplicate)) { - duplicateCounter++; - String currentTitle = currentCustomer.getTitle(); - String newTitle = currentTitle + " " + duplicateCounter; - try { - Optional customerOpt = customerService.findCustomerByTenantIdAndTitle(tenantIdToDeduplicate, newTitle); - if (customerOpt.isPresent()) { - // fallback logic: customer with title 'currentTitle + " " + duplicateCounter;' might be another duplicate. - newTitle = currentTitle + "_" + currentCustomer.getId(); - } - } catch (Exception e) { - log.trace("Failed to find customer with title due to: ", e); - // fallback logic: customer with title 'currentTitle + " " + duplicateCounter;' might be another duplicate. - newTitle = currentTitle + "_" + currentCustomer.getId(); - } - currentCustomer.setTitle(newTitle); - try { - customerService.saveCustomer(currentCustomer); - } catch (Exception e) { - log.error("[{}] Failed to update customer with id and title: {}, oldTitle: {}, due to: ", - currentCustomer.getTenantId(), newTitle, currentTitle, e); - } - continue; - } - titleToDeduplicate = currentCustomer.getTitle(); - tenantIdToDeduplicate = currentCustomer.getTenantId(); - duplicateCounter = 1; - } - } - - private void migrateDeviceConnectivity() { - if (adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "connectivity") == null) { - AdminSettings connectivitySettings = new AdminSettings(); - connectivitySettings.setTenantId(TenantId.SYS_TENANT_ID); - connectivitySettings.setKey("connectivity"); - connectivitySettings.setJsonValue(JacksonUtil.valueToTree(connectivityConfiguration.getConnectivity())); - adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, connectivitySettings); - } + public void updateData() throws Exception { + log.info("Updating data ..."); + //TODO: should be cleaned after each release + installScripts.updateResourcesUsage(); + log.info("Data updated."); } @Override diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index cccaf9062d..a0f4beab08 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS tb_schema_settings ( schema_version bigint NOT NULL, + product varchar(2) NOT NULL, CONSTRAINT tb_schema_settings_pkey PRIMARY KEY (schema_version) );