blob: 97c72303c0949d6a0585b22b85496b3849b154d4 [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
Andrey Andreevcc042092014-01-03 17:08:27 +020016.. contents::
17 :local:
18
19.. raw:: html
20
21 <div class="custom-index container"></div>
22
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040023********************
24Migration file names
25********************
26
27Each Migration is run in numeric order forward or backwards depending on the
28method taken. Two numbering styles are available:
29
30* **Sequential:** each migration is numbered in sequence, starting with **001**.
31 Each number must be three digits, and there must not be any gaps in the
32 sequence. (This was the numbering scheme prior to CodeIgniter 3.0.)
33* **Timestamp:** each migration is numbered using the timestamp when the migration
34 was created, in **YYYYMMDDHHIISS** format (e.g. **20121031100537**). This
35 helps prevent numbering conflicts when working in a team environment, and is
36 the preferred scheme in CodeIgniter 3.0 and later.
37
Andrey Andreev11f58dc2014-01-03 13:57:58 +020038The desired style may be selected using the ``$config['migration_type']``
39setting in your *application/config/migration.php* file.
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040040
41Regardless of which numbering style you choose to use, prefix your migration
42files with the migration number followed by an underscore and a descriptive
43name for the migration. For example:
44
Andrey Andreev11f58dc2014-01-03 13:57:58 +020045* 001_add_blog.php (sequential numbering)
46* 20121031100537_add_blog.php (timestamp numbering)
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040047
Joe McFrederick596e48d2012-09-04 14:25:47 -040048******************
49Create a Migration
50******************
Joe McFrederick596e48d2012-09-04 14:25:47 -040051
52This will be the first migration for a new site which has a blog. All
Andrey Andreev11f58dc2014-01-03 13:57:58 +020053migrations go in the **application/migrations/** directory and have names such
54as *20121031100537_add_blog.php*.
55::
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040056
57 <?php
Joe McFrederick596e48d2012-09-04 14:25:47 -040058
59 defined('BASEPATH') OR exit('No direct script access allowed');
60
61 class Migration_Add_blog extends CI_Migration {
62
63 public function up()
64 {
65 $this->dbforge->add_field(array(
66 'blog_id' => array(
67 'type' => 'INT',
68 'constraint' => 5,
69 'unsigned' => TRUE,
70 'auto_increment' => TRUE
71 ),
72 'blog_title' => array(
73 'type' => 'VARCHAR',
74 'constraint' => '100',
75 ),
76 'blog_description' => array(
77 'type' => 'TEXT',
78 'null' => TRUE,
79 ),
80 ));
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040081 $this->dbforge->add_key('blog_id', TRUE);
Joe McFrederick596e48d2012-09-04 14:25:47 -040082 $this->dbforge->create_table('blog');
83 }
84
85 public function down()
86 {
87 $this->dbforge->drop_table('blog');
88 }
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040089 }
Joe McFrederick596e48d2012-09-04 14:25:47 -040090
kenjis7eedc632015-04-29 15:43:46 +090091Then in **application/config/migration.php** set ``$config['migration_version'] = 20121031100537;``.
Joe McFrederick596e48d2012-09-04 14:25:47 -040092
93*************
94Usage Example
95*************
96
Andrey Andreev20292312013-07-22 14:29:10 +030097In this example some simple code is placed in **application/controllers/Migrate.php**
Joe McFrederick596e48d2012-09-04 14:25:47 -040098to update the schema.::
99
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400100 <?php
101
102 class Migrate extends CI_Controller
Joe McFrederick596e48d2012-09-04 14:25:47 -0400103 {
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200104
105 public function index()
106 {
107 $this->load->library('migration');
108
109 if ($this->migration->current() === FALSE)
110 {
111 show_error($this->migration->error_string());
112 }
113 }
114
Joe McFrederick596e48d2012-09-04 14:25:47 -0400115 }
116
Joe McFrederick596e48d2012-09-04 14:25:47 -0400117*********************
118Migration Preferences
119*********************
120
121The following is a table of all the config options for migrations.
122
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400123========================== ====================== ========================== =============================================
124Preference Default Options Description
125========================== ====================== ========================== =============================================
126**migration_enabled** FALSE TRUE / FALSE Enable or disable migrations.
127**migration_path** APPPATH.'migrations/' None The path to your migrations folder.
128**migration_version** 0 None The current version your database should use.
129**migration_table** migrations None The table name for storing the schema
130 version number.
131**migration_auto_latest** FALSE TRUE / FALSE Enable or disable automatically
132 running migrations.
vlakoff024cfec2013-01-09 18:10:20 +0100133**migration_type** 'timestamp' 'timestamp' / 'sequential' The type of numeric identifier used to name
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400134 migration files.
135========================== ====================== ========================== =============================================
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200136
137***************
138Class Reference
139***************
140
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200141.. php:class:: CI_Migration
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200142
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200143 .. php:method:: current()
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200144
Andrey Andreev28c2c972014-02-08 04:27:48 +0200145 :returns: TRUE if no migrations are found, current version string on success, FALSE on failure
146 :rtype: mixed
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200147
Andrey Andreev28c2c972014-02-08 04:27:48 +0200148 Migrates up to the current version (whatever is set for
149 ``$config['migration_version']`` in *application/config/migration.php*).
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200150
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200151 .. php:method:: error_string()
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200152
Andrey Andreev28c2c972014-02-08 04:27:48 +0200153 :returns: Error messages
154 :rtype: string
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200155
156 This returns a string of errors that were detected while performing a migration.
157
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200158 .. php:method:: find_migrations()
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200159
Andrey Andreev28c2c972014-02-08 04:27:48 +0200160 :returns: An array of migration files
161 :rtype: array
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200162
163 An array of migration filenames are returned that are found in the **migration_path** property.
164
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200165 .. php:method:: latest()
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200166
ftwbzhao8d132bb2015-05-21 20:28:54 +0800167 :returns: Current version string on success, FALSE on failure
Andrey Andreev28c2c972014-02-08 04:27:48 +0200168 :rtype: mixed
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200169
170 This works much the same way as ``current()`` but instead of looking for
171 the ``$config['migration_version']`` the Migration class will use the very
172 newest migration found in the filesystem.
173
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200174 .. php:method:: version($target_version)
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200175
Andrey Andreev28c2c972014-02-08 04:27:48 +0200176 :param mixed $target_version: Migration version to process
177 :returns: TRUE if no migrations are found, current version string on success, FALSE on failure
178 :rtype: mixed
Andrey Andreev11f58dc2014-01-03 13:57:58 +0200179
180 Version can be used to roll back changes or step forwards programmatically to
181 specific versions. It works just like ``current()`` but ignores ``$config['migration_version']``.
182 ::
183
kenjis7eedc632015-04-29 15:43:46 +0900184 $this->migration->version(5);