Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SQLite/Schema/SchemaReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SchemaReader {
.map { (row: Row) -> ColumnDefinition in
ColumnDefinition(
name: row[TableInfoTable.nameColumn],
primaryKey: row[TableInfoTable.primaryKeyColumn] == 1 ?
primaryKey: (row[TableInfoTable.primaryKeyColumn] ?? 0) > 0 ?
try parsePrimaryKey(column: row[TableInfoTable.nameColumn]) : nil,
type: ColumnDefinition.Affinity(row[TableInfoTable.typeColumn]),
nullable: row[TableInfoTable.notNullColumn] == 0,
Expand Down
37 changes: 37 additions & 0 deletions Tests/SQLiteTests/Schema/SchemaReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ class SchemaReaderTests: SQLiteTestCase {
)
}

func test_columnDefinitions_composite_primary_keys() throws {
try db.run("""
CREATE TABLE t (
col1 INTEGER,
col2 INTEGER,
col3 INTEGER,
PRIMARY KEY (col1, col2)
);
""")

XCTAssertEqual(
try schemaReader.columnDefinitions(table: "t"), [
ColumnDefinition(
name: "col1",
primaryKey: .init(autoIncrement: false),
type: .INTEGER,
nullable: true,
defaultValue: .NULL,
references: nil),
ColumnDefinition(
name: "col2",
primaryKey: .init(autoIncrement: false),
type: .INTEGER,
nullable: true,
defaultValue: .NULL,
references: nil),
ColumnDefinition(
name: "col3",
primaryKey: nil,
type: .INTEGER,
nullable: true,
defaultValue: .NULL,
references: nil)
]
)
}

func test_indexDefinitions_no_index() throws {
let indexes = try schemaReader.indexDefinitions(table: "users")
XCTAssertTrue(indexes.isEmpty)
Expand Down