zheltikov/simple-migrator

一款简单的数据库迁移工具。

v1.0.4 2024-05-20 06:43 UTC

This package is auto-updated.

Last update: 2024-09-20 07:23:40 UTC


README

一款简单的数据库迁移工具。

用法

首先,通过Composer安装此软件包

$ composer require zheltikov/simple-migrator

然后,在你的项目中定义一个配置文件,例如 migration_config.php

<?php

use Zheltikov\SimpleMigrator\{Config, Dialect, Migration, MigrationSet};

return new Config(
    // Supply here a closure returning a PDO connection to the database
    PDO: fn() => new PDO('pgsql:host=localhost;port=5432;dbname=postgres;user=postgres;password=secret'),
    
    // Define your migrations here...
    migrationSet: new MigrationSet([
        new Migration(
            id: '1',
            up: 'CREATE TABLE people (id INTEGER);',
            down: 'DROP TABLE people;',
        ),
        new Migration(
            id: '2',
            up: 'ALTER TABLE people ADD COLUMN name VARCHAR;',
            down: 'ALTER TABLE people DROP COLUMN name;',
        ),
        // ...
    ]),
    
    // Optionally, change the name of the migration log table.
    // By default, it is 'migrations'.
    tableName: 'my_migrations',
    
    // You can also choose an SQL dialect here.
    // By default, it is Dialect::POSTGRESQL
    dialect: Dialect::SQLITE,
);

然后,你可以通过运行以下命令应用这些迁移

$ vendor/bin/simple-migrator latest migration_config.php

或者,查看其他命令以逐个应用迁移

$ vendor/bin/simple-migrator 

Usage: vendor/bin/simple-migrator COMMAND CONFIG_FILE

COMMAND is one of the following actions:
    up             Apply the next migration
    down           Revert to the previous migration
    latest         Apply all migrations up to the latest one
    current        Print the ID of the migration currently applied to the database

CONFIG_FILE points to the migration config file.

特性

  • PostgreSQL支持(通过PDO)
  • MySQL支持
  • SQLite支持