From ef01159b843659b4e0a858b09a0372f736be599c Mon Sep 17 00:00:00 2001 From: Jimmy School Date: Tue, 10 Jan 2017 14:33:50 -0600 Subject: [PATCH] create migrations section --- fluent/model.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/fluent/model.md b/fluent/model.md index 62a09709..c1d8322e 100644 --- a/fluent/model.md +++ b/fluent/model.md @@ -88,7 +88,7 @@ When a `User` is saved, the `makeNode()` method will be called and the resulting ## Preparations -Some databases, like MySQL, need to be prepared for a new schema. In MySQL, this means creating a new table. +Some databases, like MySQL, need to be prepared for a new schema. In MySQL, this means creating a new table. Preparations are also equatable to migrations, as they can be used to alter schemes after they've already been created. ### Prepare @@ -123,6 +123,27 @@ final class User { Here we are deleting the table named `users`. +### Preparations as Migrations + +If you want to add a field to your table after you've already created the initial schema, you can create a struct or class that inherits from `Preparation` like so: + +```swift + +struct AddFooToBar: Preparation { + static func prepare(_ database: Database) throws { + try database.modify("bars", closure: { bar in + bar.string("foo", length: 150, optional: false, unique: false, default: nil) + }) + } + + static func revert(_ database: Database) throws { + + } +} +``` + +Then, in your Droplet setup, add this line: `drop.preparations.append(AddFooToBar.self)` + ### Droplet To run these prepations when the applications boots, you must add the Model to the `Droplet`. @@ -223,4 +244,3 @@ Change the table/collection name ```swift static var entity = "new_name" ``` -