summaryrefslogtreecommitdiffstats
path: root/application
diff options
context:
space:
mode:
authorPhil Sturgeon <email@philsturgeon.co.uk>2011-02-07 21:39:00 +0100
committerPhil Sturgeon <email@philsturgeon.co.uk>2011-02-07 21:39:00 +0100
commit9758d84b69185f80fd8197f28046af7ef3b2a2d3 (patch)
treeed4d30bd2b0d9bee4603cd5cd2943969e59f7119 /application
parentef112c0830df4a31563351125888b0d522a1c965 (diff)
Added Migrations library, config and an example controller/migration file.
Diffstat (limited to 'application')
-rw-r--r--application/config/migration.php38
-rw-r--r--application/controllers/migrate.php40
-rw-r--r--application/migrations/001_Create_accounts.php32
3 files changed, 110 insertions, 0 deletions
diff --git a/application/config/migration.php b/application/config/migration.php
new file mode 100644
index 000000000..37b1b8534
--- /dev/null
+++ b/application/config/migration.php
@@ -0,0 +1,38 @@
+<?php defined('BASEPATH') OR exit('No direct script access allowed');
+/*
+|--------------------------------------------------------------------------
+| Enable/Disable Migrations
+|--------------------------------------------------------------------------
+|
+| Migrations are disabled by default for security reasons.
+| You should enable migrations whenever you intend to do a schema migration
+| and disable it back when you're done.
+|
+*/
+$config['migration_enabled'] = TRUE;
+
+
+/*
+|--------------------------------------------------------------------------
+| Migrations version
+|--------------------------------------------------------------------------
+|
+| This is used to set migration version that the file system should be on.
+| If you run $this->migration->latest() this is the version that schema will
+| be upgraded / downgraded to.
+|
+*/
+$config['migration_version'] = 1;
+
+
+/*
+|--------------------------------------------------------------------------
+| Migrations Path
+|--------------------------------------------------------------------------
+|
+| Path to your migrations folder.
+| Typically, it will be within your application path.
+| Also, writing permission is required within the migrations path.
+|
+*/
+$config['migration_path'] = APPPATH . 'migrations/';
diff --git a/application/controllers/migrate.php b/application/controllers/migrate.php
new file mode 100644
index 000000000..e5442e79c
--- /dev/null
+++ b/application/controllers/migrate.php
@@ -0,0 +1,40 @@
+<?php
+class Migrate extends CI_Controller
+{
+ function __construct()
+ {
+ parent::__construct();
+
+ $this->load->library('migration');
+
+ /** VERY IMPORTANT - only turn this on when you need it. */
+// show_error('Access to this controller is blocked, turn me on when you need me.');
+ }
+
+ // Install up to the most up-to-date version.
+ function install()
+ {
+ if ( ! $this->migration->current())
+ {
+ show_error($this->migration->error);
+ exit;
+ }
+
+ echo "<br />Migration Successful<br />";
+ }
+
+ // This will migrate up to the configed migration version
+ function version($id = NULL)
+ {
+ // No $id supplied? Use the config version
+ $id OR $id = $this->config->item('migration_version');
+
+ if ( ! $this->migration->version($id))
+ {
+ show_error($this->migration->error);
+ exit;
+ }
+
+ echo "<br />Migration Successful<br />";
+ }
+}
diff --git a/application/migrations/001_Create_accounts.php b/application/migrations/001_Create_accounts.php
new file mode 100644
index 000000000..4b2fc936f
--- /dev/null
+++ b/application/migrations/001_Create_accounts.php
@@ -0,0 +1,32 @@
+<?php defined('BASEPATH') OR exit('No direct script access allowed');
+
+class Migration_Create_accounts extends CI_Migration {
+
+ function up()
+ {
+ if ( ! $this->db->table_exists('accounts'))
+ {
+ // Setup Keys
+ $this->dbforge->add_key('id', TRUE);
+
+ $this->dbforge->add_field(array(
+ 'id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE),
+ 'company_name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE),
+ 'first_name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE),
+ 'last_name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE),
+ 'phone' => array('type' => 'TEXT', 'null' => FALSE),
+ 'email' => array('type' => 'TEXT', 'null' => FALSE),
+ 'address' => array('type' => 'TEXT', 'null' => FALSE),
+ 'Last_Update' => array('type' => 'DATETIME', 'null' => FALSE)
+ ));
+
+ $this->dbforge->add_field("Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");
+ $this->dbforge->create_table('accounts', TRUE);
+ }
+ }
+
+ function down()
+ {
+ $this->dbforge->drop_table('accounts');
+ }
+}