数据库迁移工具Phinx

数据库迁移工具

robmorgan/phinx
文档*http://docs.phinx.org/en/latest/
[教程*https://robmorgan.id.au/posts/getting-started-with-phinx/]
1.安装
composer require "robmorgan/phinx

2.执行
php vendor/bin/phinx

直接运行 php vendor/bin/phinx init 可生成配置文件
另外一种方法是直接使用php文件做配置文件

使用phinx.php进行配置

#db_config.php
 'localhost',
    'DB_NAME' => 'lleg',
    'DB_USER' => 'root',
    'DB_PWD' => '',
);
$settings = $config;

#phinx.php
 array(
        "migrations"    => "db/migrations",
        "seeds"         => "db/seeds"
    ),
    "environments"   => array(
        "defaut_migration_table"    => "phinxlog",
        "default_database"          => "lleg",
        "default_environment"       => "development"
        "production"   => array(
            "adapter"   => "mysql",
            "host"      => $settings["DB_HOST"],
            "name"      => $settings["DB_NAME"],
            "user"      => $settings["DB_USER"],
            "pass"      => $settings["DB_PWD"],
            "port"      => 3306,
            "charset"   => "utf8"
        ),
        "development"   => array(
            "adapter"   => "mysql",
            "host"      => $settings["DB_HOST"],
            "name"      => $settings["DB_NAME"],
            "user"      => $settings["DB_USER"],
            "pass"      => $settings["DB_PWD"],
            "port"      => 3306,
            "charset"   => "utf8"
        )
    )
);

执行 php vendor/bin/phinx status查看连接状态

执行 php vendor/bin/phinx create Migration

现在生成了created /db/migrations/20180310020523_migration.php

编辑这个文件,添加数据库创建内容

public function change()
    {
        $user = $this->table('user');
        $user->addColumn('open_id', 'string', ['limit'=>64]);
        $user->addColumn('register_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']);
        $user->addColumn('favorite_music', 'integer', ['default'=> 0, 'comment'=>'喜欢的音乐']);
        $user->addColumn('favorite_vedio', 'integer', ['default'=> 0, 'comment'=>'喜欢的视频数']);
        $user->addColumn('favorite_article', 'integer', ['default'=> 0, 'comment'=>'喜欢的文章数']);
        $user->addColumn('baby_birthday', 'date', ['null'=>true, 'comment'=>'宝宝生日']);
        $user->addColumn('baby_sex', 'boolean', ['null'=>true, 'comment'=>'宝宝性别']);
        $user->addColumn('last_login', 'datetime', ['null'=>true, 'comment'=>'最后登陆日期']);
        $user->save();
    }

默认会添加一个自增id,作为主键

执行 php vendor/bin/phinx migrate

初始化数据

执行 php vendor/bin/phinx seed:create CategorySeeder
系统自动创建 created ./db/seeds/CategorySeeder.php
修改 CategorySeeder.php

执行 php vendor/bin/phinx seed:run 将会进行所有Seed
如果想运行指定的Seed需要用- s参数指定
php vendor/bin/phinx seed:run -s CategorySeeder

更新表结构

当需要更新表结构的时候,需要再创建一个migrate
执行php vendor/bin/phinx create ChangeArtist
再将需要更新的内容写到change函数

public function change()
    {
        $this->execute('alter table resource drop column artist ;');
        $resource = $this->table('resource');
        $resource->addColumn('artist', 'string', ['limit'=>128, 'default'=>'']);
        $resource->update();
    }

最后执行php vendor/bin/phinx migrate
之前的已经执行过的migrate不会执行, 只会执行更新的部分

回滚

php vendor/bin/phinx rollback

说明

以上操作基于Linux操作系统。
在windows中使用phinx.bat文件

D:\workspace_ued\TelegramPostBot (master -> origin)
λ vendor\bin\phinx.bat
Phinx by CakePHP - https://phinx.org. 0.10.6

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  breakpoint   Manage breakpoints
  create       Create a new migration
  help         Displays help for a command
  init         Initialize the application for Phinx
  list         Lists commands
  migrate      Migrate the database
  rollback     Rollback the last or to a specific migration
  status       Show migration status
  test         Verify the configuration file
 seed
  seed:create  Create a new database seeder
  seed:run     Run database seeders

你可能感兴趣的:(数据库迁移工具Phinx)