Skip to content

Commit 1c44076

Browse files
committed
Add drop(index:)
1 parent c812caf commit 1c44076

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Sources/SQLite/Schema/SchemaChanger.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class SchemaChanger: CustomStringConvertible {
4545
case addColumn(ColumnDefinition)
4646
case addIndex(IndexDefinition, ifNotExists: Bool)
4747
case dropColumn(String)
48+
case dropIndex(String, ifExists: Bool)
4849
case renameColumn(String, String)
4950
case renameTable(String)
5051
case createTable(columns: [ColumnDefinition], ifNotExists: Bool)
@@ -60,6 +61,8 @@ public class SchemaChanger: CustomStringConvertible {
6061
return "ALTER TABLE \(table.quote()) RENAME COLUMN \(from.quote()) TO \(to.quote())"
6162
case .dropColumn(let column) where SQLiteFeature.dropColumn.isSupported(by: version):
6263
return "ALTER TABLE \(table.quote()) DROP COLUMN \(column.quote())"
64+
case .dropIndex(let name, let ifExists):
65+
return "DROP INDEX \(ifExists ? " IF EXISTS " : "") \(name.quote())"
6366
case .createTable(let columns, let ifNotExists):
6467
return "CREATE TABLE \(ifNotExists ? " IF NOT EXISTS " : "") \(table.quote()) (" +
6568
columns.map { $0.toSQL() }.joined(separator: ", ") +
@@ -111,6 +114,10 @@ public class SchemaChanger: CustomStringConvertible {
111114
operations.append(.dropColumn(column))
112115
}
113116

117+
public func drop(index: String, ifExists: Bool = false) {
118+
operations.append(.dropIndex(index, ifExists: ifExists))
119+
}
120+
114121
public func rename(column: String, to: String) {
115122
operations.append(.renameColumn(column, to))
116123
}
@@ -324,8 +331,9 @@ extension TableDefinition {
324331
func apply(_ operation: SchemaChanger.Operation?) -> TableDefinition {
325332
switch operation {
326333
case .none: return self
327-
case .createTable, .addIndex: fatalError()
334+
case .createTable, .addIndex, .dropIndex: fatalError()
328335
case .addColumn: fatalError("Use 'ALTER TABLE ADD COLUMN (...)'")
336+
329337
case .dropColumn(let column):
330338
return TableDefinition(name: name,
331339
columns: columns.filter { $0.name != column },

Tests/SQLiteTests/Schema/SchemaChangerTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,44 @@ class SchemaChangerTests: SQLiteTestCase {
124124
}
125125
}
126126

127+
func test_drop_index() throws {
128+
try db.execute("""
129+
CREATE INDEX age_index ON users(age)
130+
""")
131+
132+
try schemaChanger.alter(table: "users") { table in
133+
table.drop(index: "age_index")
134+
}
135+
let indexes = try schema.indexDefinitions(table: "users").filter { !$0.isInternal }
136+
XCTAssertEqual(0, indexes.count)
137+
}
138+
139+
func test_drop_index_if_exists() throws {
140+
try db.execute("""
141+
CREATE INDEX age_index ON users(age)
142+
""")
143+
144+
try schemaChanger.alter(table: "users") { table in
145+
table.drop(index: "age_index")
146+
}
147+
148+
try schemaChanger.alter(table: "users") { table in
149+
table.drop(index: "age_index", ifExists: true)
150+
}
151+
152+
XCTAssertThrowsError(
153+
try schemaChanger.alter(table: "users") { table in
154+
table.drop(index: "age_index", ifExists: false)
155+
}
156+
) { error in
157+
if case Result.error(let message, _, _) = error {
158+
XCTAssertEqual(message, "no such index: age_index")
159+
} else {
160+
XCTFail("unexpected error \(error)")
161+
}
162+
}
163+
}
164+
127165
func test_drop_table() throws {
128166
try schemaChanger.drop(table: "users")
129167
XCTAssertThrowsError(try db.scalar(users.count)) { error in

0 commit comments

Comments
 (0)