diff --git a/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql b/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql new file mode 100644 index 0000000000..758854f92b --- /dev/null +++ b/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql @@ -0,0 +1,9 @@ +-- +-- SPDX-FileCopyrightText: Copyright The ThingsBoard Authors +-- SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +-- SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 +-- + +-- Tenant-scoped paging over calculated fields (WHERE tenant_id = ? ORDER BY id LIMIT ?), issued for every tenant +-- when the calculated field actors initialize, otherwise scans the whole table and sorts on each call. +CREATE INDEX IF NOT EXISTS idx_calculated_field_tenant_id_id ON calculated_field(tenant_id, id); diff --git a/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java b/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java new file mode 100644 index 0000000000..1d91bc0e82 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors +// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 +package org.thingsboard.server.service.install.lts; + +import org.springframework.stereotype.Component; +import org.thingsboard.server.queue.util.TbCoreComponent; + +/** + * Registration-only migration with no {@link #apply()} (no data migration). + *
+ * {@link MigrationService} selects migrations from the injected {@link Migration} beans, not from the
+ * on-disk {@code data/upgrade/lts/
+ * It needs a slot of its own rather than {@code 4.4.0.0}: that version is already released, and
+ * {@link Version#isInRange} is exclusive at the lower bound, so an installation already on {@code 4.4.0.0}
+ * would never pick up a change added there.
+ *
+ * The dir/bean consistency (both ways) is guarded by a test in {@code MigrationIntegrationTest}.
+ */
+@Component
+@TbCoreComponent
+public class V4_4_1_0Migration implements Migration {
+
+ @Override
+ public String getVersion() {
+ return "4.4.1.0";
+ }
+}
diff --git a/application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java b/application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java
index 4d0e570e04..bbdb95c466 100644
--- a/application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java
+++ b/application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java
@@ -451,6 +451,25 @@ public class MigrationIntegrationTest extends AbstractControllerTest {
* older script legitimately behind. What may never happen is a script declaring a clause the fresh-install
* DDL does not. The single-row index is compared exactly: there is only ever one right form of it.
*/
+ @Test
+ public void appliesSchemaForV4410CreatesCalculatedFieldTenantIdIndex() {
+ // The test-context schema already ships the index (schema-entities-idx.sql), so drop it first to prove the
+ // 4.4.1.0 schema_update.sql re-creates it. The finally block restores it for the shared DaoSqlTest schema.
+ try {
+ jdbcTemplate.execute("DROP INDEX IF EXISTS idx_calculated_field_tenant_id_id");
+ assertFalse(indexExists("idx_calculated_field_tenant_id_id"));
+
+ migrationService.applyMigrations("4.4.0.0", "4.4.1.0", false, () -> {});
+
+ assertTrue(indexExists("idx_calculated_field_tenant_id_id"));
+ assertEquals("CREATE INDEX idx_calculated_field_tenant_id_id ON public.calculated_field USING btree (tenant_id, id)",
+ jdbcTemplate.queryForObject("SELECT indexdef FROM pg_indexes WHERE indexname = ?", String.class,
+ "idx_calculated_field_tenant_id_id"));
+ } finally {
+ jdbcTemplate.execute("CREATE INDEX IF NOT EXISTS idx_calculated_field_tenant_id_id ON calculated_field(tenant_id, id)");
+ }
+ }
+
@Test
public void everyTbClusterDdlCopyIsConsistentWithSchemaEntities() {
String schemaEntities = readResource("sql/schema-entities.sql");
@@ -707,6 +726,12 @@ public class MigrationIntegrationTest extends AbstractControllerTest {
return Boolean.TRUE.equals(exists);
}
+ private boolean indexExists(String indexName) {
+ Boolean exists = jdbcTemplate.queryForObject(
+ "SELECT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = ?)", Boolean.class, indexName);
+ return Boolean.TRUE.equals(exists);
+ }
+
private boolean constraintExists(String constraintName) {
Boolean exists = jdbcTemplate.queryForObject(
"SELECT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ?)", Boolean.class, constraintName);
diff --git a/dao/src/main/resources/sql/schema-entities-idx.sql b/dao/src/main/resources/sql/schema-entities-idx.sql
index 1cf38b7a03..bf1f2c3a27 100644
--- a/dao/src/main/resources/sql/schema-entities-idx.sql
+++ b/dao/src/main/resources/sql/schema-entities-idx.sql
@@ -181,3 +181,5 @@ CREATE INDEX IF NOT EXISTS idx_iot_hub_installed_item_tenant_id ON iot_hub_insta
CREATE INDEX IF NOT EXISTS idx_iot_hub_installed_item_item_type ON iot_hub_installed_item(tenant_id, item_type);
CREATE INDEX IF NOT EXISTS idx_iot_hub_installed_item_item_id ON iot_hub_installed_item(tenant_id, item_id);
+
+CREATE INDEX IF NOT EXISTS idx_calculated_field_tenant_id_id ON calculated_field(tenant_id, id);