Skip to content

Commit 96f4b35

Browse files
committed
PRIMARY KEY cleanup
Let's enable defaults for primary keys, and let's not bother with a uniqueness parameter, since it's implied. Signed-off-by: Stephen Celis <[email protected]>
1 parent a705c40 commit 96f4b35

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

Documentation/Index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ The `create(table:)` function has several default parameters we can override.
282282
283283
The `column` function is used for a single column definition. It takes an [expression](#expressions) describing the column name and type, and accepts several parameters that map to various column constraints and clauses.
284284
285-
- `primaryKey` adds an `INTEGER PRIMARY KEY` constraint to a single column. (See the `primaryKey` function under [Table Constraints](#table-constraints) for non-integer primary keys).
285+
- `primaryKey` adds a `PRIMARY KEY` constraint to a single column.
286286
287287
``` swift
288288
t.column(id, primaryKey: true)
@@ -292,9 +292,11 @@ The `column` function is used for a single column definition. It takes an [expre
292292
// "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
293293
```
294294
295-
> _Note:_ The `primaryKey` parameter cannot be used alongside `defaultValue` or `references`. If you need to create a column that has a default value and is also a primary and/or foreign key, use the `primaryKey` and `foreignKey` functions mentioned under [Table Constraints](#table-constraints).
295+
> _Note:_ The `primaryKey` parameter cannot be used alongside `references`. If you need to create a column that has a default value and is also a primary and/or foreign key, use the `primaryKey` and `foreignKey` functions mentioned under [Table Constraints](#table-constraints).
296296
>
297-
> Primary keys cannot be optional (`Expression<Int64?>`).
297+
> Primary keys cannot be optional (_e.g._, `Expression<Int64?>`).
298+
>
299+
> Only an `INTEGER PRIMARY KEY` can take `.Autoincrement`.
298300
299301
- `unique` adds a `UNIQUE` constraint to the column. (See the `unique` function under [Table Constraints](#table-constraints) for uniqueness over multiple columns).
300302

SQLite Tests/SchemaTests.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ private let age = Expression<Int?>("age")
77
private let salary = Expression<Double>("salary")
88
private let admin = Expression<Bool>("admin")
99
private let manager_id = Expression<Int64?>("manager_id")
10-
private let uniqueIdentifier = Expression<UniqueIdentifier>("uniqueIdentifier")
1110

1211
class SchemaTests: XCTestCase {
1312

@@ -48,13 +47,22 @@ class SchemaTests: XCTestCase {
4847

4948
func test_createTable_column_nonIntegerPrimaryKey() {
5049
ExpectExecution(db, "CREATE TABLE \"users\" (\"email\" TEXT PRIMARY KEY NOT NULL)",
51-
db.create(table: users) { $0.column(email, primaryKey: true) }
50+
db.create(table: users) { t in
51+
t.column(email, primaryKey: true)
52+
}
5253
)
5354
}
54-
55-
func test_createTable_column_customTypePrimaryKey() {
56-
ExpectExecution(db, "CREATE TABLE \"users\" (\"uniqueIdentifier\" TEXT PRIMARY KEY NOT NULL)",
57-
db.create(table: users) { $0.column(uniqueIdentifier, primaryKey: true) }
55+
56+
func test_createTable_column_nonIntegerPrimaryKey_withDefaultValue() {
57+
let uuid = Expression<String>("uuid")
58+
let uuidgen: () -> Expression<String> = db.create(function: "uuidgen") {
59+
return NSUUID().UUIDString
60+
}
61+
62+
ExpectExecution(db, "CREATE TABLE \"users\" (\"uuid\" TEXT PRIMARY KEY NOT NULL DEFAULT (\"uuidgen\"()))",
63+
db.create(table: users) { t in
64+
t.column(uuid, primaryKey: true, defaultValue: uuidgen())
65+
}
5866
)
5967
}
6068

SQLite Tests/TestHelper.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,3 @@ func ExpectExecution(db: Database, SQL: String, statement: @autoclosure () -> St
5555
func ExpectExecution(db: Database, SQL: String, query: Query) {
5656
ExpectExecutions(db, [SQL: 1]) { _ in for _ in query {} }
5757
}
58-
59-
60-
public class UniqueIdentifier : NSUUID, Value {
61-
public class var declaredDatatype:String { get { return "TEXT" } }
62-
63-
public class func fromDatatypeValue(datatypeValue: String) -> UniqueIdentifier {
64-
return UniqueIdentifier(UUIDString: datatypeValue)!
65-
}
66-
67-
public var datatypeValue: String {
68-
return self.UUIDString
69-
}
70-
}

SQLite/Schema.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public final class SchemaBuilder {
164164
check: Expression<Bool>? = nil,
165165
defaultValue value: Expression<V>?
166166
) {
167-
column(name, nil, false, unique, check, value)
167+
column(name, nil, false, unique, check, value.map { wrap("", $0) })
168168
}
169169
170170
public func column<V: Value>(
@@ -182,7 +182,7 @@ public final class SchemaBuilder {
182182
check: Expression<Bool>? = nil,
183183
defaultValue value: Expression<V>?
184184
) {
185-
column(Expression<V>(name), nil, true, unique, check, value)
185+
column(Expression<V>(name), nil, true, unique, check, value.map { wrap("", $0) })
186186
}
187187
188188
public func column<V: Value>(
@@ -197,10 +197,10 @@ public final class SchemaBuilder {
197197
public func column<V: Value>(
198198
name: Expression<V>,
199199
primaryKey: Bool,
200-
unique: Bool = false,
201-
check: Expression<Bool>? = nil
200+
check: Expression<Bool>? = nil,
201+
defaultValue value: Expression<V>? = nil
202202
) {
203-
column(name, primaryKey ? .Default : nil, false, unique, check, nil, nil)
203+
column(name, primaryKey ? .Default : nil, false, false, check, value.map { wrap("", $0) }, nil)
204204
}
205205
206206
// MARK: - INTEGER Columns
@@ -218,10 +218,9 @@ public final class SchemaBuilder {
218218
public func column<V: Value where V.Datatype == Int64>(
219219
name: Expression<V>,
220220
primaryKey: PrimaryKey?,
221-
unique: Bool = false,
222221
check: Expression<Bool>? = nil
223222
) {
224-
column(name, primaryKey, false, unique, check, nil, nil)
223+
column(name, primaryKey, false, false, check, nil, nil)
225224
}
226225
227226
// MARK: REFERENCES

0 commit comments

Comments
 (0)