regur/lmvc-database-migration

A lightweight migration system for PHP applications.

dev-main 2025-04-16 13:55 UTC

This package is auto-updated.

Last update: 2025-06-16 14:24:39 UTC


README

A simple database migration package for PHP applications, inspired by Laravel's Artisan.

📌 Features

  • Create and manage database migrations
  • CLI command similar to Laravel Artisan
  • Auto-generates migration files
  • Supports custom database schemas

🚀 Installation

Install via Composer:

composer require regur/lmvc-database-migration

After installation, the CLI command will be available at:

php vendor/bin/command

To make it accessible from the root directory, create a symlink:

ln -s vendor/bin/command command

or else make one file at root directory commmand.php (the .env file is also needed for database credentials)

namespace Regur\LMVC\Framework\Bin;

use Regur\LMVC\Framework\Database\Bootstrap;
use Dotenv\Dotenv;


$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$dbcred = [
    'host' => $_ENV['DB_HOST'],
    'database' => $_ENV['DB_NAME'],
    'username' => $_ENV['DB_UNAME'],
    'password' => $_ENV['DB_PWD']
];

Bootstrap::init($dbcred);

Now you can run:

php command migrate

🛠 Usage

Create a New Migration

php command make:migration create_users_table

This will generate a migration file inside Application/Database/Migrations.

Run Migrations

php command migrate

Applies all pending migrations.

Fresh Migrations (Reset & Run All)

php command migrate:fresh

Drops all tables and runs migrations from scratch.

Apply Pending Migrations

php command migrate:up

Runs the next batch of pending migrations.

Rollback the Last Migration Batch

php command migrate:down

Rolls back the last executed migration batch.

Refresh Migrations (Rollback & Reapply)

php command migrate:refresh

Rolls back all migrations and runs them again.

📂 Project Structure

my-migration-package/
│── src/
│   ├── Database/
│   │   ├── Core/               # Core migration classes
│   │   │   ├── Migration.php
│   │   │   ├── Schema.php
│   │   │   ├── Blueprint.php
│   │   │   ├── DB.php
│   │   ├── Migrations/         # Auto-generated migrations
│   ├── Bootstrap.php           # Main bootstrap file
│── bin/
│   ├── command                 # CLI command file
│── composer.json
│── README.md

🔧 Configuration

Configure the database connection in bin/command:

Regur\LMVC\Framework\Database\Bootstrap::init([
    'host' => '127.0.0.1',
    'database' => 'your_db',
    'username' => 'root',
    'password' => '',
]);

📜 License

This package is open-source and available under the MIT License.

💡 Need help? Feel free to create an issue or contribute!