<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 數據庫遷移工具 使用數據庫遷移工具可以將數據庫結構和數據很容易的在不同的數據庫之間管理遷移。 在以前,為了實現“程序安裝”,你可能會導出一份sql文件,安裝時,用程序解析這個sql文件,執行里面的語句,這樣做有諸多的局限性,但現在使用數據庫遷移工具,你可使用一個強大的類庫API來創建數據庫結構和記錄,并且可以容易的安裝到Mysql,sqlite,sqlserver等數據庫。 原項目手冊:[phinx](https://book.cakephp.org/phinx/0/en/index.html) 使用范例: > 使用之前你應當正確的連接到數據庫,不論是mysql,sqlite,sqlserver ### 安裝 ``` composer require topthink/think-migration ``` ### 創建遷移工具文件 ``` //執行命令,創建一個操作文件,一定要用大駝峰寫法,如下 php think migrate:create AnyClassNameYouWant //執行完成后,會在項目根目錄多一個database目錄,這里面存放類庫操作文件 //文件名類似/database/migrations/20190615151716_any_class_name_you_want.php ``` ### 編輯文件 ``` <?php use think\migration\Migrator; use think\migration\db\Column; class AnyClassNameYouWant extends Migrator { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class * * The following commands can be used in this method and Phinx will * automatically reverse them when rolling back: * * createTable * renameTable * addColumn * renameColumn * addIndex * addForeignKey * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ 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'=>'登陸狀態')) ->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陸標識')) ->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登錄IP')) ->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登錄時間')) ->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'刪除狀態,1已刪除')) ->addIndex(array('username'), array('unique' => true)) ->create(); } } ``` ### 執行遷移工具 ``` php think migrate:run //此時數據庫便創建了prefix_users表. ``` > 數據庫會有一個migrations表,這個是工具使用的表,不要修改 ### 例子 ~~~ $table = $this->table('followers', ['id' => false, 'primary_key' => ['user_id', 'follower_id']]); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime') ->addIndex(['email','username'], ['limit' => ['email' => 5, 'username' => 2]]) ->addIndex('user_guid', ['limit' => 6]) ->create(); ~~~ ### 表支持的參數選項 | 選項 | 描述 | | -- | ------ | | comment | 給表結構設置文本注釋 | | row\_format | 設置行記錄模格式| | engine | 表引擎 *(默認 ``InnoDB``)* | | collation | 表字符集 *(默認 ``utf8\_general\_ci``)* | | signed | 是否無符號 `signed` *(默認 ``true``)* | ### 常用列 * biginteger * binary * boolean * date * datetime * decimal * float * integer * string * text * time * timestamp * uuid ### 所有的類型都支持的參數 | Option | Description | | --- | --- | | limit | 文本或者整型的長度 | | length | `limit`別名 | | default | 默認值 | | null | 允許 `NULL` 值 (不該給主鍵設置 | | after | 在哪個字段名后 *(只對MySQL有效)* | | comment | 給列設置文本注釋 | ### 索引的用法 ``` ->addIndex(['email','username'], ['limit' => ['email' => 5, 'username' => 2]]) ->addIndex('user_guid', ['limit' => 6]) ->addIndex('email',['type'=>'fulltext']) ``` 如上面例子所示,默認是`普通索引`,mysql可設置生效`復合索引`,mysql可以設置`fulltext`. ## 自動版本升級降級 該項目可以升級和還原,就像git/svn一樣rollback。 如果希望實現自動升級降級,那就把邏輯寫在change方法里,只最終調用`create`和`update`方法,不要調用`save`方法。 `change`方法內僅支持以下操作 * createTable * renameTable * addColumn * renameColumn * addIndex * addForeignKey 如果真的有調用其他方法,可以寫到`up`和`down`方法里,這里的邏輯不支持自動還原,up寫升級的邏輯,down寫降級的邏輯。 ~~~ public function change() { // create the table $table = $this->table('user_logins'); $table->addColumn('user_id', 'integer') ->addColumn('created', 'datetime') ->create(); } /** * Migrate Up. */ public function up() { } /** * Migrate Down. */ public function down() { } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看