From 9758d84b69185f80fd8197f28046af7ef3b2a2d3 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Mon, 7 Feb 2011 20:39:00 +0000 Subject: Added Migrations library, config and an example controller/migration file. --- application/config/migration.php | 38 ++++++++++++++++++++++++ application/controllers/migrate.php | 40 ++++++++++++++++++++++++++ application/migrations/001_Create_accounts.php | 32 +++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 application/config/migration.php create mode 100644 application/controllers/migrate.php create mode 100644 application/migrations/001_Create_accounts.php (limited to 'application') 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 @@ +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 @@ +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 "
Migration Successful
"; + } + + // 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 "
Migration Successful
"; + } +} 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 @@ +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'); + } +} -- cgit v1.2.3-24-g4f1b