Skip to content

Instantly share code, notes, and snippets.

@natebird
Last active November 29, 2018 16:44
Show Gist options
  • Save natebird/f16c30895c6b2a62c0c1251cf4b07730 to your computer and use it in GitHub Desktop.
Save natebird/f16c30895c6b2a62c0c1251cf4b07730 to your computer and use it in GitHub Desktop.
Vapor database migration examples using addProperties function
extension Quote: Migration {
static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
return Database.create(self, on: connection) { builder in
// Add fields from above
try addProperties(to: builder)
// Add FOREIGN KEY reference
builder.reference(from: \.authorID, to: \Author.id)
// make content UNIQUE and indexed
builder.unique(on: \.content)
}
}
}
extension Quote: Migration {
static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
return Database.create(self, on: connection) { builder in
builder.field(for: \.id, type: .uuid)
builder.field(for: \.content)
builder.field(for: \.authorID, type: .uuid)
builder.field(for: \.createdAt)
builder.field(for: \.updatedAt)
// Add FOREIGN KEY reference
builder.reference(from: \.authorID, to: \Author.id)
// make content UNIQUE and indexed
builder.unique(on: \.content)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment