alphasoft-fr/sql-migration

2.0.0 2023-11-16 11:25 UTC

This package is auto-updated.

Last update: 2024-11-16 13:41:22 UTC


README

This is a utility library and commands for managing database migrations in SQL.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Installation

Use Composer

Composer Require

composer require alphasoft-fr/sql-migration

Requirements

  • PHP version 8.1

Usage

Configuration

  1. Create a configuration file named migration-config.php at the root of your project. You can use the following example as a starting point:
<?php

return [
    'connection' => new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'),
    'migrations_directory' => __DIR__ . '/migrations',
    // Other configuration options...
];
  1. Customize the configuration options according to your needs. You can provide the PDO connection instance and specify the directory where migration files will be stored.

Generating a Migration File

You can generate a new migration file using the following command:

php vendor/bin/sqlmigration sql:migration:generate

This will create a new migration file in the specified migrations directory with placeholder content for both the up and down migrations.

Modify the generated migration file with SQL queries corresponding to the intended migration:

-- UP MIGRATION --
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- DOWN MIGRATION --
DROP TABLE users;

Running Migrations

You can apply pending migrations using the following command:

php vendor/bin/sqlmigration sql:migration:migrate

This command will execute all pending migrations in ascending order of their version numbers. Successfully applied migrations will be displayed in the console output.

Rolling Back Migrations

You can revert the last applied migration using the following command:

php vendor/bin/sqlmigration sql:migration:down <version>

Replace <version> with the version number of the migration you want to revert. This command will execute the down migration for the specified version, effectively rolling back the changes made by that migration.

Contributing

If you encounter any issues or have suggestions for improvements, please feel free to open an issue on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.