vakata/migrations

PHP database migrations

1.1.1 2024-02-19 16:18 UTC

This package is auto-updated.

Last update: 2024-04-19 16:40:36 UTC


README

Latest Version on Packagist Software License

PHP database migrations.

Install

Via Composer

$ composer require vakata/migrations

Usage

Prepare a directory for all migrations. A migration consists of a folder with 3 files inside:

  • schema.sql - needed schema modifications
  • data.sql - optional data to insert / update / delete
  • uninstall.sql - optional statements to fully revert this migration

Example structure:

 |-migrations
   |- base
   |  |- _core
   |     |- 000
   |        |- schema.sql
   |        |- data.sql
   |        |- uninstall.sql
   |- app
      |- feature1
      |  |- 000
      |  |  |- schema.sql
      |  |  |- data.sql
      |  |  |- uninstall.sql
      |  |- 001
      |     |- schema.sql
      |- feature2
         |- 000
            |- schema.sql
            |- uninstall.sql

Create the neccessary table, for example:

-- postgre
CREATE TABLE IF NOT EXISTS migrations (
  migration SERIAL NOT NULL,
  package varchar(255) NOT NULL,
  installed timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  removed timestamp DEFAULT NULL ,
  PRIMARY KEY (migration)
);
-- mysql
CREATE TABLE IF NOT EXISTS migrations (
  migration bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  package varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
  installed datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  removed datetime DEFAULT NULL,
  PRIMARY KEY (migration)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ;
$migrations = new \vakata\migrations\Migrations(
    new \vakata\database\DB('<connection-string-here>'),
    'path/to/migrations/folder',
    [ 'base/', 'app/feature2', 'app' ] // optional feature flags (also used for sorting)
    null // optional sort callback
);
// this will install the packages in this order:
// base/_core/000, app/feature2/000, app/feature1/000, app/feature1/000
$migrations->up();

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email github@vakata.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.