數(shù)據(jù)庫(kù)遷移?
遷移是一種有條理、有組織的方式更改數(shù)據(jù)庫(kù)的便捷方式。你可以手動(dòng)編輯SQL的片段,然后你要負(fù)責(zé)告訴其他開(kāi)發(fā)人員他們也需要去運(yùn)行這段SQL。你還必須跟蹤下次部署時(shí)需要對(duì)生產(chǎn)機(jī)器運(yùn)行哪些更改。
數(shù)據(jù)庫(kù)表**遷移**會(huì)跟蹤已經(jīng)運(yùn)行的遷移,因此您只需更新應(yīng)用程序文件并調(diào)用$migration->current()以確定應(yīng)運(yùn)行哪些遷移。當(dāng)前版本位于**application/Config/Migrations.php**中。
- 遷移文件名
- 創(chuàng)建遷移
- 使用$currentVersion
- 數(shù)據(jù)庫(kù)組
- 命名空間
- 用法示例
- 命令行工具
- 遷移參數(shù)
- 類參考
遷移文件名?
每個(gè)遷移都按數(shù)字順序向前或向后運(yùn)行,具體取決于所采用的方法。有兩種編號(hào)樣式可供選擇:
- 順序:每個(gè)遷移按順序編號(hào),從001開(kāi)始。每個(gè)數(shù)字必須是三位數(shù),并且序列中不得有任何間隙。(這是CodeIgniter 3.0之前的編號(hào)方案。)
- 時(shí)間戳:使用創(chuàng)建遷移時(shí)的時(shí)間戳對(duì)每個(gè)遷移進(jìn)行編號(hào),格式為**YYYYMMDDHHIES**格式(例如**20121031100537**)。這有助于防止在團(tuán)隊(duì)環(huán)境中工作時(shí)出現(xiàn)編號(hào)沖突,并且是CodeIgniter 3.0及更高版本中的首選方案。
可以使用*application/Config/Migrations.php*文件中的$type設(shè)置選擇所需的樣式。默認(rèn)設(shè)置為時(shí)間戳。
無(wú)論您選擇使用哪種編號(hào)樣式,請(qǐng)?jiān)谶w移文件前加上遷移編號(hào),后跟下劃線和遷移的描述性名稱。例如:
- 001_add_blog.php(順序編號(hào))
- 20121031100537_add_blog.php(時(shí)間戳編號(hào))
創(chuàng)建遷移?
這將是新博客站點(diǎn)的首次遷移。所有遷移都在 application/Database/Migrations/ 目錄中,并命名,如 20121031100537_Add_blog.php。
<?php namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AddBlog extends Migration
{
public function up()
{
$this->forge->addField([
'blog_id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => true,
'auto_increment' => true,
],
'blog_title' => [
'type' => 'VARCHAR',
'constraint' => '100',
],
'blog_description' => [
'type' => 'TEXT',
'null' => true,
],
]);
$this->forge->addKey('blog_id', true);
$this->forge->createTable('blog');
}
public function down()
{
$this->forge->dropTable('blog');
}
}
然后在 application/Config/Migrations.php 中設(shè)置 $currentVersion = 20121031100537;。
數(shù)據(jù)庫(kù)連接和數(shù)據(jù)庫(kù)Forge類都可以通過(guò) $this->db和$this->forge分別使用。
或者,你可以使用命令行調(diào)用來(lái)生成框架遷移文件。請(qǐng)參閱下面的更多細(xì)節(jié)。
使用$currentVersion?
$currentVersion設(shè)置允許你標(biāo)記應(yīng)用程序命名空間應(yīng)設(shè)置的位置。這對(duì)于在生產(chǎn)環(huán)境中使用尤其有用。在你的應(yīng)用程序中,你始終可以將遷移更新到當(dāng)前版本,而不是最新版本,以確保生產(chǎn)和登臺(tái)服務(wù)器正在運(yùn)行正確的架構(gòu)。在開(kāi)發(fā)服務(wù)器上,你可以為尚未準(zhǔn)備好生產(chǎn)的代碼添加其他遷移。通過(guò)使用該latest()方法,你可以確保你的開(kāi)發(fā)機(jī)器始終運(yùn)行前沿架構(gòu)。
數(shù)據(jù)庫(kù)組?
只能針對(duì)單個(gè)數(shù)據(jù)庫(kù)組運(yùn)行遷移。如果 在application/Config/Database.php 中定義了多個(gè)組 ,則它將針對(duì)該$defaultGroup同一配置文件中指定的組運(yùn)行。有時(shí)你可能需要為不同的數(shù)據(jù)庫(kù)組使用不同的模式。也許你有一個(gè)用于所有常規(guī)站點(diǎn)信息的數(shù)據(jù)庫(kù),而另一個(gè)數(shù)據(jù)庫(kù)用于關(guān)鍵任務(wù)數(shù)據(jù)。通過(guò)$DBGroup在遷移上設(shè)置屬性,可以確保僅針對(duì)正確的組運(yùn)行遷移。此名稱必須與數(shù)據(jù)庫(kù)組的名稱完全匹配:
class Migration_Add_blog extends \CodeIgniter\Database\Migration
{
protected $DBGroup = 'alternate_db_group';
public function up() { . . . }
public function down() { . . . }
}
命名空間?
遷移庫(kù)可以自動(dòng)掃描你在 application/Config/Autoload.php 中定義的所有名稱空間 及其$psr4屬性以匹配目錄名稱。它將包括它在Database/Migrations中找到的所有遷移。
每個(gè)命名空間都有自己的版本序列,這將幫助您升級(jí)和降級(jí)每個(gè)模塊(命名空間),而不會(huì)影響其他命名空間。
例如,假設(shè)我們?cè)贏utoload配置文件中定義了以下命名空間:
$psr4 = [
'App' => APPPATH,
'MyCompany' => ROOTPATH.'MyCompany'
];
這將查找位于**APPPATH/Database/Migrations**和**ROOTPATH/Database/Migrations**的任何遷移。這使得在可重用的模塊化代碼套件中包含遷移變得簡(jiǎn)單。
用法示例?
在此示例中,一些簡(jiǎn)單的代碼放在 application/Controllers/Migrate.php 中以更新架構(gòu):
<?php
class Migrate extends \CodeIgniter\Controller
{
public function index()
{
$migrate = \Config\Services::migrations();
try
{
$migrate->current();
}
catch (\Exception $e)
{
// Do something with the error here...
}
}
}
命令行工具?
CodeIgniter附帶了幾個(gè):doc:commands </cli/cli_commands>,它們可以從命令行獲得,以幫助你處理遷移。這些工具不需要使用遷移,但可能會(huì)使那些希望使用它們的人更容易。這些工具主要提供對(duì)MigrationRunner類中可用的相同方法的訪問(wèn)。
migrate
Migrates a database group with all available migrations:
> php spark migrate
You can use (migrate) with the following options:
-g
- to chose database group, otherwise default database group will be used.-n
- to choose namespace, otherwise (App) namespace will be used.-all
- to migrate all namespaces to the latest migration
This example will migrate Blog namespace with any new migrations on the test database group:
> php spark migrate -g test -n Blog
When using the -all
option, it will scan through all namespaces attempting to find any migrations that have
not been run. These will all be collected and then sorted as a group by date created. This should help
to minimize any potential conflicts between the main application and any modules.
rollback
回滾所有遷移,將所有數(shù)據(jù)庫(kù)組轉(zhuǎn)為空白平板,有效遷移0:
> php spark migrate:rollback
你可以使用(rollback)以下選項(xiàng):
- (-g)選擇數(shù)據(jù)庫(kù)組,否則將使用默認(rèn)數(shù)據(jù)庫(kù)組。
- (-n)選擇名稱空間,否則將使用(App)名稱空間。
- (all)將所有名稱空間遷移到最新的遷移
refresh
首先回滾所有遷移,然后遷移到最新版本,刷新數(shù)據(jù)庫(kù)狀態(tài):
> php spark migrate:refresh
你可以使用(refresh)以下選項(xiàng):
- (-g)選擇數(shù)據(jù)庫(kù)組,否則將使用默認(rèn)數(shù)據(jù)庫(kù)組。
- (-n)選擇名稱空間,否則將使用(App)名稱空間。
- (all)將所有名稱空間遷移到最新的遷移
status
顯示所有遷移的列表及其運(yùn)行的日期和時(shí)間,如果尚未運(yùn)行,則顯示’–’:
> php spark migrate:status
Filename Migrated On
First_migration.php 2016-04-25 04:44:22
你可以使用(status)以下選項(xiàng):
- (-g)選擇數(shù)據(jù)庫(kù)組,否則將使用默認(rèn)數(shù)據(jù)庫(kù)組。
make:migration
Creates a skeleton migration file in app/Database/Migrations. It automatically prepends the current timestamp. The class name it creates is the Pascal case version of the filename.
> php spark make:migration <class> [options]
You can use (make:migration) with the following options:
-n
- to choose namespace, otherwise the value ofAPP_NAMESPACE
will be used.-force
- If a similarly named migration file is present in destination, this will be overwritten.
遷移參數(shù)?
以下是 app/Config/Migrations.php 中提供的所有遷移配置選項(xiàng)的表。
參數(shù) | 默認(rèn)值 | 可選項(xiàng) | 描述 |
---|---|---|---|
enabled | true | true / false | 啟用或者禁用遷移 |
table | migrations | None | 用于存儲(chǔ)當(dāng)前版本的數(shù)據(jù)庫(kù)表名 |
timestampFormat | Y-m-d-His_ | The format to use for timestamps when creating a migration. |
類參考?
- class
CodeIgniterDatabaseMigrationRunner
?
current
($group)?
參數(shù):
- $group (mixed) – database group name, if null (App) namespace will be used.
返回: TRUE if no migrations are found, current version string on success, FALSE on failure
返回類型: mixed
Migrates up to the current version (whatever is set for
$currentVersion
in application/Config/Migrations.php).
findMigrations
()?
返回: An array of migration files 返回類型: array An array of migration filenames are returned that are found in the path property.
latest
($namespace, $group)?
參數(shù):
- $namespace (mixed) – application namespace, if null (App) namespace will be used.
- $group (mixed) – database group name, if null default database group will be used.
返回: Current version string on success, FALSE on failure
返回類型: mixed
This works much the same way as
current()
but instead of looking for the$currentVersion
the Migration class will use the very newest migration found in the filesystem.
latestAll
($group)?
參數(shù):
- $group (mixed) – database group name, if null default database group will be used.
返回: TRUE on success, FALSE on failure
返回類型: mixed
This works much the same way as
latest()
but instead of looking for one namespace, the Migration class will use the very newest migration found for all namespaces.
version
($target_version, $namespace, $group)?
參數(shù):
- $namespace (mixed) – application namespace, if null (App) namespace will be used.
- $group (mixed) – database group name, if null default database group will be used.
- $target_version (mixed) – Migration version to process
返回: TRUE if no migrations are found, current version string on success, FALSE on failure
返回類型: mixed
Version can be used to roll back changes or step forwards programmatically to specific versions. It works just like
current()
but ignores$currentVersion
.$migration->version(5);
setNamespace
($namespace)?
參數(shù):
- $namespace (string) – application namespace.
返回: The current MigrationRunner instance
返回類型: Sets the path the library should look for migration files:
$migration->setNamespace($path) ->latest();
setGroup
($group)?
參數(shù):
- $group (string) – database group name.
返回: The current MigrationRunner instance
返回類型: Sets the path the library should look for migration files:
$migration->setNamespace($path) ->latest();