Vapor-Day3 Migration

Migration

struct CreateTodo: Migration {
    func prepare(on database: Database) -> EventLoopFuture {
        return database.schema("todos")
            .id()
            .field("title", .string, .required)
            .create()
    }

    func revert(on database: Database) -> EventLoopFuture {
        return database.schema("todos").delete()
    }
}

最开始一直不知道这段代码是干啥用的。。。。
然后挨个翻文档,翻到一个命令 vapor run migrate。
然后就试试呗。

 % vapor-beta run migrate
[10/10] Linking Run
Migrate Command: Prepare
[ INFO ] query read _fluent_migrations
The following migration(s) will be prepared:
+ CreateTodo on default
Would you like to continue?
y/n> y
[ INFO ] query read _fluent_migrations
[ INFO ] query read _fluent_migrations limits=[count(1)]
[ INFO ] query create _fluent_migrations input=[[id: 33CC083E-F102-403A-A8B0-0E40B047D465, created_at: 2020-05-02 10:12:53 +0000, batch: 1, updated_at: 2020-05-02 10:12:53 +0000, name: "CreateTodo"]]
Migration successful

然后mysql会自动创建todos 的表。

mysql> show tables;
+-----------------------------+
| Tables_in_swift_fluent_test |
+-----------------------------+
| _fluent_migrations          |
| article                     |
| todos                       |
| user                        |
+-----------------------------+
4 rows in set (0.00 sec)

我也不知道用得对不对 反正我认为也不应该让我搭建一个新服务器的时候,手动去创建每一个表。。。 那太不科学了。

你可能感兴趣的:(Vapor-Day3 Migration)