Skip to content

Commit 08bed02

Browse files
committed
Implement add(index:)
1 parent 1c44076 commit 08bed02

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Sources/SQLite/Schema/SchemaChanger.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public class SchemaChanger: CustomStringConvertible {
110110
operations.append(.addColumn(column))
111111
}
112112

113+
public func add(index: IndexDefinition, ifNotExists: Bool = false) {
114+
operations.append(.addIndex(index, ifNotExists: ifNotExists))
115+
}
116+
113117
public func drop(column: String) {
114118
operations.append(.dropColumn(column))
115119
}

Tests/SQLiteTests/Schema/SchemaChangerTests.swift

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

127+
func test_add_index() throws {
128+
try schemaChanger.alter(table: "users") { table in
129+
table.add(index: .init(table: table.name, name: "age_index", unique: false, columns: ["age"], indexSQL: nil))
130+
}
131+
132+
let indexes = try schema.indexDefinitions(table: "users").filter { !$0.isInternal }
133+
XCTAssertEqual([
134+
IndexDefinition(table: "users",
135+
name: "age_index",
136+
unique: false,
137+
columns: ["age"],
138+
where: nil,
139+
orders: nil,
140+
origin: .createIndex)
141+
], indexes)
142+
}
143+
144+
func test_add_index_if_not_exists() throws {
145+
let index = IndexDefinition(table: "users", name: "age_index", unique: false, columns: ["age"], indexSQL: nil)
146+
try schemaChanger.alter(table: "users") { table in
147+
table.add(index: index)
148+
}
149+
150+
try schemaChanger.alter(table: "users") { table in
151+
table.add(index: index, ifNotExists: true)
152+
}
153+
154+
XCTAssertThrowsError(
155+
try schemaChanger.alter(table: "users") { table in
156+
table.add(index: index, ifNotExists: false)
157+
}
158+
)
159+
}
160+
127161
func test_drop_index() throws {
128162
try db.execute("""
129163
CREATE INDEX age_index ON users(age)

0 commit comments

Comments
 (0)