Skip to content

Commit c812caf

Browse files
committed
Make ForeignKey public, rename fields for clarity
1 parent 9dc478c commit c812caf

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

Sources/SQLite/Schema/SchemaDefinitions.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,24 @@ public struct ColumnDefinition: Equatable {
117117
}
118118

119119
public struct ForeignKey: Equatable {
120-
let table: String
121-
let column: String
122-
let primaryKey: String?
120+
let fromColumn: String
121+
let toTable: String
122+
// when null, use primary key of "toTable"
123+
let toColumn: String?
123124
let onUpdate: String?
124125
let onDelete: String?
126+
127+
public init(toTable: String, toColumn: String? = nil, onUpdate: String? = nil, onDelete: String? = nil) {
128+
self.init(fromColumn: "", toTable: toTable, toColumn: toColumn, onUpdate: onUpdate, onDelete: onDelete)
129+
}
130+
131+
public init(fromColumn: String, toTable: String, toColumn: String? = nil, onUpdate: String? = nil, onDelete: String? = nil) {
132+
self.fromColumn = fromColumn
133+
self.toTable = toTable
134+
self.toColumn = toColumn
135+
self.onUpdate = onUpdate
136+
self.onDelete = onDelete
137+
}
125138
}
126139

127140
public let name: String
@@ -400,8 +413,8 @@ extension ColumnDefinition.ForeignKey {
400413
func toSQL() -> String {
401414
([
402415
"REFERENCES",
403-
table.quote(),
404-
primaryKey.map { "(\($0.quote()))" },
416+
toTable.quote(),
417+
toColumn.map { "(\($0.quote()))" },
405418
onUpdate.map { "ON UPDATE \($0)" },
406419
onDelete.map { "ON DELETE \($0)" }
407420
] as [String?]).compactMap { $0 }

Sources/SQLite/Schema/SchemaReader.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SchemaReader {
1919
}
2020

2121
let foreignKeys: [String: [ColumnDefinition.ForeignKey]] =
22-
Dictionary(grouping: try foreignKeys(table: table), by: { $0.column })
22+
Dictionary(grouping: try foreignKeys(table: table), by: { $0.fromColumn })
2323

2424
let columnDefinitions = try connection.prepareRowIterator("PRAGMA table_info(\(table.quote()))")
2525
.map { (row: Row) -> ColumnDefinition in
@@ -111,9 +111,9 @@ public class SchemaReader {
111111
try connection.prepareRowIterator("PRAGMA foreign_key_list(\(table.quote()))")
112112
.map { row in
113113
ColumnDefinition.ForeignKey(
114-
table: row[ForeignKeyListTable.tableColumn],
115-
column: row[ForeignKeyListTable.fromColumn],
116-
primaryKey: row[ForeignKeyListTable.toColumn],
114+
fromColumn: row[ForeignKeyListTable.fromColumn],
115+
toTable: row[ForeignKeyListTable.tableColumn],
116+
toColumn: row[ForeignKeyListTable.toColumn],
117117
onUpdate: row[ForeignKeyListTable.onUpdateColumn] == TableBuilder.Dependency.noAction.rawValue
118118
? nil : row[ForeignKeyListTable.onUpdateColumn],
119119
onDelete: row[ForeignKeyListTable.onDeleteColumn] == TableBuilder.Dependency.noAction.rawValue

Tests/SQLiteTests/Schema/SchemaChangerTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,38 @@ class SchemaChangerTests: SQLiteTestCase {
269269
table.add(index: .init(table: "foo", name: "name_index", unique: true, columns: ["name"], indexSQL: nil))
270270
}
271271
}
272+
273+
func test_create_table_with_foreign_key_reference() throws {
274+
try schemaChanger.create(table: "foo") { table in
275+
table.add(column: .init(name: "id", primaryKey: .init(autoIncrement: true), type: .INTEGER))
276+
}
277+
278+
try schemaChanger.create(table: "bars") { table in
279+
table.add(column: .init(name: "id", primaryKey: .init(autoIncrement: true), type: .INTEGER))
280+
table.add(column: .init(name: "foo_id",
281+
type: .INTEGER,
282+
nullable: false,
283+
references: .init(toTable: "foo", toColumn: "id")))
284+
}
285+
286+
let barColumns = try schema.columnDefinitions(table: "bars")
287+
288+
XCTAssertEqual([
289+
ColumnDefinition(name: "id",
290+
primaryKey: .init(autoIncrement: true, onConflict: nil),
291+
type: .INTEGER,
292+
nullable: true,
293+
unique: false,
294+
defaultValue: .NULL,
295+
references: nil),
296+
297+
ColumnDefinition(name: "foo_id",
298+
primaryKey: nil,
299+
type: .INTEGER,
300+
nullable: false,
301+
unique: false,
302+
defaultValue: .NULL,
303+
references: .init(fromColumn: "foo_id", toTable: "foo", toColumn: "id", onUpdate: nil, onDelete: nil))
304+
], barColumns)
305+
}
272306
}

Tests/SQLiteTests/Schema/SchemaDefinitionsTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ColumnDefinitionTests: XCTestCase {
1111

1212
("\"other_id\" INTEGER NOT NULL REFERENCES \"other_table\" (\"some_id\")",
1313
ColumnDefinition(name: "other_id", primaryKey: nil, type: .INTEGER, nullable: false, defaultValue: .NULL,
14-
references: .init(table: "other_table", column: "", primaryKey: "some_id", onUpdate: nil, onDelete: nil))),
14+
references: .init(fromColumn: "", toTable: "other_table", toColumn: "some_id", onUpdate: nil, onDelete: nil))),
1515

1616
("\"text\" TEXT",
1717
ColumnDefinition(name: "text", primaryKey: nil, type: .TEXT, nullable: true, defaultValue: .NULL, references: nil)),
@@ -245,9 +245,9 @@ class ForeignKeyDefinitionTests: XCTestCase {
245245
func test_toSQL() {
246246
XCTAssertEqual(
247247
ColumnDefinition.ForeignKey(
248-
table: "foo",
249-
column: "bar",
250-
primaryKey: "bar_id",
248+
fromColumn: "bar",
249+
toTable: "foo",
250+
toColumn: "bar_id",
251251
onUpdate: nil,
252252
onDelete: "SET NULL"
253253
).toSQL(), """

Tests/SQLiteTests/Schema/SchemaReaderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SchemaReaderTests: SQLiteTestCase {
5454
nullable: true,
5555
unique: false,
5656
defaultValue: .NULL,
57-
references: .init(table: "users", column: "manager_id", primaryKey: "id", onUpdate: nil, onDelete: nil)),
57+
references: .init(fromColumn: "manager_id", toTable: "users", toColumn: "id", onUpdate: nil, onDelete: nil)),
5858
ColumnDefinition(name: "created_at",
5959
primaryKey: nil,
6060
type: .NUMERIC,
@@ -194,7 +194,7 @@ class SchemaReaderTests: SQLiteTestCase {
194194

195195
let foreignKeys = try schemaReader.foreignKeys(table: "test_links")
196196
XCTAssertEqual(foreignKeys, [
197-
.init(table: "users", column: "test_id", primaryKey: "id", onUpdate: nil, onDelete: nil)
197+
.init(fromColumn: "test_id", toTable: "users", toColumn: "id", onUpdate: nil, onDelete: nil)
198198
])
199199
}
200200

0 commit comments

Comments
 (0)