From 9c63d0bb34be4007178d5a7e46348d5e23fee3ff Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 27 Oct 2011 01:55:44 +0100 Subject: Bumped CodeIgniter version to 2.1.0. --- user_guide/libraries/migration.html | 176 ++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 user_guide/libraries/migration.html (limited to 'user_guide/libraries/migration.html') diff --git a/user_guide/libraries/migration.html b/user_guide/libraries/migration.html new file mode 100644 index 000000000..ed99044d1 --- /dev/null +++ b/user_guide/libraries/migration.html @@ -0,0 +1,176 @@ + + + + + +Migration Class : CodeIgniter User Guide + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 2.1.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

Migration Class

+ +

Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You’d also have to keep track of which changes need to be run against the production machines next time you deploy.

+ +

The database table migration tracks which migrations have already been run so all you have to do is update your application files and call $this->migrate->current() to work out which migrations should be run. The current version is found in config/migration.php.

+ +

Create a Migration

+ +

This will be the first migration for a new site which has a blog. All migrations go in the folder application/migrations/ and have names such as: 001_add_blog.php.

+ +
+defined('BASEPATH') OR exit('No direct script access allowed');
+
+class Migration_Add_blog extends CI_Migration {
+
+	public function up()
+	{
+		$this->dbforge->add_field(array(
+			'blog_id' => array(
+				'type' => 'INT',
+				'constraint' => 5,
+				'unsigned' => TRUE,
+				'auto_increment' => TRUE
+			),
+			'blog_title' => array(
+				'type' => 'VARCHAR',
+				'constraint' => '100',
+			),
+			'blog_description' => array(
+				'type' => 'TEXT',
+				'null' => TRUE,
+			),
+		));
+		
+		$this->dbforge->create_table('blog');
+	}
+
+	public function down()
+	{
+		$this->dbforge->drop_table('blog');
+	}
+
+ +

Then in application/config/migration.php set $config['migration_version'] = 1;. + +

Usage Example

+ +

In this example some simple code is placed in application/controllers/migrate.php to update the schema.

+ +
+$this->load->library('migration');
+
+if ( ! $this->migration->current())
+{
+	show_error($this->migration->error_string());
+}
+
+ + +

Function Reference

+ +

$this->migration->current()

+ +

The current migration is whatever is set for $config['migration_version'] in application/config/migration.php.

+ + +

$this->migration->latest()

+ +

This works much the same way as current() but instead of looking for the $config['migration_version'] the Migration class will use the very newest migration found in the filesystem.

+ +

$this->migration->version()

+ +

Version can be used to roll back changes or step forwards programmatically to specific versions. It works just like current but ignores $config['migration_version'].

+ +
+$this->load->library('migration');
+
+$this->migration->version(5);
+
+ +

Migration Preferences

+ +

The following is a list of all the config options for migrations.

+ + + + + + + + + + + + + + + +
PreferenceDefault ValueOptionsDescription
migration_enabledFALSETRUE / FALSEEnable or disable migrations.
migration_version0NoneThe current version your database should use.
migration_pathAPPPATH.'migrations/'NoneThe path to your migrations folder.
+ + +
+ + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b