cloudmanic / codeigniter-migrations Goto Github PK
View Code? Open in Web Editor NEWA database migration utility for Codeigniter inspired by Ruby on Rails.
Home Page: http://www.cloudmanic.com
A database migration utility for Codeigniter inspired by Ruby on Rails.
Home Page: http://www.cloudmanic.com
By: Spicer Matthews <[email protected]> Cloudmanic Labs, LLC http://www.cloudmanic.com Based On: http://codeigniter.com/wiki/Migrations/ By: Mat'as Montes Description: An open source utility for Codeigniter inspired by Ruby on Rails. The one thing Ruby on Rails has that Codeigniter does not have built in is database migrations. That function to keep track of database chages (versions) and migrate your database to what ever version you need. Migrate up or migrate down. With this library you can now do this. This library is not complete, please read http://codeigniter.com/wiki/Migrations for future needs and issues. This "fork" of Mat'as orginal work just tweets something to work better in our work. Both Libraries are powerful and work in nearly the same way. Maybe someday we can create a joint project with Mat'as or even better get this into that core of Codeigniter. Install: Copy the files to these locations. migrate.php -> application/libraries/ dbmigrate.php -> applcation/controllers/ The migration files included in this are just examples. You should install them where ever you point your $config["migrations_path"] to. Add these config to your config.php /* |-------------------------------------------------------------------------- | 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. | | Some more severe security measures might take place in future releases. | */ $config["migrations_enabled"] = TRUE; /* |-------------------------------------------------------------------------- | 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["migrations_path"] = APPPATH . "migrations/"; /* |-------------------------------------------------------------------------- | Migrations version |-------------------------------------------------------------------------- | | This is used to set the default migration for this code base. | Sometimes you want the system to automaticly migrate the database | to the most current migration. Or there might be higher migrations | that are not part of the production env. Setting the migration does | does nothing here. It is a way for a programer to check the config. | | On login you might want to do something like this | $this->migrate->version($this->config->item('migrations_version')); | */ $config["migrations_version"] = 6; Usage: The code is all based out of a library so you can call it anywhere with. $this->load->library('migrate'); $this->migrate->setverbose(TRUE); // echo statments or not $this->migrate->version(id); // migrate the database to a particular version $this->migrate->install(); // install to the latest version. The dbmigreate.php controller just shows the use of these functions. If you are going to use it. remove the 'die();' and put it back in place when you are done. ** THE KICKER ** At the bottom of migreate.php we assume you are keeping track of your versions with a db table called config. Yes you can not use this migration script to create that table. You need at least one table installed to boot strap this. You will want to review the _update_schema_version() function to either modify or copy how it works. Also, in our applcations we have this code to set our applcation configs. We do not believe in writting to the file system that is what a database is for :). // // Setup Config. // function setup_system_config() { $query = $this->db->get('config'); foreach ($query->result() AS $row) $this->config->set_item($row->Config_Name, $row->Config_Data); } In this code we set. $this->CI->config->item('migrationversion'); and that is how _get_schema_version() gets the current version. This is the version this database is set too. ** Suggestion ** We also do this on login to make sure this database is migrated to the version the config files wants it to be migrated too. // Make sure our database is up-to-date $this->migrate->setverbose(FALSE); if(! $this->migrate->version($this->config->item('migrations_version'))) show_error($this->migrate->error); Over all you should read this and http://codeigniter.com/wiki/Migrations to get an over all feel for this and make any changes you need for your applcation. Other Helpful Stuff: Included in this git repo is an example system.php file. A way to have migrations run on every page load. You can copy it directly or use it as an example for your own code structure. When you auto load the system.php lib this should be your order: $autoload['libraries'] = array('database', 'migrate', 'system', 'session'); Here is a video walk through of migrations in action. http://www.vimeo.com/13490644
Is there one?
i tried to do migrations with more database: a default database and another. This is the database.php file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => '',
'database' => 'buy_prova_test',
'dbdriver' => 'mysqli',
'dbprefix' => 'ext_',
'pconnect' => FALSE,
//'db_debug' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['oauth_test'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => '',
'database' => 'oauth_test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
//'db_debug' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
?>
This is, instead, the first migration that runs fine:
<?php
class Migration_Create_external_site_manager extends CI_Migration
{
public function up()
{
$this->dbforge->add_field("
ID bigint(20) NOT NULL AUTO_INCREMENT,
sito varchar(255) NOT NULL,
url_override text NOT NULL,
title varchar(255) NOT NULL,
title_facebook text NOT NULL,
description text NOT NULL,
description_facebook text NOT NULL,
description_twitter text NOT NULL,
img_facebook text NOT NULL,
img_twitter text NOT NULL,
PRIMARY KEY (ID)"
);
$this->dbforge->create_table('external_site_manager');
}
public function down()
{
$this->dbforge->drop_table('external_site_manager');
}
}
?>
And this is the second migration that i would do on oauth_test database, it doesn't return error, but i don't see any table in the database:
<?php
class Migration_Create_oauth_test extends CI_Migration
{
public function up()
{
$oauth=$this->load->database('oauth_test',true);
$this->oauth_forge=$this->load->dbforge($oauth,TRUE));
$this->oauth_forge->add_field("
access_token varchar(40) NOT NULL,
client_id varchar(80) NOT NULL,
user_id varchar(255) DEFAULT NULL,
expires timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
scope varchar(2000) DEFAULT NULL,
PRIMARY KEY (access_token)");
$this->oauth_forge->create_table("oauth_access_tokens");
$this->oauth_forge->add_field("
client_id varchar(80) NOT NULL,
client_secret varchar(80) NOT NULL,
redirect_uri varchar(2000) NOT NULL,
grant_types varchar(80) DEFAULT NULL,
scope varchar(100) DEFAULT NULL,
user_id varchar(80) DEFAULT NULL,
PRIMARY KEY (client_id)
");
$this->oauth_forge->create_table("oauth_clients");
}
public function down()
{
$oauth=$this->load->database('oauth_test',true);
$this->oauth_forge=$this->load->dbforge($oauth,TRUE));
$this->oauth_forge->drop_table("oauth_clients");
$this->oauth_forge->drop_table("oauth_access_tokens");
}
}
?>
Thank you in advance for help!
Error
Unable to load the requested language file: language/english/migrations_lang.php
I am resetting 'config/migration.php' $config['migration_version'] = 1; and renaming the file name 'migration/001_users.php', but it shows following error.
"This migration could not be found."
thanks in advance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.