Greg Aker | f41c9cf | 2011-12-25 00:15:17 -0600 | [diff] [blame] | 1 | ################ |
| 2 | Migrations Class |
| 3 | ################ |
| 4 | |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 5 | Migrations are a convenient way for you to alter your database in a |
| 6 | structured and organized manner. You could edit fragments of SQL by hand |
| 7 | but you would then be responsible for telling other developers that they |
| 8 | need to go and run them. You would also have to keep track of which changes |
| 9 | need to be run against the production machines next time you deploy. |
| 10 | |
| 11 | The database table **migration** tracks which migrations have already been |
| 12 | run so all you have to do is update your application files and |
Cory | 05f3ad2 | 2013-02-21 10:36:31 -0500 | [diff] [blame] | 13 | call **$this->migration->current()** to work out which migrations should be run. |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 14 | The current version is found in **config/migration.php**. |
| 15 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 16 | ******************** |
| 17 | Migration file names |
| 18 | ******************** |
| 19 | |
| 20 | Each Migration is run in numeric order forward or backwards depending on the |
| 21 | method taken. Two numbering styles are available: |
| 22 | |
| 23 | * **Sequential:** each migration is numbered in sequence, starting with **001**. |
| 24 | Each number must be three digits, and there must not be any gaps in the |
| 25 | sequence. (This was the numbering scheme prior to CodeIgniter 3.0.) |
| 26 | * **Timestamp:** each migration is numbered using the timestamp when the migration |
| 27 | was created, in **YYYYMMDDHHIISS** format (e.g. **20121031100537**). This |
| 28 | helps prevent numbering conflicts when working in a team environment, and is |
| 29 | the preferred scheme in CodeIgniter 3.0 and later. |
| 30 | |
Jonathon Hill | b719bfd | 2012-11-12 09:03:36 -0500 | [diff] [blame] | 31 | The desired style may be selected using the **$config['migration_type']** |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 32 | setting in your **migration.php** config file. |
| 33 | |
| 34 | Regardless of which numbering style you choose to use, prefix your migration |
| 35 | files with the migration number followed by an underscore and a descriptive |
| 36 | name for the migration. For example: |
| 37 | |
| 38 | * **001_add_blog.php** (sequential numbering) |
| 39 | * **20121031100537_add_blog.php** (timestamp numbering) |
| 40 | |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 41 | ****************** |
| 42 | Create a Migration |
| 43 | ****************** |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 44 | |
| 45 | This will be the first migration for a new site which has a blog. All |
| 46 | migrations go in the folder **application/migrations/** and have names such |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 47 | as **20121031100537_add_blog.php**.:: |
| 48 | |
| 49 | <?php |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 50 | |
| 51 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 52 | |
| 53 | class Migration_Add_blog extends CI_Migration { |
| 54 | |
| 55 | public function up() |
| 56 | { |
| 57 | $this->dbforge->add_field(array( |
| 58 | 'blog_id' => array( |
| 59 | 'type' => 'INT', |
| 60 | 'constraint' => 5, |
| 61 | 'unsigned' => TRUE, |
| 62 | 'auto_increment' => TRUE |
| 63 | ), |
| 64 | 'blog_title' => array( |
| 65 | 'type' => 'VARCHAR', |
| 66 | 'constraint' => '100', |
| 67 | ), |
| 68 | 'blog_description' => array( |
| 69 | 'type' => 'TEXT', |
| 70 | 'null' => TRUE, |
| 71 | ), |
| 72 | )); |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 73 | $this->dbforge->add_key('blog_id', TRUE); |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 74 | $this->dbforge->create_table('blog'); |
| 75 | } |
| 76 | |
| 77 | public function down() |
| 78 | { |
| 79 | $this->dbforge->drop_table('blog'); |
| 80 | } |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 81 | } |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 82 | |
| 83 | Then in **application/config/migration.php** set **$config['migration_version'] = 1;**. |
| 84 | |
| 85 | ************* |
| 86 | Usage Example |
| 87 | ************* |
| 88 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 89 | In this example some simple code is placed in **application/controllers/Migrate.php** |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 90 | to update the schema.:: |
| 91 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 92 | <?php |
| 93 | |
| 94 | class Migrate extends CI_Controller |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 95 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 96 | public function index() |
| 97 | { |
| 98 | $this->load->library('migration'); |
| 99 | |
| 100 | if ($this->migration->current() === FALSE) |
| 101 | { |
| 102 | show_error($this->migration->error_string()); |
| 103 | } |
| 104 | } |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ****************** |
| 108 | Function Reference |
| 109 | ****************** |
| 110 | |
Joe McFrederick | 596e48d | 2012-09-04 14:25:47 -0400 | [diff] [blame] | 111 | $this->migration->current() |
| 112 | ============================ |
| 113 | |
| 114 | The current migration is whatever is set for **$config['migration_version']** in |
| 115 | **application/config/migration.php**. |
| 116 | |
| 117 | $this->migration->error_string() |
| 118 | ================================= |
| 119 | |
| 120 | This returns a string of errors while performing a migration. |
| 121 | |
| 122 | $this->migration->find_migrations() |
| 123 | ==================================== |
| 124 | |
| 125 | An array of migration filenames are returned that are found in the **migration_path** |
| 126 | property. |
| 127 | |
| 128 | $this->migration->latest() |
| 129 | =========================== |
| 130 | |
| 131 | This works much the same way as current() but instead of looking for |
| 132 | the **$config['migration_version']** the Migration class will use the very |
| 133 | newest migration found in the filesystem. |
| 134 | |
| 135 | $this->migration->version() |
| 136 | ============================ |
| 137 | |
| 138 | Version can be used to roll back changes or step forwards programmatically to |
| 139 | specific versions. It works just like current but ignores **$config['migration_version']**.:: |
| 140 | |
| 141 | $this->load->library('migration'); |
| 142 | |
| 143 | $this->migration->version(5); |
| 144 | |
| 145 | ********************* |
| 146 | Migration Preferences |
| 147 | ********************* |
| 148 | |
| 149 | The following is a table of all the config options for migrations. |
| 150 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 151 | ========================== ====================== ========================== ============================================= |
| 152 | Preference Default Options Description |
| 153 | ========================== ====================== ========================== ============================================= |
| 154 | **migration_enabled** FALSE TRUE / FALSE Enable or disable migrations. |
| 155 | **migration_path** APPPATH.'migrations/' None The path to your migrations folder. |
| 156 | **migration_version** 0 None The current version your database should use. |
| 157 | **migration_table** migrations None The table name for storing the schema |
| 158 | version number. |
| 159 | **migration_auto_latest** FALSE TRUE / FALSE Enable or disable automatically |
| 160 | running migrations. |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 161 | **migration_type** 'timestamp' 'timestamp' / 'sequential' The type of numeric identifier used to name |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 162 | migration files. |
| 163 | ========================== ====================== ========================== ============================================= |