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 |
| 13 | call **$this->migrate->current()** to work out which migrations should be run. |
| 14 | The current version is found in **config/migration.php**. |
| 15 | |
| 16 | ****************** |
| 17 | Create a Migration |
| 18 | ****************** |
| 19 | |
| 20 | .. note:: Each Migration is run in numerical order forward or backwards |
| 21 | depending on the method taken. Use a prefix of 3 numbers followed by an |
| 22 | underscore for the filename of your migration. |
| 23 | |
| 24 | This will be the first migration for a new site which has a blog. All |
| 25 | migrations go in the folder **application/migrations/** and have names such |
| 26 | as: **001_add_blog.php**.:: |
| 27 | |
| 28 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 29 | |
| 30 | class Migration_Add_blog extends CI_Migration { |
| 31 | |
| 32 | public function up() |
| 33 | { |
| 34 | $this->dbforge->add_field(array( |
| 35 | 'blog_id' => array( |
| 36 | 'type' => 'INT', |
| 37 | 'constraint' => 5, |
| 38 | 'unsigned' => TRUE, |
| 39 | 'auto_increment' => TRUE |
| 40 | ), |
| 41 | 'blog_title' => array( |
| 42 | 'type' => 'VARCHAR', |
| 43 | 'constraint' => '100', |
| 44 | ), |
| 45 | 'blog_description' => array( |
| 46 | 'type' => 'TEXT', |
| 47 | 'null' => TRUE, |
| 48 | ), |
| 49 | )); |
| 50 | |
| 51 | $this->dbforge->create_table('blog'); |
| 52 | } |
| 53 | |
| 54 | public function down() |
| 55 | { |
| 56 | $this->dbforge->drop_table('blog'); |
| 57 | } |
| 58 | |
| 59 | Then in **application/config/migration.php** set **$config['migration_version'] = 1;**. |
| 60 | |
| 61 | ************* |
| 62 | Usage Example |
| 63 | ************* |
| 64 | |
| 65 | In this example some simple code is placed in **application/controllers/migrate.php** |
| 66 | to update the schema.:: |
| 67 | |
| 68 | $this->load->library('migration'); |
| 69 | |
| 70 | if ( ! $this->migration->current()) |
| 71 | { |
| 72 | show_error($this->migration->error_string()); |
| 73 | } |
| 74 | |
| 75 | ****************** |
| 76 | Function Reference |
| 77 | ****************** |
| 78 | |
| 79 | There are five available methods for the Migration class: |
| 80 | |
| 81 | - $this->migration->current(); |
| 82 | - $this->migration->error_string(); |
| 83 | - $this->migration->find_migrations(); |
| 84 | - $this->migration->latest(); |
| 85 | - $this->migration->version(); |
| 86 | |
| 87 | $this->migration->current() |
| 88 | ============================ |
| 89 | |
| 90 | The current migration is whatever is set for **$config['migration_version']** in |
| 91 | **application/config/migration.php**. |
| 92 | |
| 93 | $this->migration->error_string() |
| 94 | ================================= |
| 95 | |
| 96 | This returns a string of errors while performing a migration. |
| 97 | |
| 98 | $this->migration->find_migrations() |
| 99 | ==================================== |
| 100 | |
| 101 | An array of migration filenames are returned that are found in the **migration_path** |
| 102 | property. |
| 103 | |
| 104 | $this->migration->latest() |
| 105 | =========================== |
| 106 | |
| 107 | This works much the same way as current() but instead of looking for |
| 108 | the **$config['migration_version']** the Migration class will use the very |
| 109 | newest migration found in the filesystem. |
| 110 | |
| 111 | $this->migration->version() |
| 112 | ============================ |
| 113 | |
| 114 | Version can be used to roll back changes or step forwards programmatically to |
| 115 | specific versions. It works just like current but ignores **$config['migration_version']**.:: |
| 116 | |
| 117 | $this->load->library('migration'); |
| 118 | |
| 119 | $this->migration->version(5); |
| 120 | |
| 121 | ********************* |
| 122 | Migration Preferences |
| 123 | ********************* |
| 124 | |
| 125 | The following is a table of all the config options for migrations. |
| 126 | |
| 127 | ========================== ====================== ============= ============================================= |
| 128 | Preference Default Options Description |
| 129 | ========================== ====================== ============= ============================================= |
| 130 | **migration_enabled** FALSE TRUE / FALSE Enable or disable migrations. |
| 131 | **migration_path** APPPATH.'migrations/' None The path to your migrations folder. |
| 132 | **migration_version** 0 None The current version your database should use. |
| 133 | **migration_table** migrations None The table name for storing the shema |
| 134 | version number. |
| 135 | **migration_auto_latest** FALSE TRUE / FALSE Enable or disable automatically |
| 136 | running migrations. |
| 137 | ========================== ====================== ============= ============================================= |