rafaelmiano / phpspa-migrate
A simple and elegant database migration system for PHP applications
v1.1.0
2025-07-02 02:01 UTC
Requires
- php: >=7.4
- ext-pdo: *
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 IDstring(name, length = 255)
- Add VARCHAR columninteger(name)
- Add INT columntext(name)
- Add TEXT columntimestamp(name)
- Add TIMESTAMP columntimestamps()
- Add created_at and updated_at columnsunique()
- Make column uniquenullable()
- Make column nullableforeignKey(column)
- Add foreign keyreferences(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:
- Show a confirmation prompt
- Drop all existing tables
- Recreate the migrations table
- 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.