blob: a007d5be7a0c922bacc9c258adb9834f27839dbb [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
Andrey Andreev11f58dc2014-01-03 13:57:58 +020013call ``$this->migration->current()`` to work out which migrations should be run.
14The current version is found in **application/config/migration.php**.
Joe McFrederick596e48d2012-09-04 14:25:47 -040015
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040016********************
17Migration file names
18********************
19
20Each Migration is run in numeric order forward or backwards depending on the
21method 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
Andrey Andreev11f58dc2014-01-03 13:57:58 +020031The desired style may be selected using the ``$config['migration_type']``
32setting in your *application/config/migration.php* file.
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040033
34Regardless of which numbering style you choose to use, prefix your migration
35files with the migration number followed by an underscore and a descriptive
36name for the migration. For example:
37
Andrey Andreev11f58dc2014-01-03 13:57:58 +020038* 001_add_blog.php (sequential numbering)
39* 20121031100537_add_blog.php (timestamp numbering)
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040040
Joe McFrederick596e48d2012-09-04 14:25:47 -040041******************
42Create a Migration
43******************
Joe McFrederick596e48d2012-09-04 14:25:47 -040044
45This will be the first migration for a new site which has a blog. All
Andrey Andreev11f58dc2014-01-03 13:57:58 +020046migrations go in the **application/migrations/** directory and have names such
47as *20121031100537_add_blog.php*.
48::
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040049
50 <?php
Joe McFrederick596e48d2012-09-04 14:25:47 -040051
52 defined('BASEPATH') OR exit('No direct script access allowed');
53
54 class Migration_Add_blog extends CI_Migration {
55
56 public function up()
57 {
58 $this->dbforge->add_field(array(
59 'blog_id' => array(
60 'type' => 'INT',
61 'constraint' => 5,
62 'unsigned' => TRUE,
63 'auto_increment' => TRUE
64 ),
65 'blog_title' => array(
66 'type' => 'VARCHAR',
67 'constraint' => '100',
68 ),
69 'blog_description' => array(
70 'type' => 'TEXT',
71 'null' => TRUE,
72 ),
73 ));
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040074 $this->dbforge->add_key('blog_id', TRUE);
Joe McFrederick596e48d2012-09-04 14:25:47 -040075 $this->dbforge->create_table('blog');
76 }
77
78 public function down()
79 {
80 $this->dbforge->drop_table('blog');
81 }
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040082 }
Joe McFrederick596e48d2012-09-04 14:25:47 -040083
Andrey Andreev11f58dc2014-01-03 13:57:58 +020084Then in **application/config/migration.php** set ``$config['migration_version'] = 1;``.
Joe McFrederick596e48d2012-09-04 14:25:47 -040085
86*************
87Usage Example
88*************
89
90In this example some simple code is placed in **application/controllers/migrate.php**
91to update the schema.::
92
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040093 <?php
94
95 class Migrate extends CI_Controller
Joe McFrederick596e48d2012-09-04 14:25:47 -040096 {
Andrey Andreev11f58dc2014-01-03 13:57:58 +020097
98 public function index()
99 {
100 $this->load->library('migration');
101
102 if ($this->migration->current() === FALSE)
103 {
104 show_error($this->migration->error_string());
105 }
106 }
107
Joe McFrederick596e48d2012-09-04 14:25:47 -0400108 }
109
Joe McFrederick596e48d2012-09-04 14:25:47 -0400110*********************
111Migration Preferences
112*********************
113
114The following is a table of all the config options for migrations.
115
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400116========================== ====================== ========================== =============================================
117Preference Default Options Description
118========================== ====================== ========================== =============================================
119**migration_enabled** FALSE TRUE / FALSE Enable or disable migrations.
120**migration_path** APPPATH.'migrations/' None The path to your migrations folder.
121**migration_version** 0 None The current version your database should use.
122**migration_table** migrations None The table name for storing the schema
123 version number.
124**migration_auto_latest** FALSE TRUE / FALSE Enable or disable automatically
125 running migrations.
vlakoff024cfec2013-01-09 18:10:20 +0100126**migration_type** 'timestamp' 'timestamp' / 'sequential' The type of numeric identifier used to name
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400127 migration files.
128========================== ====================== ========================== =============================================
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200129
130***************
131Class Reference
132***************
133
134.. class:: CI_Migration
135
136 .. method:: current()
137
138 :returns: mixed
139
140 Migrates up to the current version (whatever is set for ``$config['migration_version']`` in *application/config/migration.php*).
141
142 .. method:: error_string()
143
144 :returns: string
145
146 This returns a string of errors that were detected while performing a migration.
147
148 .. method:: find_migrations()
149
150 :returns: array
151
152 An array of migration filenames are returned that are found in the **migration_path** property.
153
154 .. method:: latest()
155
156 :returns: mixed
157
158 This works much the same way as ``current()`` but instead of looking for
159 the ``$config['migration_version']`` the Migration class will use the very
160 newest migration found in the filesystem.
161
162 .. method:: version($target_version)
163
164 :param mixed $target_version: Migration version to process
165 :returns: mixed
166
167 Version can be used to roll back changes or step forwards programmatically to
168 specific versions. It works just like ``current()`` but ignores ``$config['migration_version']``.
169 ::
170
171 $this->migration->version(5);