Phinx 数据库迁移工具

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

安装

composer require "robmorgan/phinx

查看所有命令

vendor/bin/phinx

Phinx 数据库迁移工具_第1张图片
image.png

生成配置文件

vendor/bin/phinx init

使用phinx.php进行配置

// db.php
 'localhost',
    'DB_NAME' => 'lleg',
    'DB_USER' => 'root',
    'DB_PWD' => '',
);
$settings = $config;
// phinx.php
 array(
        "migrations"    => "/database/migrations",
        "seeds"         => "/database/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"
        )
    )
);

查看连接状态

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

Phinx 数据库迁移工具_第2张图片
image.png

生成数据库迁移文件

执行 vendor/bin/phinx create CreateMembersTable

Phinx 数据库迁移工具_第3张图片
image.png

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

public function change()
    {
        $user = $this->table('Members');
        $user->addColumn('username', 'string', ['limit'=>64]);
        $user->addColumn('password', 'string');
        $user->addColumn('status', 'integer', ['default' => 1, 'comment' => '状态']);
        $user->addColumn('register_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']);
        $user->addColumn('last_login', 'datetime', ['null'=>true, 'comment'=>'最后登陆日期']);
        $user->save();
    }

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

数据库迁移

执行 vendor/bin/phinx migrate

生成填充数据文件

执行 vendor/bin/phinx seed:create MemberSeeder

Phinx 数据库迁移工具_第4张图片
image.png

编辑填充文件

public function run()
    {
        $data = [
            [
                'username' => 'zhangsan',
                'password' => md5('123456'),
            ],
            [
                'username' => 'lisi',
                'password' => md5('123456'),
            ]
        ];

        $this->table('members')->insert($data)->saveData();
    }

填充数据

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

更新表结构

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

public function change()
    {
        $this->execute('alter table members drop column status;');
        $this->table('members')
               ->addColumn('state', 'integer', ['default' => 1, 'comment' => '状态'])
               ->update();
    }

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

回滚

vendor/bin/phinx rollback

指定配置文件

vendor/bin/phinx migrate --configuration ./config/phinx.php

简写

vendor/bin/phinx migrate -c ./config/phinx.php

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