forked from stephencelis/SQLite.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaChangerTests.swift
157 lines (127 loc) · 5.49 KB
/
SchemaChangerTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import XCTest
@testable import SQLite
class SchemaChangerTests: SQLiteTestCase {
var schemaChanger: SchemaChanger!
var schema: SchemaReader!
override func setUpWithError() throws {
try super.setUpWithError()
try createUsersTable()
try insertUsers("bob")
schema = SchemaReader(connection: db)
schemaChanger = SchemaChanger(connection: db)
}
func test_empty_migration_does_not_change_column_definitions() throws {
let previous = try schema.columnDefinitions(table: "users")
try schemaChanger.alter(table: "users") { _ in
}
let current = try schema.columnDefinitions(table: "users")
XCTAssertEqual(previous, current)
}
func test_empty_migration_does_not_change_index_definitions() throws {
let previous = try schema.indexDefinitions(table: "users")
try schemaChanger.alter(table: "users") { _ in
}
let current = try schema.indexDefinitions(table: "users")
XCTAssertEqual(previous, current)
}
func test_empty_migration_does_not_change_foreign_key_definitions() throws {
let previous = try schema.foreignKeys(table: "users")
try schemaChanger.alter(table: "users") { _ in
}
let current = try schema.foreignKeys(table: "users")
XCTAssertEqual(previous, current)
}
func test_empty_migration_does_not_change_the_row_count() throws {
let previous = try db.scalar(users.count)
try schemaChanger.alter(table: "users") { _ in
}
let current = try db.scalar(users.count)
XCTAssertEqual(previous, current)
}
func test_drop_column() throws {
try schemaChanger.alter(table: "users") { table in
table.drop(column: "age")
}
let columns = try schema.columnDefinitions(table: "users").map(\.name)
XCTAssertFalse(columns.contains("age"))
}
func test_drop_column_legacy() throws {
schemaChanger = .init(connection: db, version: .init(major: 3, minor: 24)) // DROP COLUMN introduced in 3.35.0
try schemaChanger.alter(table: "users") { table in
table.drop(column: "age")
}
let columns = try schema.columnDefinitions(table: "users").map(\.name)
XCTAssertFalse(columns.contains("age"))
}
func test_rename_column() throws {
try schemaChanger.alter(table: "users") { table in
table.rename(column: "age", to: "age2")
}
let columns = try schema.columnDefinitions(table: "users").map(\.name)
XCTAssertFalse(columns.contains("age"))
XCTAssertTrue(columns.contains("age2"))
}
func test_rename_column_legacy() throws {
schemaChanger = .init(connection: db, version: .init(major: 3, minor: 24)) // RENAME COLUMN introduced in 3.25.0
try schemaChanger.alter(table: "users") { table in
table.rename(column: "age", to: "age2")
}
let columns = try schema.columnDefinitions(table: "users").map(\.name)
XCTAssertFalse(columns.contains("age"))
XCTAssertTrue(columns.contains("age2"))
}
func test_add_column() throws {
let column = SQLite.Expression<String>("new_column")
let newColumn = ColumnDefinition(name: "new_column",
type: .TEXT,
nullable: true,
defaultValue: .stringLiteral("foo"))
try schemaChanger.alter(table: "users") { table in
table.add(column: newColumn)
}
let columns = try schema.columnDefinitions(table: "users")
XCTAssertTrue(columns.contains(newColumn))
XCTAssertEqual(try db.pluck(users.select(column))?[column], "foo")
}
func test_add_column_primary_key_fails() throws {
let newColumn = ColumnDefinition(name: "new_column",
primaryKey: .init(autoIncrement: false, onConflict: nil),
type: .TEXT)
XCTAssertThrowsError(try schemaChanger.alter(table: "users") { table in
table.add(column: newColumn)
}) { error in
if case SchemaChanger.Error.invalidColumnDefinition(_) = error {
XCTAssertEqual("Invalid column definition: can not add primary key column", error.localizedDescription)
} else {
XCTFail("invalid error: \(error)")
}
}
}
func test_drop_table() throws {
try schemaChanger.drop(table: "users")
XCTAssertThrowsError(try db.scalar(users.count)) { error in
if case Result.error(let message, _, _) = error {
XCTAssertEqual(message, "no such table: users")
} else {
XCTFail("unexpected error \(error)")
}
}
}
func test_drop_table_if_exists_true() throws {
try schemaChanger.drop(table: "xxx", ifExists: true)
}
func test_drop_table_if_exists_false() throws {
XCTAssertThrowsError(try schemaChanger.drop(table: "xxx", ifExists: false)) { error in
if case Result.error(let message, _, _) = error {
XCTAssertEqual(message, "no such table: xxx")
} else {
XCTFail("unexpected error \(error)")
}
}
}
func test_rename_table() throws {
try schemaChanger.rename(table: "users", to: "users_new")
let users_new = Table("users_new")
XCTAssertEqual((try db.scalar(users_new.count)) as Int, 1)
}
}