Browse Source

Migrate user settings to jsonb

pull/8522/head
ViacheslavKlimov 3 years ago
parent
commit
ad4e212edf
  1. 29
      application/src/main/data/upgrade/3.6.2/schema_update.sql
  2. 3
      application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java
  3. 3
      application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java
  4. 3
      application/src/main/java/org/thingsboard/server/service/notification/channels/MobileAppNotificationChannel.java
  5. 8
      dao/src/main/java/org/thingsboard/server/dao/model/sql/UserSettingsEntity.java
  6. 3
      dao/src/main/java/org/thingsboard/server/dao/sql/user/UserRepository.java
  7. 2
      dao/src/main/resources/sql/schema-entities-idx.sql
  8. 2
      dao/src/main/resources/sql/schema-entities.sql

29
application/src/main/data/upgrade/3.6.2/schema_update.sql

@ -0,0 +1,29 @@
--
-- Copyright © 2016-2023 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.
--
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;
$$;
CREATE INDEX IF NOT EXISTS idx_user_settings_mobile_fcm_token ON user_settings ((settings ->> 'fcmToken')) WHERE type = 'MOBILE';

3
application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java

@ -277,6 +277,9 @@ public class ThingsboardInstallService {
log.info("Skipping images migration. Run the upgrade with fromVersion as '3.6.2-images' to migrate");
}
//TODO DON'T FORGET to update switch statement in the CacheCleanupService if you need to clear the cache
case "3.6.2":
log.info("Upgrading ThingsBoard from version 3.6.2 to 3.6.3 ...");
databaseEntitiesUpgradeService.upgradeDatabase("3.6.2");
break;
default:
throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion);

3
application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java

@ -772,6 +772,9 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
}
});
break;
case "3.6.2":
updateSchema("3.6.2", 3006002, "3.6.3", 3006003, null);
break;
default:
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
}

3
application/src/main/java/org/thingsboard/server/service/notification/channels/MobileAppNotificationChannel.java

@ -53,7 +53,8 @@ public class MobileAppNotificationChannel implements NotificationChannel<User, M
firebaseService.sendMessage(ctx.getTenantId(), config.getFirebaseServiceAccountCredentials(),
fcmToken, processedTemplate.getSubject(), processedTemplate.getBody());
} catch (FirebaseMessagingException e) {
if (e.getMessagingErrorCode() == MessagingErrorCode.UNREGISTERED) {
MessagingErrorCode errorCode = e.getMessagingErrorCode();
if (errorCode == MessagingErrorCode.UNREGISTERED || errorCode == MessagingErrorCode.INVALID_ARGUMENT) {
// the token is no longer valid
mobileInfo.setFcmToken(null);
userService.saveMobileInfo(recipient.getTenantId(), recipient.getId(), mobileInfo);

8
dao/src/main/java/org/thingsboard/server/dao/model/sql/UserSettingsEntity.java

@ -26,7 +26,7 @@ import org.thingsboard.server.common.data.settings.UserSettingsCompositeKey;
import org.thingsboard.server.common.data.settings.UserSettingsType;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.model.ToData;
import org.thingsboard.server.dao.util.mapping.JsonStringType;
import org.thingsboard.server.dao.util.mapping.JsonBinaryType;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -37,7 +37,7 @@ import java.util.UUID;
@Data
@NoArgsConstructor
@TypeDef(name = "json", typeClass = JsonStringType.class)
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
@Entity
@Table(name = ModelConstants.USER_SETTINGS_TABLE_NAME)
@IdClass(UserSettingsCompositeKey.class)
@ -49,8 +49,8 @@ public class UserSettingsEntity implements ToData<UserSettings> {
@Id
@Column(name = ModelConstants.USER_SETTINGS_TYPE_PROPERTY)
private String type;
@Type(type = "json")
@Column(name = ModelConstants.USER_SETTINGS_SETTINGS)
@Type(type = "jsonb")
@Column(name = ModelConstants.USER_SETTINGS_SETTINGS, columnDefinition = "jsonb")
private JsonNode settings;
public UserSettingsEntity(UserSettings userSettings) {

3
dao/src/main/java/org/thingsboard/server/dao/sql/user/UserRepository.java

@ -73,7 +73,8 @@ public interface UserRepository extends JpaRepository<UserEntity, UUID> {
Long countByTenantId(UUID tenantId);
@Query(value = "UPDATE user_settings SET settings = (settings::jsonb - 'fcmToken')::text WHERE type = 'MOBILE'", nativeQuery = true)
@Query(value = "UPDATE user_settings SET settings = settings - 'fcmToken' " +
"WHERE type = 'MOBILE' AND (settings ->> 'fcmToken') = :fcmToken", nativeQuery = true)
@Modifying
@Transactional
void unassignFcmToken(@Param("fcmToken") String fcmToken);

2
dao/src/main/resources/sql/schema-entities-idx.sql

@ -129,3 +129,5 @@ CREATE INDEX IF NOT EXISTS idx_resource_etag ON resource(tenant_id, etag);
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 INDEX IF NOT EXISTS idx_user_settings_mobile_fcm_token ON user_settings ((settings ->> 'fcmToken')) WHERE type = 'MOBILE';

2
dao/src/main/resources/sql/schema-entities.sql

@ -871,7 +871,7 @@ CREATE TABLE IF NOT EXISTS notification (
CREATE TABLE IF NOT EXISTS user_settings (
user_id uuid NOT NULL,
type VARCHAR(50) NOT NULL,
settings varchar(10000),
settings jsonb,
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES tb_user(id) ON DELETE CASCADE,
CONSTRAINT user_settings_pkey PRIMARY KEY (user_id, type)
);

Loading…
Cancel
Save