Skip to content

Commit eca9313

Browse files
authored
Fix database insert for Entity and Device registry (#4160)
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
1 parent 39be820 commit eca9313

File tree

6 files changed

+25
-22
lines changed

6 files changed

+25
-22
lines changed

Sources/Shared/Database/AppDeviceRegistryTable.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ import GRDB
44
final class AppDeviceRegistryTable: DatabaseTableProtocol {
55
func createIfNeeded(database: DatabaseQueue) throws {
66
let shouldCreateTable = try database.read { db in
7-
try !db.tableExists(GRDBDatabaseTable.appDeviceRegistry.rawValue)
7+
try !db.tableExists(GRDBDatabaseTable.deviceRegistry.rawValue)
88
}
99
if shouldCreateTable {
1010
try database.write { db in
11-
try db.create(table: GRDBDatabaseTable.appDeviceRegistry.rawValue) { t in
12-
t.uniqueKey([
13-
DatabaseTables.DeviceRegistry.serverId.rawValue,
14-
DatabaseTables.DeviceRegistry.deviceId.rawValue,
15-
])
11+
try db.create(table: GRDBDatabaseTable.deviceRegistry.rawValue) { t in
1612
// Core identifiers
1713
t.column(DatabaseTables.DeviceRegistry.serverId.rawValue, .text).notNull().indexed()
1814
t.column(DatabaseTables.DeviceRegistry.deviceId.rawValue, .text).notNull().indexed()
@@ -51,6 +47,12 @@ final class AppDeviceRegistryTable: DatabaseTableProtocol {
5147
// Relationships
5248
t.column(DatabaseTables.DeviceRegistry.primaryConfigEntry.rawValue, .text)
5349
t.column(DatabaseTables.DeviceRegistry.viaDeviceID.rawValue, .text)
50+
51+
// ID
52+
t.uniqueKey([
53+
DatabaseTables.DeviceRegistry.serverId.rawValue,
54+
DatabaseTables.DeviceRegistry.deviceId.rawValue,
55+
])
5456
}
5557
}
5658
}

Sources/Shared/Database/AppEntityRegistryTable.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@ import GRDB
44
final class AppEntityRegistryTable: DatabaseTableProtocol {
55
func createIfNeeded(database: DatabaseQueue) throws {
66
let shouldCreateTable = try database.read { db in
7-
try !db.tableExists(GRDBDatabaseTable.appEntityRegistry.rawValue)
7+
try !db.tableExists(GRDBDatabaseTable.entityRegistry.rawValue)
88
}
99
if shouldCreateTable {
1010
try database.write { db in
11-
try db.create(table: GRDBDatabaseTable.appEntityRegistry.rawValue) { t in
12-
// Primary key (composite of serverId-uniqueId)
13-
t.column(DatabaseTables.EntityRegistry.id.rawValue, .text).primaryKey().notNull()
14-
11+
try db.create(table: GRDBDatabaseTable.entityRegistry.rawValue) { t in
1512
// Core identifiers
1613
t.column(DatabaseTables.EntityRegistry.serverId.rawValue, .text).notNull().indexed()
1714
t.column(DatabaseTables.EntityRegistry.uniqueId.rawValue, .text).notNull().indexed()
1815

19-
// Ensure the combination is unique
20-
t.uniqueKey([
21-
DatabaseTables.EntityRegistry.serverId.rawValue,
22-
DatabaseTables.EntityRegistry.uniqueId.rawValue,
23-
])
2416
t.column(DatabaseTables.EntityRegistry.entityId.rawValue, .text).indexed()
2517
t.column(DatabaseTables.EntityRegistry.platform.rawValue, .text)
2618
t.column(DatabaseTables.EntityRegistry.configEntryId.rawValue, .text)
@@ -51,6 +43,12 @@ final class AppEntityRegistryTable: DatabaseTableProtocol {
5143
t.column(DatabaseTables.EntityRegistry.options.rawValue, .jsonText)
5244
t.column(DatabaseTables.EntityRegistry.translationKey.rawValue, .text)
5345
t.column(DatabaseTables.EntityRegistry.hasEntityName.rawValue, .boolean)
46+
47+
// ID
48+
t.uniqueKey([
49+
DatabaseTables.EntityRegistry.serverId.rawValue,
50+
DatabaseTables.EntityRegistry.uniqueId.rawValue,
51+
])
5452
}
5553
}
5654
}

Sources/Shared/Database/DatabaseTables.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public enum GRDBDatabaseTable: String {
66
case assistPipelines
77
case carPlayConfig
88
case appEntityRegistryListForDisplay
9-
case appEntityRegistry
10-
case appDeviceRegistry
9+
case entityRegistry
10+
case deviceRegistry
1111
case appPanel
1212
case customWidget
1313
case appArea

Sources/Shared/Environment/AppDatabaseUpdater.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol {
212212
"error": error.localizedDescription,
213213
]
214214
))
215+
assertionFailure("Failed to save areas in database: \(error)")
215216
}
216217
}
217218

@@ -248,6 +249,7 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol {
248249
"error": error.localizedDescription,
249250
]
250251
))
252+
assertionFailure("Failed to save EntityRegistryListForDisplay in database: \(error)")
251253
}
252254
}
253255

@@ -281,6 +283,7 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol {
281283
"error": error.localizedDescription,
282284
]
283285
))
286+
assertionFailure("Failed to save entity registry in database: \(error)")
284287
}
285288
}
286289

@@ -314,6 +317,7 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol {
314317
"error": error.localizedDescription,
315318
]
316319
))
320+
assertionFailure("Failed to save device registry in database: \(error)")
317321
}
318322
}
319323

Sources/Shared/Environment/DeviceRegistryEntry.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public struct DeviceRegistryEntry: Codable, HADataDecodable {
113113
// MARK: - Database Model
114114

115115
public struct AppDeviceRegistry: Codable, FetchableRecord, PersistableRecord {
116+
public static var databaseTableName: String = GRDBDatabaseTable.deviceRegistry.rawValue
116117
public let serverId: String
117118

118119
public let deviceId: String

Sources/Shared/Environment/EntityRegistry.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public struct EntityRegistryEntry: Codable, HADataDecodable {
132132
// MARK: - Database Model
133133

134134
public struct AppEntityRegistry: Codable, FetchableRecord, PersistableRecord {
135+
public static var databaseTableName: String = GRDBDatabaseTable.entityRegistry.rawValue
136+
135137
public let serverId: String
136138

137139
// All EntityRegistryEntry fields
@@ -196,10 +198,6 @@ public struct AppEntityRegistry: Codable, FetchableRecord, PersistableRecord {
196198
self.hasEntityName = registry.hasEntityName
197199
}
198200

199-
public var id: String {
200-
"\(serverId)-\(uniqueId)"
201-
}
202-
203201
// Computed helpers (same as EntityRegistryEntry)
204202
public var displayName: String {
205203
name ?? originalName ?? entityId ?? "-"

0 commit comments

Comments
 (0)