Skip to content

Instantly share code, notes, and snippets.

@MihaelIsaev
Last active January 16, 2020 01:11

Revisions

  1. MihaelIsaev renamed this gist Jan 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fluent4+automigration → fluent4+automigration.swift
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ func configure(_ app: Application) throws {

    // Todo.swift
    final class Todo: Model, Content {
    static let schema = "todos2"
    static let schema = "todos"

    @ID(key: "id")
    var id: UUID?
  2. MihaelIsaev created this gist Jan 8, 2020.
    46 changes: 46 additions & 0 deletions fluent4+automigration
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // configure.swift
    func configure(_ app: Application) throws {
    app.migrations.add(Todo(), to: .psql)

    try app.migrator.setupIfNeeded().wait()
    try app.migrator.prepareBatch().wait()
    }

    // Todo.swift
    final class Todo: Model, Content {
    static let schema = "todos2"

    @ID(key: "id")
    var id: UUID?

    @Field(key: "title")
    var title: String

    @Timestamp(key: "created_at", on: .create)
    public var createdAt: Date?

    @Timestamp(key: "updated_at", on: .update)
    public var updatedAt: Date?

    init() { }

    init(id: UUID? = nil, title: String) {
    self.id = id
    self.title = title
    }
    }

    extension Todo: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
    database.schema(Self.schema)
    .field("id", .uuid, .identifier(auto: false))
    .field("title", .datetime, .required)
    .field("created_at", .datetime)
    .field("updated_at", .datetime)
    .create()
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
    database.schema(Self.schema).delete()
    }
    }