Skip to content

Commit 006386a

Browse files
authored
Merge pull request #19 from vapor/default-value
add default value
2 parents d68c862 + cadc0be commit 006386a

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

Sources/FluentPostgreSQL/PostgreSQLColumn.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ public struct PostgreSQLColumn {
66
/// The columns size. Negative values mean varying size.
77
public let size: Int16
88

9+
/// This column's default value.
10+
public var `default`: String?
11+
912
/// Creates a new `PostgreSQLColumn`.
10-
public init(type: PostgreSQLDataType, size: Int16? = nil) {
13+
public init(type: PostgreSQLDataType, size: Int16? = nil, default: String? = nil) {
1114
self.type = type
1215
self.size = size ?? -1
16+
self.default = `default`
1317
}
1418
}
1519

Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ extension PostgreSQLDatabase: SchemaSupporting, IndexSupporting {
3131
string += " NOT NULL"
3232
}
3333

34+
if let d = field.type.default {
35+
string += " DEFAULT \(d)"
36+
}
37+
3438
return string
3539
}
3640

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ class FluentPostgreSQLTests: XCTestCase {
100100
}
101101
}
102102

103+
func testDefaultValue() throws {
104+
database.enableLogging(using: DatabaseLogger(handler: { print($0) }))
105+
let conn = try database.makeConnection(on: eventLoop).await(on: eventLoop)
106+
try? DefaultTest.revert(on: conn).await(on: eventLoop)
107+
try DefaultTest.prepare(on: conn).await(on: eventLoop)
108+
let test = DefaultTest()
109+
// _ = try test.save(on: conn).await(on: eventLoop)
110+
let builder = test.query(on: conn)
111+
builder.query.data = ["foo": "bar"] // there _must_ be a better way
112+
builder.query.action = .create
113+
try builder.execute().await(on: eventLoop)
114+
if let fetched = try DefaultTest.query(on: conn).first().await(on: eventLoop) {
115+
XCTAssertNotNil(fetched.date?.value)
116+
} else {
117+
XCTFail()
118+
}
119+
try DefaultTest.revert(on: conn).await(on: eventLoop)
120+
conn.close()
121+
}
122+
103123
static let allTests = [
104124
("testSchema", testSchema),
105125
("testModels", testModels),
@@ -117,6 +137,45 @@ class FluentPostgreSQLTests: XCTestCase {
117137
]
118138
}
119139

140+
struct PostgreSQLDate: PostgreSQLType, Codable {
141+
static var postgreSQLDataType: PostgreSQLDataType {
142+
return .timestamp
143+
}
144+
145+
static var postgreSQLDataArrayType: PostgreSQLDataType {
146+
return ._timestamp
147+
}
148+
149+
static var postgreSQLColumn: PostgreSQLColumn {
150+
return PostgreSQLColumn(type: .timestamp, size: nil, default: "CURRENT_TIMESTAMP")
151+
}
152+
153+
var value: Date?
154+
155+
init(_ value: Date? = nil) {
156+
self.value = value
157+
}
158+
159+
static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> PostgreSQLDate {
160+
return try PostgreSQLDate(Date.convertFromPostgreSQLData(data))
161+
}
162+
163+
func convertToPostgreSQLData() throws -> PostgreSQLData {
164+
return try value?.convertToPostgreSQLData() ?? PostgreSQLData(type: .timestamp, format: .binary, data: nil)
165+
}
166+
}
167+
168+
struct DefaultTest: PostgreSQLModel, Migration {
169+
var id: Int?
170+
var date: PostgreSQLDate?
171+
var foo: String
172+
init() {
173+
self.id = nil
174+
self.date = nil
175+
self.foo = "bar'"
176+
}
177+
}
178+
120179
struct Pet: PostgreSQLJSONType, Codable {
121180
var name: String
122181
}

0 commit comments

Comments
 (0)