lightscale/migrator

一个通用的数据库迁移库

0.0.4 2022-06-24 10:59 UTC

This package is auto-updated.

Last update: 2024-09-24 16:18:12 UTC


README

A simple php database migrations library and management tool. This is standalone database migration library that is usable in any platform with any database.

它处于非常初期的开发阶段但可用。您需要了解您在做什么才能使用它。

安装

这应该使用composer从packagist安装

$ composer require lightscale/migrator

配置

您需要做两件事来使用它。将管理迁移的命令行工具复制到项目的根目录。它需要与/vendor在同一目录下。

$ cp vendor/lightscale/migrator/bin/migrator.php ./migrator

然后您需要一个配置文件。在这里您需要给出migrator回调函数以获取当前迁移版本并设置它。您还需要创建一个函数来提供您需要修改数据库的数据库对象。

这里有一个非常基本的模板

<?php

function init() {
    /*
     * This runs in the migrator constructor to allow you to setup
     * any more general things.
     *
     * An example would be load any components of your application that
     * are required to make this work. For example in wordpress you need
     * to require "wp-load.php" to initialize wordpress and be able to use
     * its database and functions.
     */
}

function version_get($db) {
    /*
     * This needs to access where you have stored the current migrations verison
     * and return it.
     *
     * Read version_update for more info
     */
}

function version_update($db, $version) {
    /*
     * This needs to create some form of storage for the version and update it
     * when ever the version changes. This could be done with a file or a
     * database table. Anything you like.
     *
     * For example in wordpress you can use wp_options and the options API.
     *
     * For a standalone project you can create a database table with a single
     * row and column to hold the version.
     */
}

function get_db() {
    /*
     * This needs to return an object that gives access in some form to the
     * database or databases that you need to migrate.
     *
     * This will be the only thing that is passed to the up and down functions
     * within the migrations.
     *
     * This is really what makes this completely independent of what you are
     * using this library on.
     */
}


/*
 * This file needs to return the configuration in an associative array.
 */
return [
    'init' => 'init',
    'migrations_dir' => 'database_migrations', // Required
    'version_update_fn' => 'version_update',   // Required
    'version_get_fn' => 'version_get',         // Required
    'get_db_fn' => 'get_db'                    // Required
];

当然,您可能希望将其放在类或命名空间中,以避免命名冲突。只需确保配置文件返回一个包含所需属性的关联数组即可。

查看lightscale/migrator-config-wordpress以获取一个工作示例。

用法

运行./migrator脚本。这将输出帮助信息。

以下是可用的命令

  create    Create a new migration
  help      Displays help for a command
  list      Lists commands
  migrate   Run all remaining migrations
  reset     Rollback all migrations.
  rollback  Undo one migration

迁移

以下是迁移的文件结构。您可以在配置中指定目录,然后它就是平面的

/
└── dbmigrations
    ├── 191026_144428_test_mig_1.php
    ├── 191026_144432_test_mig_2.php
    └── 191026_144435_test_mig_3.php

以下是迁移的结构

<?php

use Lightscale\Migrator\Migration;

class example implements Migration {

    public function up($db) {
        /*
         * Called when migrate is run.
         *
         * The DB parameter is the value
         * returned by "get_db_fn" from the config.
         */
    }

    public function down($db) {
        /*
         * Called when rollback or reset.
         *
         * The DB parameter is the value
         * returned by "get_db_fn" from the config.
         */
    }

}

发布历史

  • 0.0.2
    • 特性 - 设置一个类以扩展迁移。
    • 错误 - 当迁移失败时,更新版本为最后一次成功的版本。
    • 错误 - 修复migrator_dir处理,以便它可以找到迁移。
  • 0.0.1 所有核心功能正常工作。仍在持续开发中。

要求

  • PHP 7.1.3
  • Composer

贡献者

Sam Light

许可证

本项目受GPLv3许可证许可 - 请参阅LICENSE文件以获取详细信息。