Skip to content

Commit c826e75

Browse files
committed
fix non-int primary key; passing tests
1 parent 789ba49 commit c826e75

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extension PostgreSQLDatabase: QuerySupporting {
6565
values.append(row.value)
6666
}
6767
insert.values.append(values)
68+
insert.returning.append(.all)
6869
query = .insert(insert)
6970
case ._select:
7071
var select: PostgreSQLSelect = .select()

Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extension PostgreSQLDatabase: SchemaSupporting {
3939
constraints.append(.notNull)
4040
}
4141

42-
// FIXME: is array support
4342
let isArray: Bool
4443
if let array = type as? AnyArray.Type {
4544
type = array.anyElementType
@@ -56,19 +55,22 @@ extension PostgreSQLDatabase: SchemaSupporting {
5655

5756
if isIdentifier {
5857
if _globalEnableIdentityColumns {
58+
let pkDefault: PostgreSQLPrimaryKeyDefault?
59+
// create a unique name for the primary key since it will be added
60+
// as a separate index.
61+
let unique: String
62+
if let table = column.table {
63+
unique = table.identifier.string + "." + column.identifier.string
64+
} else {
65+
unique = column.identifier.string
66+
}
5967
switch dataType {
6068
case .smallint, .integer, .bigint:
61-
// create a unique name for the primary key since it will be added
62-
// as a separate index.
63-
let unique: String
64-
if let table = column.table {
65-
unique = table.identifier.string + "." + column.identifier.string
66-
} else {
67-
unique = column.identifier.string
68-
}
69-
constraints.append(.primaryKey(default: .generated(.byDefault), identifier: .identifier("pk:\(unique)")))
70-
default: break
69+
pkDefault = .generated(.byDefault)
70+
default:
71+
pkDefault = nil
7172
}
73+
constraints.append(.primaryKey(default: pkDefault, identifier: .identifier("pk:\(unique)")))
7274
} else {
7375
switch dataType {
7476
case .smallint: dataType = .smallserial
@@ -79,6 +81,10 @@ extension PostgreSQLDatabase: SchemaSupporting {
7981
}
8082
}
8183

84+
if isArray {
85+
dataType = .array(dataType)
86+
}
87+
8288
// FIXME: is array support
8389
return .columnDefinition(column, dataType, constraints)
8490
}

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class FluentPostgreSQLTests: XCTestCase {
169169
static func prepare(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
170170
return PostgreSQLDatabase.create(DefaultTest.self, on: conn) { builder in
171171
builder.field(for: \.id, isIdentifier: true)
172-
builder.field(for: \.date, type: .timestamp(nil), .default(.function(.function("current_timestamp", []))))
172+
builder.field(for: \.date, type: .timestamp, .default(.literal(.numeric("current_timestamp"))))
173173
builder.field(for: \.foo)
174174
}
175175
}
@@ -386,7 +386,7 @@ struct Planet: PostgreSQLModel, PostgreSQLMigration, Equatable {
386386
}
387387
}
388388

389-
extension PostgreSQLColumnType {
389+
extension PostgreSQLDataType {
390390
static var planetType: PostgreSQLDataType {
391391
return .custom("PLANET_TYPE")
392392
}

0 commit comments

Comments
 (0)