rafaelmiano/phpspa-migrate

A simple and elegant database migration system for PHP applications

v1.1.0 2025-07-02 02:01 UTC

This package is auto-updated.

Last update: 2025-08-10 01:24:32 UTC


README

A simple and elegant database migration system for PHP applications.

Installation

composer require rafaelmiano/phpspa-migrate

Configuration

Create a .env file in your project root with database configuration:

DB_HOST=localhost
DB_DATABASE=your_database
DB_USERNAME=root
DB_PASSWORD=

Usage

You can use the migration tool through vendor/bin:

php vendor/bin/migrate create users_table

Creating Migrations

This will create a new migration file in database/migrations with a timestamp prefix.

Example:

php vendor/bin/migrate create users_table

This will create a new migration file in database/migrations with a timestamp prefix.

Writing Migrations

<?php

use RafaelMiano\PhpSpaMigrate\Migration\Migration;

return new class($connection) extends Migration
{
    public function up()
    {
        $this->createTable('users', function($table) {
            $table->id();
            $table->string('username')->unique();
            $table->string('email');
            $table->string('password');
            $table->timestamps();
        });
    }

    public function down()
    {
        $this->dropTable('users');
    }
};

Available Schema Methods

  • id() - Add auto-incrementing ID
  • string(name, length = 255) - Add VARCHAR column
  • integer(name) - Add INT column
  • text(name) - Add TEXT column
  • timestamp(name) - Add TIMESTAMP column
  • timestamps() - Add created_at and updated_at columns
  • unique() - Make column unique
  • nullable() - Make column nullable
  • foreignKey(column) - Add foreign key
  • references(table.column) - Reference a foreign key

Running Migrations

php vendor/bin/migrate run     # Run pending migrations
php vendor/bin/migrate fresh   # Drop all tables and run migrations

Rolling Back Migrations

php vendor/bin/migrate rollback   # Rollback last batch of migrations

Other Commands

php vendor/bin/migrate help     # Show all available commands
php vendor/bin/migrate status   # Show migration status

Fresh Migrations

The fresh command provides a clean slate by dropping all tables and running migrations again:

php vendor/bin/migrate fresh

This will:

  1. Show a confirmation prompt
  2. Drop all existing tables
  3. Recreate the migrations table
  4. Run all migrations from scratch

Features

  • Interactive database creation
  • Batch-based migrations
  • Simple and intuitive schema builder
  • Environment configuration support
  • Rollback capability
  • Pretty console output
  • Error handling and user feedback

Contributing

Feel free to submit pull requests or create issues for bugs and feature requests.

License

This package is open-source software licensed under the MIT license.