mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-18 02:46:38 -05:00
<!-- 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. -->
41 lines
1.9 KiB
Swift
41 lines
1.9 KiB
Swift
import Foundation
|
|
import GRDB
|
|
|
|
final class HAppEntityTable: DatabaseTableProtocol {
|
|
// TODO: Create an object that can conform to database creation protocol and auto create/update tables
|
|
func createIfNeeded(database: DatabaseQueue) throws {
|
|
let shouldCreateTable = try database.read { db in
|
|
try !db.tableExists(GRDBDatabaseTable.HAAppEntity.rawValue)
|
|
}
|
|
if shouldCreateTable {
|
|
try database.write { db in
|
|
try db.create(table: GRDBDatabaseTable.HAAppEntity.rawValue) { t in
|
|
t.primaryKey(DatabaseTables.AppEntity.id.rawValue, .text).notNull()
|
|
t.column(DatabaseTables.AppEntity.entityId.rawValue, .text).notNull()
|
|
t.column(DatabaseTables.AppEntity.serverId.rawValue, .text).notNull()
|
|
t.column(DatabaseTables.AppEntity.domain.rawValue, .text).notNull()
|
|
t.column(DatabaseTables.AppEntity.name.rawValue, .text).notNull()
|
|
t.column(DatabaseTables.AppEntity.icon.rawValue, .text)
|
|
t.column(DatabaseTables.AppEntity.rawDeviceClass.rawValue, .text)
|
|
}
|
|
}
|
|
} else {
|
|
// In case a new column is added to the table, we need to alter the table
|
|
try database.write { db in
|
|
for column in DatabaseTables.AppEntity.allCases {
|
|
let shouldCreateTable = try !db.columns(in: GRDBDatabaseTable.HAAppEntity.rawValue)
|
|
.contains { columnInfo in
|
|
columnInfo.name == column.rawValue
|
|
}
|
|
|
|
if shouldCreateTable {
|
|
try db.alter(table: GRDBDatabaseTable.HAAppEntity.rawValue) { tableAlteration in
|
|
tableAlteration.add(column: column.rawValue)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|