How to manage your Materialize migrations with Laravel Zero?

Introduction

Prerequisites

What is Laravel Zero?

Demo project

Downloading the mzschema binary

wget https://github.com/bobbyiliev/mzschema/raw/main/builds/mzschema
chmod +x mzschema
./mzschema install
Materialize Migrations  SQLite database created successfully at:$HOME/.mz_migrations/mzschema/database.sqlite

Clone the mzschema repository (optional)

git clone https://github.com/MaterializeInc/materialize/issues/8779
cd mzschema
php mzschema app:build

Environment variables

MZ_CONNECTION=pgsql
MZ_HOST=127.0.0.1
MZ_PORT=6875
MZ_DATABASE=materialize
MZ_USERNAME=materialize
MZ_PASSWORD=materialize

Creating a migration

mkdir migrations
<?phpuse Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection('materialize')->statement(
"CREATE TABLE transfers (id int, name text)"
);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection('materialize')->statement(
"DROP TABLE IF EXISTS transfers"
);
}
};

Running the migrations

./mzschema migrate --path=./migrations/ --realpath
Do you really wish to run this command? (yes/no) [no]:
> yes
Migrating: 2022_03_16_155051_create_transfers_table
Migrated: 2022_03_16_155051_create_transfers_table (8.97ms)

Checking migration status

./mzschema migrate:status --path=./migrations/ --realpath
+------+------------------------------------------+-------+
| Ran? | Migration | Batch |
+------+------------------------------------------+-------+
| Yes | 2022_03_16_155051_create_transfers_table | 2 |
+------+------------------------------------------+-------+

Rolling back the migrations

./mzschema migrate:rollback --path=./migrations/ --realpath
./mzschema migrate:refresh --path=./migrations/ --realpath

Limitations

Renaming

<?phpuse Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Create the new `transfers_new` view
DB::connection('materialize')->statement(
"CREATE TABLE transfers_new (id int, name text, amount int)"
);
// Rename the old `transfers` view to `transfers_old`
DB::connection('materialize')->statement(
"ALTER TABLE transfers RENAME TO transfers_old"
);
// Rename the new `transfers_new` view to `transfers`
DB::connection('materialize')->statement(
"ALTER TABLE transfers_new RENAME TO transfers"
);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection('materialize')->statement(
"DROP TABLE IF EXISTS transfers, transfers_old, transfers_new"
);
}
};

Branching

Blue-green deployments

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bobby Iliev

I am a professional System Administrator with a demonstrated history of working in the internet industry. I am a Linux lover