|
1 | 1 | # Schema Files |
2 | 2 |
|
3 | | -Leaf MVC took inspiration from Laravel and Ruby on Rails, adopting migrations, seeders, and factories to make database management easy. While this approach is powerful, managing multiple files for a single table can become a hassle. |
4 | | - |
5 | | -That’s why in Leaf MVC v4, we’re introducing Schema Files—a simpler, more intuitive way to define, test, and seed your database in one place. |
| 3 | +Leaf MVC has always taken cues from Laravel and Rails to make database management smooth. But juggling migrations, seeders, and factories for one table? That gets messy, quickly! Schema files offer a cleaner, all-in-one way to define, test, and seed your database. |
6 | 4 |
|
7 | 5 | ## What are Schema Files? |
8 | 6 |
|
9 | | -Schema Files build on the JSON schema idea from earlier Leaf MVC versions but take things even further. Instead of managing separate migrations, seeders, and factories, everything is now in one place. Written in YAML, they’re clean, easy to read, and eliminate unnecessary repetition—so you can focus on building, not juggling files. |
| 7 | +With Schema Files in Leaf MVC 4, you can define your database tables, seed data, and test data—all in one simple YAML file. No need to manage separate migrations, seeders, or factories. It’s clean, readable, and designed to help you move fast without the overhead. |
10 | 8 |
|
11 | 9 | ```yml [flights.yml] |
12 | 10 | columns: |
@@ -70,16 +68,20 @@ Breaking this file down, there are three main sections: |
70 | 68 | - `columns`: This is used to set the columns of the table. The key is the column name and the value is a key value pair of column properties. The available properties are: |
71 | 69 | - `type`: The type of the column. This can be `string`, `integer`, `timestamp` or any type supported by Laravel's Eloquent. |
72 | 70 | - `length`: The length of the column. This is only used for `string` columns. |
73 | | - - `unique`: This is used to set the column as unique. |
74 | 71 | - `nullable`: This is used to set the column as nullable. |
75 | 72 | - `default`: This is used to set the default value of the column. |
76 | | - - `autoIncrement`: This is used to set the column as auto-incrementing. |
77 | 73 | - `unsigned`: This is used to set the column as unsigned. |
78 | 74 | - `index`: This is used to set the column as an index. |
| 75 | + - `unique`: This is used to set the column as unique. |
79 | 76 | - `primary`: This is used to set the column as the primary key. |
80 | 77 | - `foreign`: This is used to set the column as a foreign key. The value of this key is the table and column the column is a foreign key to. |
| 78 | + - `values`: This is used to set the values of the column. This is only used for `enum` and `set` columns. |
81 | 79 | - `onDelete`: This is used to set the `ON DELETE` constraint of the foreign key. |
82 | 80 | - `onUpdate`: This is used to set the `ON UPDATE` constraint of the foreign key. |
| 81 | + - `comment`: This is used to set the comment of the column. |
| 82 | + - `autoIncrement`: This is used to set the column as auto-incrementing. |
| 83 | + - `useCurrent`: This is used to set the column to use the current timestamp. This is only used for `timestamp` columns. |
| 84 | + - `useCurrentOnUpdate`: This is used to set the column to use the current timestamp on update. This is only used for `timestamp` columns. |
83 | 85 |
|
84 | 86 | - `seeds`: This is used to set the seeders of the table. The available properties are: |
85 | 87 | - `count`: This is used to set the number of seeds to generate. |
@@ -179,8 +181,25 @@ php leaf db:script ImportUsersFromOldTable |
179 | 181 |
|
180 | 182 | Migration histories keep track of changes to your database, making it easy to roll back if needed. Unlike other frameworks, **Leaf MVC handles this automatically**—no need to manually create migrations just to track history. |
181 | 183 |
|
| 184 | +```yml [users.yml] |
| 185 | +columns: |
| 186 | + name: string |
| 187 | + email: |
| 188 | + type: string |
| 189 | + length: 255 |
| 190 | + unique: true |
| 191 | + password: string |
| 192 | + email_verified_at: timestamp |
| 193 | + is_super_admin: # [!code ++] |
| 194 | + type: boolean # [!code ++] |
| 195 | + default: false # [!code ++] |
| 196 | +``` |
| 197 | + |
| 198 | +This example adds a new column `is_super_admin` to the `users` table. When you run `php leaf db:migrate`, Leaf will compare it to the previous version of the file, find the differences and automatically create the `is_super_admin` column for you in your database. You don't need to worry about writing migration files or keeping track of changes manually. |
| 199 | + |
182 | 200 | Every time you update a Schema File and run `db:migrate`, Leaf logs the changes for you. No more digging through migration files—just focus on building. And when you need to roll back? Simply run `php leaf db:rollback`. |
183 | 201 |
|
| 202 | + |
184 | 203 | ## Seeding your database |
185 | 204 |
|
186 | 205 | Database seeds let you pre-populate your database with initial data—whether it's default settings, test data, or sample records. Instead of manually adding entries, you can use seeders to automate this process. |
|
0 commit comments