Skip to content

Commit cdaade1

Browse files
committed
Respect ifNotExists for index
1 parent f2a8684 commit cdaade1

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Sources/SQLite/Schema/SchemaChanger.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class SchemaChanger: CustomStringConvertible {
4343

4444
public enum Operation {
4545
case addColumn(ColumnDefinition)
46-
case addIndex(IndexDefinition)
46+
case addIndex(IndexDefinition, ifNotExists: Bool)
4747
case dropColumn(String)
4848
case renameColumn(String, String)
4949
case renameTable(String)
@@ -54,12 +54,8 @@ public class SchemaChanger: CustomStringConvertible {
5454
switch self {
5555
case .addColumn(let definition):
5656
return "ALTER TABLE \(table.quote()) ADD COLUMN \(definition.toSQL())"
57-
case .addIndex(let definition):
58-
let unique = definition.unique ? "UNIQUE" : ""
59-
let columns = definition.columns.joined(separator: ", ")
60-
let `where` = definition.where.map { " WHERE " + $0 } ?? ""
61-
62-
return "CREATE \(unique) INDEX \(definition.name) ON \(definition.table) (\(columns)) \(`where`)"
57+
case .addIndex(let definition, let ifNotExists):
58+
return definition.toSQL(ifNotExists: ifNotExists)
6359
case .renameColumn(let from, let to) where SQLiteFeature.renameColumn.isSupported(by: version):
6460
return "ALTER TABLE \(table.quote()) RENAME COLUMN \(from.quote()) TO \(to.quote())"
6561
case .dropColumn(let column) where SQLiteFeature.dropColumn.isSupported(by: version):
@@ -152,7 +148,7 @@ public class SchemaChanger: CustomStringConvertible {
152148
precondition(!columnDefinitions.isEmpty)
153149
return [
154150
.createTable(columns: columnDefinitions, ifNotExists: ifNotExists)
155-
] + indexDefinitions.map { .addIndex($0) }
151+
] + indexDefinitions.map { .addIndex($0, ifNotExists: ifNotExists) }
156152
}
157153

158154
private func columnName<T>(for expression: Expression<T>) -> String {

Tests/SQLiteTests/Schema/SchemaChangerTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,18 @@ class SchemaChangerTests: SQLiteTestCase {
252252
}
253253
}
254254
}
255+
256+
func test_create_table_if_not_exists_with_index() throws {
257+
try schemaChanger.create(table: "foo") { table in
258+
table.add(column: .init(name: "id", primaryKey: .init(autoIncrement: true), type: .INTEGER))
259+
table.add(column: .init(name: "name", type: .TEXT))
260+
table.add(index: .init(table: "foo", name: "name_index", unique: true, columns: ["name"], indexSQL: nil))
261+
}
262+
263+
// ifNotExists needs to apply to index creation as well
264+
try schemaChanger.create(table: "foo", ifNotExists: true) { table in
265+
table.add(column: .init(name: "id", primaryKey: .init(autoIncrement: true), type: .INTEGER))
266+
table.add(index: .init(table: "foo", name: "name_index", unique: true, columns: ["name"], indexSQL: nil))
267+
}
268+
}
255269
}

0 commit comments

Comments
 (0)