当前位置: 首页 > 工具软件 > Phinx > 使用案例 >

PHP|ThinkPHP|Yii 数据库迁移(Phinx)的使用

楚翰
2023-12-01

        首先,PHP Composer, ThinkPHP,Yii 这三者的数据库迁移都是基于Phinx来操作。而对于基础数据库的建表可以导出一份SQL文件作为基础数据也不需要在写create table什么的比较费时间。

        讲解场景:当你又一个sell数据库,其中有表sell,你要新增tille_2字段,其中sell表基础SQL语句是:

-- Adminer 4.3.1 MySQL dump

SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `wm_abouts`;
CREATE TABLE `wm_abouts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `skey` varchar(100) NOT NULL DEFAULT '0' COMMENT '标识',
  `title` varchar(100) DEFAULT '' COMMENT '标题',
  `content` text COMMENT '内容',
  `video` varchar(255) DEFAULT NULL,
  `w_time` int(10) unsigned NOT NULL COMMENT '创建时间',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态(0隐藏,1显示)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='单页管理';


-- 2021-10-13 05:47:22
  • 初始化:
php7.2 vendor/bin/phinx init

         生成了一个配置文件[phinx.php]:

<?php
$dbConfig = require '../app/database.php';

return
[
    'paths' => [
        'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations', // 文件夹
        'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds' // 文件夹
    ],
    'environments' => [
        'default_migration_table' => 'phinxlog', // 生成一个表记录日志
        'default_environment' => 'development', // 运行下列那个数据库环境配置
        'production' => [
            'adapter' => 'mysql', // 数据库类型
            'host' => $dbConfig['hostname'], // 数据库地址
            'name' => $dbConfig['database'], // 数据库名
            'user' => $dbConfig['username'], // 数据库连接账号
            'pass' => $dbConfig['password'], // 数据库连接密码
            'port' => $dbConfig['hostport'], // 数据库端口号
            'table_prefix' => $dbConfig['hostport']['prefix'], // 数据库前缀
            'charset' => 'utf8', // 数据库字符集
        ],
        'development' => [
            'adapter' => 'mysql',
            'host' => $dbConfig['hostname'],
            'name' => $dbConfig['database'],
            'user' => $dbConfig['username'],
            'pass' => $dbConfig['password'],
            'port' => $dbConfig['hostport'],
            'table_prefix' => $dbConfig['hostport']['prefix'],
            'charset' => 'utf8',
        ],
        'testing' => [
            'adapter' => 'mysql',
            'host' => $dbConfig['hostname'],
            'name' => $dbConfig['database'],
            'user' => $dbConfig['username'],
            'pass' => $dbConfig['password'],
            'port' => $dbConfig['hostport'],
            'table_prefix' => $dbConfig['hostport']['prefix'],
            'charset' => 'utf8',
        ]
    ],
    'version_order' => 'creation'
];
  • 成一个版本文件:
 php7.2 vendor/bin/phinx create ChangeTableAbout

        默认代码如下:

<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Abouts extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {

    }
}

注意:需要你自己填充代码到change:

<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Abouts extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {


        // 添加字段title_2
        $table = $this->table('abouts');
        $table->addColumn('title_2', 'string', array(
            'limit' => 100, 'default' => 'default_title2', 'comment' => '副标题'
        ))->save();
    }
}

 运行:php7.2 phinx migrate --dry-run

 类似资料: