?
This document uses PHP Chinese website manual Release
遷移是以結(jié)構(gòu)化和有組織的方式更改數(shù)據(jù)庫的一種方便的方式。您可以手工編輯SQL片段,但隨后您將負(fù)責(zé)告訴其他開發(fā)人員他們需要去運(yùn)行它們。您還必須跟蹤下次部署時(shí)需要針對生產(chǎn)機(jī)器運(yùn)行哪些更改。
數(shù)據(jù)庫表遷移跟蹤哪些遷移已經(jīng)運(yùn)行,所以您所要做的就是更新應(yīng)用程序文件并調(diào)用$this->migration->current()
以確定應(yīng)該運(yùn)行哪些遷移。當(dāng)前版本可在application/config/移入.php...
遷移文件名
創(chuàng)建遷移
使用實(shí)例
遷移偏好
類引用
根據(jù)所采用的方法,每個(gè)遷移都按數(shù)字順序向前或向后運(yùn)行。有兩種編號樣式:
順序:每個(gè)遷移按順序編號,從001開始。每個(gè)數(shù)字必須是三位數(shù)字,并且序列中不得有空位。(這是CodeIgniter 3.0之前的編號方案。)
時(shí)間戳:使用YYYYMMDDHHIISS格式(例如20121031100537)創(chuàng)建遷移時(shí),使用時(shí)間戳對每個(gè)遷移進(jìn)行編號。這有助于防止在團(tuán)隊(duì)環(huán)境中工作時(shí)編號沖突,并且是CodeIgniter 3.0及更高版本中的首選方案。
可以使用$config['migration_type']
在你的application/config/移入.php檔案。
無論您選擇使用哪種編號樣式,遷移文件的前綴都是遷移號,后面是下劃線和遷移的描述性名稱。例如:
001_add_blog.php(順序編號)
20121031100537_add_blog.php(時(shí)間戳編號)
這將是一個(gè)擁有博客的新網(wǎng)站的首次遷移。所有遷移都在application / migrations /目錄中,并具有名稱,如20121031100537_add_blog.php。
<?phpdefined('BASEPATH') OR exit('No direct script access allowed'); class Migration_Add_blog extends CI_Migration { public function up() { $this->dbforge->add_field(array( 'blog_id' => array('type' => 'INT','constraint' => 5,'unsigned' => TRUE,'auto_increment' => TRUE), 'blog_title' => array('type' => 'VARCHAR','constraint' => '100',),'blog_description' => array('type' => 'TEXT','null' => TRUE,),)); $this->dbforge->add_key('blog_id', TRUE); $this->dbforge->create_table('blog'); } public function down() { $this->dbforge->drop_table('blog'); } }
然后進(jìn)去application/config/移入.php集$config['migration_version'] = 20121031100537;
...
在這個(gè)例子中,一些簡單的代碼放在應(yīng)用程序/控制器/Migrate.php若要更新架構(gòu),請執(zhí)行以下操作:
<?phpclass Migrate extends CI_Controller{ public function index(){ $this->load->library('migration'); if ($this->migration->current() === FALSE) { show_error($this->migration->error_string());} } }
下面是用于遷移的所有配置選項(xiàng)的表。
偏好 | 默認(rèn) | 選項(xiàng) | 描述 |
---|---|---|---|
migration_enabled | 假 | 真假 | 啟用或禁用遷移。 |
migration_path | APPPATH.'migrations /” | 沒有 | 您的遷移文件夾的路徑。 |
migration_version | 0 | 沒有 | 數(shù)據(jù)庫應(yīng)該使用的當(dāng)前版本。 |
migration_table | 遷移 | 沒有 | 用于存儲(chǔ)模式版本號的表名。 |
migration_auto_latest | 假 | 真假 | 啟用或禁用自動(dòng)運(yùn)行遷移。 |
MIGRATION_TYPE | “時(shí)間戳” | '時(shí)間戳'/'順序' | 用于命名遷移文件的數(shù)字標(biāo)識(shí)符的類型。 |
class CI_Migrationcurrent()
返回: | 如果未找到遷移,則為TRUE,成功時(shí)為當(dāng)前版本字符串,失敗時(shí)為FALSE |
---|---|
返回類型: | mixed |
error_string()
返回: | 錯(cuò)誤消息 |
---|---|
返回類型: | 串 |
find_migrations()
返回: | 一組遷移文件 |
---|---|
返回類型: | 數(shù)組 |
latest()
返回: | 成功時(shí)的當(dāng)前版本字符串,失敗時(shí)為FALSE |
---|---|
返回類型: | mixed |
version($target_version)
參數(shù): | $ target_version(混合) - 將遷移版本處理 |
---|---|
返回: | 如果未找到遷移,則為TRUE,成功時(shí)為當(dāng)前版本字符串,失敗時(shí)為FALSE |
返回類型: | mixed |
$ target_version(混合) - 將遷移版本處理
Returns: TRUE if no migrations are found, current version string on success, FALSE on failure
Return type: mixed
版本可用于回滾更改或以編程方式前進(jìn)到特定版本。它像`current()`一樣工作,但忽略`$ config ['migration_version']`。