數(shù)據(jù)庫遷移工具

數(shù)據(jù)庫遷移工具

首先通過 composer 安裝

composer require topthink/think-migration

注意事項(xiàng),不支持修改文件配置目錄


在命令行下運(yùn)行查看幫助,可以看到新增的命令

php think
 migrate
  migrate:create     Create a new migration
  migrate:rollback   Rollback the last or to a specific migration
  migrate:run        Migrate the database
  migrate:status     Show migration status
 optimize
  optimize:autoload  Optimizes PSR0 and PSR4 packages to be loaded wit
h classmaps too, good for production.  optimize:config    Build config and common file cache.  optimize:route     Build route cache.  optimize:schema    Build database schema cache. seed
  seed:create        Create a new database seeder  seed:run           Run database seeders

創(chuàng)建遷移類,首字母必須為大寫

php think migrate:create Users

可以看到目錄下有新文件 .\database\migrations\20161117144043_users.php

使用實(shí)例

<?phpuse Phinx\Migration\AbstractMigration;class Users extends AbstractMigration{/**
     * Change Method.
     */public function change(){// create the table$table = $this->table('users',array('engine'=>'MyISAM'));
        $table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用戶名,登陸使用'))
            ->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用戶密碼'))
            ->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陸狀態(tài)'))
            ->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陸標(biāo)識'))
            ->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登錄IP'))
            ->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登錄時(shí)間'))
            ->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'刪除狀態(tài),1已刪除'))
            ->addIndex(array('username'), array('unique' => true))
            ->create();
    }/**
     * Migrate Up.
     */public function up(){

    }/**
     * Migrate Down.
     */public function down(){

    }
}

對于同一個(gè)數(shù)據(jù)表,如果需要新的遷移動作,例如刪除字段、創(chuàng)建字段,可以創(chuàng)建新的更改文件,像svn一樣往前記錄操作,方便回滾。

更具體的使用可查看

http://docs.phinx.org/en/latest/

文檔最后更新時(shí)間:2022-05-31 09:21:13

文檔
目錄

深色
模式

切換
寬度