blob: cb7d96a6d22a81e15db9b49542794aec1f2e1ffb [file] [log] [blame]
Greg Akerf41c9cf2011-12-25 00:15:17 -06001################
2Migrations Class
3################
4
Joe McFrederick596e48d2012-09-04 14:25:47 -04005Migrations are a convenient way for you to alter your database in a
6structured and organized manner. You could edit fragments of SQL by hand
7but you would then be responsible for telling other developers that they
8need to go and run them. You would also have to keep track of which changes
9need to be run against the production machines next time you deploy.
10
11The database table **migration** tracks which migrations have already been
12run so all you have to do is update your application files and
13call **$this->migrate->current()** to work out which migrations should be run.
14The current version is found in **config/migration.php**.
15
16******************
17Create 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
24This will be the first migration for a new site which has a blog. All
25migrations go in the folder **application/migrations/** and have names such
26as: **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
59Then in **application/config/migration.php** set **$config['migration_version'] = 1;**.
60
61*************
62Usage Example
63*************
64
65In this example some simple code is placed in **application/controllers/migrate.php**
66to 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******************
76Function Reference
77******************
78
79There 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
90The current migration is whatever is set for **$config['migration_version']** in
91**application/config/migration.php**.
92
93$this->migration->error_string()
94=================================
95
96This returns a string of errors while performing a migration.
97
98$this->migration->find_migrations()
99====================================
100
101An array of migration filenames are returned that are found in the **migration_path**
102property.
103
104$this->migration->latest()
105===========================
106
107This works much the same way as current() but instead of looking for
108the **$config['migration_version']** the Migration class will use the very
109newest migration found in the filesystem.
110
111$this->migration->version()
112============================
113
114Version can be used to roll back changes or step forwards programmatically to
115specific 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*********************
122Migration Preferences
123*********************
124
125The following is a table of all the config options for migrations.
126
127========================== ====================== ============= =============================================
128Preference 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========================== ====================== ============= =============================================