Showing
6 changed files
with
74 additions
and
64 deletions
1 | +-- | |
2 | +-- Copyright © 2016-2021 The Thingsboard Authors | |
3 | +-- | |
4 | +-- Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | +-- you may not use this file except in compliance with the License. | |
6 | +-- You may obtain a copy of the License at | |
7 | +-- | |
8 | +-- http://www.apache.org/licenses/LICENSE-2.0 | |
9 | +-- | |
10 | +-- Unless required by applicable law or agreed to in writing, software | |
11 | +-- distributed under the License is distributed on an "AS IS" BASIS, | |
12 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | +-- See the License for the specific language governing permissions and | |
14 | +-- limitations under the License. | |
15 | +-- | |
16 | + | |
17 | +CREATE TABLE IF NOT EXISTS resource ( | |
18 | + id uuid NOT NULL CONSTRAINT resource_pkey PRIMARY KEY, | |
19 | + created_time bigint NOT NULL, | |
20 | + tenant_id uuid NOT NULL, | |
21 | + title varchar(255) NOT NULL, | |
22 | + resource_type varchar(32) NOT NULL, | |
23 | + resource_key varchar(255) NOT NULL, | |
24 | + search_text varchar(255), | |
25 | + file_name varchar(255) NOT NULL, | |
26 | + data varchar, | |
27 | + CONSTRAINT resource_unq_key UNIQUE (tenant_id, resource_type, resource_key) | |
28 | +); | |
29 | + | |
30 | +CREATE TABLE IF NOT EXISTS firmware ( | |
31 | + id uuid NOT NULL CONSTRAINT firmware_pkey PRIMARY KEY, | |
32 | + created_time bigint NOT NULL, | |
33 | + tenant_id uuid NOT NULL, | |
34 | + title varchar(255) NOT NULL, | |
35 | + search_text varchar(255), | |
36 | + file_name varchar(255) NOT NULL, | |
37 | + content_type varchar(255) NOT NULL, | |
38 | + data bytea | |
39 | +); | |
40 | + | |
41 | +ALTER TABLE device_profile | |
42 | + ADD COLUMN IF NOT EXISTS firmware_id uuid; | |
43 | + | |
44 | +ALTER TABLE device | |
45 | + ADD COLUMN IF NOT EXISTS firmware_id uuid; | |
46 | + | |
47 | +DO $$ | |
48 | + BEGIN | |
49 | + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_firmware_device_profile') THEN | |
50 | + ALTER TABLE device_profile | |
51 | + ADD CONSTRAINT fk_firmware_device_profile | |
52 | + FOREIGN KEY (firmware_id) REFERENCES firmware(id); | |
53 | + END IF; | |
54 | + | |
55 | + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_firmware_device') THEN | |
56 | + ALTER TABLE device | |
57 | + ADD CONSTRAINT fk_firmware_device | |
58 | + FOREIGN KEY (firmware_id) REFERENCES firmware(id); | |
59 | + END IF; | |
60 | + END; | |
61 | +$$; | ... | ... |
application/src/main/java/org/thingsboard/server/service/install/DefaultSystemDataLoaderService.java
... | ... | @@ -58,11 +58,8 @@ import org.thingsboard.server.common.data.page.PageLink; |
58 | 58 | import org.thingsboard.server.common.data.query.BooleanFilterPredicate; |
59 | 59 | import org.thingsboard.server.common.data.query.DynamicValue; |
60 | 60 | import org.thingsboard.server.common.data.query.DynamicValueSourceType; |
61 | -import org.thingsboard.server.common.data.query.EntityKey; | |
62 | -import org.thingsboard.server.common.data.query.EntityKeyType; | |
63 | 61 | import org.thingsboard.server.common.data.query.EntityKeyValueType; |
64 | 62 | import org.thingsboard.server.common.data.query.FilterPredicateValue; |
65 | -import org.thingsboard.server.common.data.query.KeyFilter; | |
66 | 63 | import org.thingsboard.server.common.data.query.NumericFilterPredicate; |
67 | 64 | import org.thingsboard.server.common.data.security.Authority; |
68 | 65 | import org.thingsboard.server.common.data.security.DeviceCredentials; |
... | ... | @@ -445,11 +442,6 @@ public class DefaultSystemDataLoaderService implements SystemDataLoaderService { |
445 | 442 | installScripts.loadSystemWidgets(); |
446 | 443 | } |
447 | 444 | |
448 | - @Override | |
449 | - public void loadSystemLwm2mResources() throws Exception { | |
450 | - installScripts.loadSystemLwm2mResources(); | |
451 | - } | |
452 | - | |
453 | 445 | private User createUser(Authority authority, |
454 | 446 | TenantId tenantId, |
455 | 447 | CustomerId customerId, | ... | ... |
... | ... | @@ -22,8 +22,6 @@ import org.springframework.beans.factory.annotation.Value; |
22 | 22 | import org.springframework.stereotype.Component; |
23 | 23 | import org.springframework.util.StringUtils; |
24 | 24 | import org.thingsboard.server.common.data.Dashboard; |
25 | -import org.thingsboard.server.common.data.ResourceType; | |
26 | -import org.thingsboard.server.common.data.TbResource; | |
27 | 25 | import org.thingsboard.server.common.data.id.CustomerId; |
28 | 26 | import org.thingsboard.server.common.data.id.EntityId; |
29 | 27 | import org.thingsboard.server.common.data.id.TenantId; |
... | ... | @@ -33,7 +31,6 @@ import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
33 | 31 | import org.thingsboard.server.common.data.widget.WidgetTypeDetails; |
34 | 32 | import org.thingsboard.server.common.data.widget.WidgetsBundle; |
35 | 33 | import org.thingsboard.server.dao.dashboard.DashboardService; |
36 | -import org.thingsboard.server.dao.exception.DataValidationException; | |
37 | 34 | import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; |
38 | 35 | import org.thingsboard.server.dao.resource.TbResourceService; |
39 | 36 | import org.thingsboard.server.dao.rule.RuleChainService; |
... | ... | @@ -45,7 +42,6 @@ import java.nio.file.DirectoryStream; |
45 | 42 | import java.nio.file.Files; |
46 | 43 | import java.nio.file.Path; |
47 | 44 | import java.nio.file.Paths; |
48 | -import java.util.Base64; | |
49 | 45 | import java.util.Optional; |
50 | 46 | |
51 | 47 | import static org.thingsboard.server.service.install.DatabaseHelper.objectMapper; |
... | ... | @@ -196,29 +192,6 @@ public class InstallScripts { |
196 | 192 | } |
197 | 193 | } |
198 | 194 | |
199 | - public void loadSystemLwm2mResources() throws Exception { | |
200 | - Path modelsDir = Paths.get(getDataDir(), MODELS_DIR); | |
201 | - if (Files.isDirectory(modelsDir)) { | |
202 | - try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(modelsDir, path -> path.toString().endsWith(XML_EXT))) { | |
203 | - dirStream.forEach( | |
204 | - path -> { | |
205 | - try { | |
206 | - byte[] fileBytes = Files.readAllBytes(path); | |
207 | - TbResource resource = new TbResource(); | |
208 | - resource.setFileName(path.getFileName().toString()); | |
209 | - resource.setTenantId(TenantId.SYS_TENANT_ID); | |
210 | - resource.setResourceType(ResourceType.LWM2M_MODEL); | |
211 | - resource.setData(Base64.getEncoder().encodeToString(fileBytes)); | |
212 | - resourceService.saveResource(resource); | |
213 | - } catch (Exception e) { | |
214 | - throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", path.toString())); | |
215 | - } | |
216 | - } | |
217 | - ); | |
218 | - } | |
219 | - } | |
220 | - } | |
221 | - | |
222 | 195 | public void loadDashboards(TenantId tenantId, CustomerId customerId) throws Exception { |
223 | 196 | Path dashboardsDir = Paths.get(getDataDir(), JSON_DIR, DEMO_DIR, DASHBOARDS_DIR); |
224 | 197 | try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(dashboardsDir, path -> path.toString().endsWith(JSON_EXT))) { | ... | ... |
... | ... | @@ -449,26 +449,12 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
449 | 449 | case "3.2.2": |
450 | 450 | try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
451 | 451 | log.info("Updating schema ..."); |
452 | - try { | |
453 | - conn.createStatement().execute("CREATE TABLE IF NOT EXISTS resource ( " + | |
454 | - "id uuid NOT NULL CONSTRAINT resource_pkey PRIMARY KEY, " + | |
455 | - "created_time bigint NOT NULL, " + | |
456 | - "tenant_id uuid NOT NULL, " + | |
457 | - "title varchar(255) NOT NULL, " + | |
458 | - "resource_type varchar(32) NOT NULL, " + | |
459 | - "resource_key varchar(255) NOT NULL, " + | |
460 | - "search_text varchar(255), " + | |
461 | - "file_name varchar(255) NOT NULL, " + | |
462 | - "data varchar, " + | |
463 | - "CONSTRAINT resource_unq_key UNIQUE (tenant_id, resource_type, resource_key)" + | |
464 | - ");"); | |
465 | - | |
466 | - conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3003000;"); | |
467 | - installScripts.loadSystemLwm2mResources(); | |
468 | - } catch (Exception e) { | |
469 | - log.error("Failed updating schema!!!", e); | |
470 | - } | |
452 | + schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.2.2", SCHEMA_UPDATE_SQL); | |
453 | + loadSql(schemaUpdateFile, conn); | |
454 | + conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3003000;"); | |
471 | 455 | log.info("Schema updated."); |
456 | + } catch (Exception e) { | |
457 | + log.error("Failed updating schema!!!", e); | |
472 | 458 | } |
473 | 459 | break; |
474 | 460 | default: | ... | ... |
... | ... | @@ -176,14 +176,14 @@ CREATE TABLE IF NOT EXISTS rule_node_state ( |
176 | 176 | ); |
177 | 177 | |
178 | 178 | CREATE TABLE IF NOT EXISTS firmware ( |
179 | - id uuid NOT NULL CONSTRAINT firmware_pkey PRIMARY KEY, | |
180 | - created_time bigint NOT NULL, | |
181 | - tenant_id uuid NOT NULL, | |
182 | - title varchar(255) NOT NULL, | |
183 | - search_text varchar(255), | |
184 | - file_name varchar(255) NOT NULL, | |
185 | - content_type varchar(255) NOT NULL, | |
186 | - data bytea | |
179 | + id uuid NOT NULL CONSTRAINT firmware_pkey PRIMARY KEY, | |
180 | + created_time bigint NOT NULL, | |
181 | + tenant_id uuid NOT NULL, | |
182 | + title varchar(255) NOT NULL, | |
183 | + search_text varchar(255), | |
184 | + file_name varchar(255) NOT NULL, | |
185 | + content_type varchar(255) NOT NULL, | |
186 | + data bytea | |
187 | 187 | ); |
188 | 188 | |
189 | 189 | CREATE TABLE IF NOT EXISTS device_profile ( | ... | ... |