Migration

Introduction

Currently Mighty only supports the MySQL/MariaDB databases, as such the migrations are quite straightforward and although the Schematic's approach is being developed and part of the future pipeline, the current best practice is still raw.
In order to generate a new migration file:
1
php mighty make:migration MigrationNameHere
Copied!
The above console command will scaffold a basic migration file under Database/Migrations.

Raw Query Approach

This is the easiest and still the best way to run your migrations with Mighty. Simply write your migration SQL script into the return array of the up or down method.
1
<?php
2
use \MightyCore\Database\Schema;
3
use \MightyCore\Database\Schematic;
4
5
class _2020_12_07_082601_AddUserTable{
6
public $timestamp=1607329561;
7
public $connection="default";
8
public function up(){
9
return [
10
'
11
CREATE TABLE `lm`.`user` (
12
`userId` INT NOT NULL AUTO_INCREMENT,
13
`username` VARCHAR(45) NOT NULL,
14
`password` VARCHAR(45) NOT NULL,
15
`createdAt` DATETIME NOT NULL,
16
PRIMARY KEY (`userId`));
17
'
18
];
19
}
20
21
public function down(){
22
return [
23
'DROP TABLE `lm`.`user`;'
24
];
25
}
26
}
27
Copied!
Notice that the return for up and down are both in arrays. This is to ease the debugging process. You can choose to squeeze the entire SQL script into one index, or spread it all out. By spreading it down to different indexes, Mighty will run the query one at a time, which will stop and throw any relevant error if applicable.
Last modified 10mo ago