|
1 | 1 | # Migrations |
| 2 | + |
| 3 | +Laravel database migrations are like version control for your database. They allow you to easily create, modify, or delete tables and columns in your database in a structured way, without having to manually write SQL queries. |
| 4 | + |
| 5 | +When you make changes to your database like adding a new table or column, modifying an existing column's data type, or changing a relationship between tables, database migrations allow you to propagate those changes to all instances of your database. |
| 6 | + |
| 7 | +## Generating Migrations |
| 8 | + |
| 9 | +Leaf MVC provides a simple way to generate migrations using the `g:migration` command: |
| 10 | + |
| 11 | +```bash |
| 12 | +php leaf g:migration <Name> |
| 13 | + |
| 14 | +# example |
| 15 | + |
| 16 | +php leaf g:migration flights |
| 17 | +``` |
| 18 | + |
| 19 | +The new migration will be placed in your `app/database/migrations` directory. Each migration file name begins with a timestamp. |
| 20 | + |
| 21 | +## Migration Structure |
| 22 | + |
| 23 | +A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. |
| 24 | + |
| 25 | +You can create and modify tables in the both of these methods. In this example, we create a posts table: |
| 26 | + |
| 27 | +```php |
| 28 | +<?php |
| 29 | +namespace App\Database\Migrations; |
| 30 | + |
| 31 | +use Leaf\Database; |
| 32 | +use Illuminate\Database\Schema\Blueprint; |
| 33 | + |
| 34 | +class CreateUsers extends Database { |
| 35 | + /** |
| 36 | + * Run the migrations. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function up() |
| 41 | + { |
| 42 | + if (!$this->capsule::schema()->hasTable("posts")): |
| 43 | + $this->capsule::schema()->create("posts", function (Blueprint $table) { |
| 44 | + $table->increments('id'); |
| 45 | + $table->string('author_id'); |
| 46 | + $table->string('title'); |
| 47 | + $table->text('body'); |
| 48 | + $table->timestamps(); |
| 49 | + }); |
| 50 | + endif; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Reverse the migrations. |
| 55 | + * |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function down() |
| 59 | + { |
| 60 | + $this->capsule::schema()->dropIfExists("posts"); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +::: tip Note |
| 66 | +Instead of building your migrations from scratch, you can use Leaf's schema builder to generate migrations from JSON data. [Learn more](/docs/mvc/schema). |
| 67 | +::: |
| 68 | + |
| 69 | +## Running migrations |
| 70 | + |
| 71 | +To run all of your outstanding migrations, execute the `db:migrate` command: |
| 72 | + |
| 73 | +```bash |
| 74 | +php leaf db:migrate |
| 75 | +``` |
| 76 | + |
| 77 | +You may also run seeds alongside your migrations if you wish to do so: |
| 78 | + |
| 79 | +```bash |
| 80 | +php leaf db:migrate -s |
| 81 | +# or |
| 82 | +php leaf db:migrate --seed |
| 83 | +``` |
| 84 | + |
| 85 | +You can also choose to run a specific migration file: |
| 86 | + |
| 87 | +```bash |
| 88 | +php leaf db:migrate -f users |
| 89 | +``` |
| 90 | + |
| 91 | +## Rolling Back Migrations |
| 92 | + |
| 93 | +To roll back the latest migration operation, you may use the `db:rollback` command. |
| 94 | + |
| 95 | +```bash |
| 96 | +php leaf db:rollback |
| 97 | +``` |
| 98 | + |
| 99 | +You may roll back a limited number of migrations by providing the `step` option to the `rollback` command. For example, the following command will roll back the last two migrations: |
| 100 | + |
| 101 | +```bash |
| 102 | +php leaf db:rollback -s 2 |
| 103 | +``` |
| 104 | + |
| 105 | +To roll back all migrations, you can just pass `all` as the `step` option. |
| 106 | + |
| 107 | +```bash |
| 108 | +php leaf db:rollback --step all |
| 109 | +``` |
| 110 | + |
| 111 | +You can also rollback a specific migration file: |
| 112 | + |
| 113 | +```bash |
| 114 | +php leaf db:rollback -f users |
| 115 | +``` |
| 116 | + |
| 117 | +## Refreshing Migrations |
| 118 | + |
| 119 | +If you would like to reset your database and re-run all of your migrations with seeds, you may use the `db:reset` command. This command will drop all tables in your database and re-run all of your migrations: |
| 120 | + |
| 121 | +```bash |
| 122 | +php leaf db:reset |
| 123 | +``` |
| 124 | + |
| 125 | +If you want to prevent seeds from running, you can use the `--noSeed` option: |
| 126 | + |
| 127 | +```bash |
| 128 | +php leaf db:reset --noSeed |
| 129 | +``` |
0 commit comments