├── README ├── dbmigrate.php ├── migrate.php ├── migrations ├── 001_Create_accounts.php ├── 002_Create_leads.php └── 003_Create_tags.php └── system.php /README: -------------------------------------------------------------------------------- 1 | By: 2 | Spicer Matthews 3 | Cloudmanic Labs, LLC 4 | http://www.cloudmanic.com 5 | 6 | Based On: 7 | http://codeigniter.com/wiki/Migrations/ 8 | By: Mat'as Montes 9 | 10 | Description: 11 | An open source utility for Codeigniter inspired by Ruby on Rails. 12 | 13 | The one thing Ruby on Rails has that Codeigniter does not have built in 14 | is database migrations. That function to keep track of database chages (versions) 15 | and migrate your database to what ever version you need. Migrate up or migrate down. 16 | With this library you can now do this. This library is not complete, please read 17 | http://codeigniter.com/wiki/Migrations for future needs and issues. This "fork" of 18 | Mat'as orginal work just tweets something to work better in our work. Both Libraries 19 | are powerful and work in nearly the same way. Maybe someday we can create a joint project 20 | with Mat'as or even better get this into that core of Codeigniter. 21 | 22 | Install: 23 | Copy the files to these locations. 24 | 25 | migrate.php -> application/libraries/ 26 | dbmigrate.php -> applcation/controllers/ 27 | 28 | The migration files included in this are just examples. You should install them where ever you 29 | point your $config["migrations_path"] to. 30 | 31 | Add these config to your config.php 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Enable/Disable Migrations 36 | |-------------------------------------------------------------------------- 37 | | 38 | | Migrations are disabled by default for security reasons. 39 | | You should enable migrations whenever you intend to do a schema migration 40 | | and disable it back when you're done. 41 | | 42 | | Some more severe security measures might take place in future releases. 43 | | 44 | */ 45 | $config["migrations_enabled"] = TRUE; 46 | 47 | /* 48 | |-------------------------------------------------------------------------- 49 | | Migrations Path 50 | |-------------------------------------------------------------------------- 51 | | 52 | | Path to your migrations folder. 53 | | Typically, it will be within your application path. 54 | | Also, writing permission is required within the migrations path. 55 | | 56 | */ 57 | $config["migrations_path"] = APPPATH . "migrations/"; 58 | 59 | 60 | /* 61 | |-------------------------------------------------------------------------- 62 | | Migrations version 63 | |-------------------------------------------------------------------------- 64 | | 65 | | This is used to set the default migration for this code base. 66 | | Sometimes you want the system to automaticly migrate the database 67 | | to the most current migration. Or there might be higher migrations 68 | | that are not part of the production env. Setting the migration does 69 | | does nothing here. It is a way for a programer to check the config. 70 | | 71 | | On login you might want to do something like this 72 | | $this->migrate->version($this->config->item('migrations_version')); 73 | | 74 | */ 75 | $config["migrations_version"] = 6; 76 | 77 | Usage: 78 | The code is all based out of a library so you can call it anywhere with. 79 | 80 | $this->load->library('migrate'); 81 | $this->migrate->setverbose(TRUE); // echo statments or not 82 | $this->migrate->version(id); // migrate the database to a particular version 83 | $this->migrate->install(); // install to the latest version. 84 | 85 | The dbmigreate.php controller just shows the use of these functions. If you are going to use it. 86 | remove the 'die();' and put it back in place when you are done. 87 | 88 | ** THE KICKER ** 89 | At the bottom of migreate.php we assume you are keeping track of your versions with a db table called config. Yes 90 | you can not use this migration script to create that table. You need at least one table installed to boot strap this. 91 | You will want to review the _update_schema_version() function to either modify or copy how it works. 92 | 93 | Also, in our applcations we have this code to set our applcation configs. We do not believe in writting to the file system 94 | that is what a database is for :). 95 | 96 | // 97 | // Setup Config. 98 | // 99 | function setup_system_config() 100 | { 101 | $query = $this->db->get('config'); 102 | foreach ($query->result() AS $row) 103 | $this->config->set_item($row->Config_Name, $row->Config_Data); 104 | } 105 | 106 | In this code we set. $this->CI->config->item('migrationversion'); and that is how _get_schema_version() 107 | gets the current version. This is the version this database is set too. 108 | 109 | ** Suggestion ** 110 | 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. 111 | 112 | // Make sure our database is up-to-date 113 | $this->migrate->setverbose(FALSE); 114 | if(! $this->migrate->version($this->config->item('migrations_version'))) 115 | show_error($this->migrate->error); 116 | 117 | 118 | Over all you should read this and http://codeigniter.com/wiki/Migrations to get an over all feel for this and make any changes 119 | you need for your applcation. 120 | 121 | 122 | Other Helpful Stuff: 123 | 124 | 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. 125 | 126 | When you auto load the system.php lib this should be your order: $autoload['libraries'] = array('database', 'migrate', 'system', 'session'); 127 | 128 | Here is a video walk through of migrations in action. 129 | 130 | http://www.vimeo.com/13490644 131 | 132 | -------------------------------------------------------------------------------- /dbmigrate.php: -------------------------------------------------------------------------------- 1 | load->library('migrate'); 10 | $this->migrate->setverbose(TRUE); 11 | 12 | /** 13 | /** VERY IMPORTANT - only turn this on when you need it. 14 | /** 15 | */ 16 | die(); 17 | } 18 | 19 | // 20 | // This will migrate up to the configed migration version 21 | // 22 | function configversion() 23 | { 24 | if(! $this->migrate->version($this->config->item('migrations_version'))) 25 | show_error($this->migrate->error); 26 | else 27 | echo "
Migration Successful
"; 28 | } 29 | 30 | // 31 | // Install up to the most up-to-date version. 32 | // 33 | function install() 34 | { 35 | if(! $this->migrate->install()) 36 | show_error($this->migrate->error); 37 | else 38 | echo "
Migration Successful
"; 39 | } 40 | 41 | // 42 | // Migrate to a particular version. 43 | // 44 | function version($id = NULL) 45 | { 46 | if(is_null($id)) 47 | show_error("Must pass in an id."); 48 | 49 | if(! $this->migrate->version($id)) 50 | show_error($this->migrate->error); 51 | else 52 | echo "
Migration Successful
"; 53 | } 54 | } 55 | ?> 56 | -------------------------------------------------------------------------------- /migrate.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudmanic/codeigniter-migrations/1fceebf1f3b543d75387f6944d54a7ca20c26be2/migrate.php -------------------------------------------------------------------------------- /migrations/001_Create_accounts.php: -------------------------------------------------------------------------------- 1 | migrate->verbose) 7 | echo "Creating table accounts..."; 8 | 9 | if(! $CI->db->table_exists('accounts')) { 10 | $cols = array( 11 | 'Id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 12 | 'Company_Name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 13 | 'First_Name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 14 | 'Last_Name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 15 | 'Phone' => array('type' => 'TEXT', 'null' => FALSE), 16 | 'Email' => array('type' => 'TEXT', 'null' => FALSE), 17 | 'Websites' => array('type' => 'TEXT', 'null' => FALSE), 18 | 'Address' => array('type' => 'TEXT', 'null' => FALSE), 19 | 'Last_Update' => array('type' => 'DATETIME', 'null' => FALSE) 20 | ); 21 | 22 | // Setup Keys 23 | $CI->dbforge->add_key('Id', TRUE); 24 | $CI->dbforge->add_field($cols); 25 | $CI->dbforge->add_field("Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); 26 | $CI->dbforge->create_table('accounts', TRUE); 27 | } 28 | } 29 | 30 | function down() 31 | { 32 | $CI =& get_instance(); 33 | if($CI->migrate->verbose) 34 | echo "Dropping table accounts..."; 35 | $CI->dbforge->drop_table('accounts'); 36 | } 37 | } 38 | 39 | ?> 40 | -------------------------------------------------------------------------------- /migrations/002_Create_leads.php: -------------------------------------------------------------------------------- 1 | migrate->verbose) 8 | echo "Creating table leads, lead_notes, lead_status..."; 9 | 10 | // Setup leads table 11 | if(! $CI->db->table_exists('leads')) { 12 | $leadstable = array( 13 | 'Leads_Id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 14 | 'Name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 15 | 'Contact_First_Name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 16 | 'Contact_Last_Name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 17 | 'Office_Phone' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 18 | 'Cell_Phone' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 19 | 'Contact_Email' => array('type' => 'VARCHAR', 'constraint' => '300', 'null' => FALSE), 20 | 'Fax' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 21 | 'Website' => array('type' => 'VARCHAR', 'constraint' => '300', 'null' => FALSE), 22 | 'Address1' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 23 | 'Address2' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 24 | 'City' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 25 | 'State' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 26 | 'Zip' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 27 | 'Site_City' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE), 28 | 'Lead_Status' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE), 29 | 'Lead_Owner' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE), 30 | 'Tags' => array('type' => 'TEXT', 'null' => FALSE) 31 | ); 32 | 33 | // Setup Keys 34 | $CI->dbforge->add_key('Leads_Id', TRUE); 35 | $CI->dbforge->add_key('Name'); 36 | $CI->dbforge->add_field($leadstable); 37 | $CI->dbforge->add_field("Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); 38 | $CI->dbforge->create_table('leads', TRUE); 39 | } 40 | 41 | 42 | // Setup Status 43 | if(! $CI->db->table_exists('lead_status')) { 44 | $status = array( 45 | 'Lead_Status_Id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 46 | 'Lead_Status_Name' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE) 47 | ); 48 | $CI->dbforge->add_field($status); 49 | $CI->dbforge->add_field("Lead_Status_Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); 50 | $CI->dbforge->add_key('Lead_Status_Id', TRUE); 51 | $CI->dbforge->create_table('lead_status', TRUE); 52 | $insert = array('Lead_Status_Name' => 'Not Reviewed'); 53 | $CI->db->insert('lead_status', $insert); 54 | $insert = array('Lead_Status_Name' => 'Contact Tag'); 55 | $CI->db->insert('lead_status', $insert); 56 | $insert = array('Lead_Status_Name' => 'Not Interested'); 57 | $CI->db->insert('lead_status', $insert); 58 | $insert = array('Lead_Status_Name' => 'Check Back Later'); 59 | $CI->db->insert('lead_status', $insert); 60 | $insert = array('Lead_Status_Name' => 'Pre-Sales'); 61 | $CI->db->insert('lead_status', $insert); 62 | $insert = array('Lead_Status_Name' => 'Lost'); 63 | $CI->db->insert('lead_status', $insert); 64 | $insert = array('Lead_Status_Name' => 'Junk Lead'); 65 | $CI->db->insert('lead_status', $insert); 66 | } 67 | 68 | // Setup Lead Notes 69 | if(! $CI->db->table_exists('lead_notes')) { 70 | $notes = array( 71 | 'Id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 72 | 'Lead_Id' => array('type' => 'INT', 'constraint' => 12, 'unsigned' => TRUE), 73 | 'Lead_Note' => array('type' => 'TEXT', 'null' => FALSE), 74 | 'Lead_Note_Title' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 75 | 'Lead_Note_Owner' => array('type' => 'INT', 'constraint' => 12, 'unsigned' => TRUE) 76 | ); 77 | $CI->dbforge->add_field($notes); 78 | $CI->dbforge->add_field("Lead_Note_Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); 79 | $CI->dbforge->add_key('Id', TRUE); 80 | $CI->dbforge->create_table('lead_notes', TRUE); 81 | } 82 | } 83 | 84 | function down() 85 | { 86 | $CI =& get_instance(); 87 | if($CI->migrate->verbose) 88 | echo "Dropping table leads, lead_notes, lead_status..."; 89 | $CI->dbforge->drop_table('leads'); 90 | $CI->dbforge->drop_table('lead_notes'); 91 | $CI->dbforge->drop_table('lead_status'); 92 | } 93 | } 94 | 95 | ?> 96 | -------------------------------------------------------------------------------- /migrations/003_Create_tags.php: -------------------------------------------------------------------------------- 1 | migrate->verbose) 8 | echo "Creating table tags..."; 9 | 10 | if(! $CI->db->table_exists('tags')) { 11 | $cols = array( 12 | 'Tags_Id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 13 | 'Tags_Name' => array('type' => 'VARCHAR', 'constraint' => '50', 'null' => FALSE) 14 | ); 15 | 16 | // Setup Keys 17 | $CI->dbforge->add_key('Tags_Id', TRUE); 18 | $CI->dbforge->add_key('Tags_Name'); 19 | $CI->dbforge->add_field($cols); 20 | $CI->dbforge->add_field("Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); 21 | $CI->dbforge->create_table('tags', TRUE); 22 | } 23 | } 24 | 25 | function down() 26 | { 27 | $CI =& get_instance(); 28 | if($CI->migrate->verbose) 29 | echo "Dropping table tags..."; 30 | $CI->dbforge->drop_table('tags'); 31 | } 32 | } 33 | 34 | ?> 35 | -------------------------------------------------------------------------------- /system.php: -------------------------------------------------------------------------------- 1 | CI =& get_instance(); 7 | $this->setup_system_config(); 8 | $this->set_profitloss(); 9 | 10 | // Make sure our database is up-to-date 11 | $this->CI->migrate->setverbose(FALSE); 12 | if(! $this->CI->migrate->version($this->CI->config->item('migrations_version'))) 13 | show_error($this->CI->migrate->error); 14 | } 15 | 16 | // 17 | // Setup Config. 18 | // 19 | function setup_system_config() 20 | { 21 | $this->setup_system_db(); 22 | $query = $this->CI->db->get('Config'); 23 | foreach ($query->result() AS $row) 24 | $this->CI->config->set_item($row->ConfigName, $row->ConfigData); 25 | return 1; 26 | } 27 | 28 | // 29 | // Setup the required tables. 30 | // 31 | function setup_system_db() 32 | { 33 | $this->CI->load->dbforge(); 34 | 35 | // Setup Sessions 36 | if(! $this->CI->db->table_exists('Sessions')) { 37 | $cols = array( 38 | 'session_id' => array('type' => 'VARCHAR', 'constraint' => '40', 'null' => FALSE, 'default' => 0), 39 | 'ip_address' => array('type' => 'VARCHAR', 'constraint' => '16', 'null' => FALSE, 'default' => 0), 40 | 'user_agent' => array('type' => 'VARCHAR', 'constraint' => '50', 'null' => FALSE), 41 | 'last_activity' => array('type' => 'INT', 'constraint' => '10', 'null' => FALSE), 42 | 'user_data' => array('type' => 'TEXT', 'null' => FALSE) 43 | ); 44 | $this->CI->dbforge->add_key('session_id', TRUE); 45 | $this->CI->dbforge->add_field($cols); 46 | $this->CI->dbforge->create_table('Sessions', TRUE); 47 | } 48 | 49 | // Setup Config table 50 | if(! $this->CI->db->table_exists('Config')) { 51 | $cols = array( 52 | 'ConfigId' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 53 | 'ConfigName' => array('type' => 'VARCHAR', 'constraint' => '25', 'null' => FALSE), 54 | 'ConfigData' => array('type' => 'TEXT', 'null' => FALSE), 55 | 'ConfigLastUpdate' => array('type' => 'DATETIME', 'null' => FALSE) 56 | ); 57 | 58 | // Setup Keys 59 | $this->CI->dbforge->add_key('ConfigId', TRUE); 60 | $this->CI->dbforge->add_field($cols); 61 | $this->CI->dbforge->add_field("ConfigCreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); 62 | $this->CI->dbforge->create_table('Config', TRUE); 63 | 64 | // Required to get boot strapped. 65 | $insert = array('ConfigName' => 'migrationversion', 'ConfigData' => '0'); 66 | $this->CI->db->insert('Config', $insert); 67 | } 68 | return 1; 69 | } 70 | } 71 | ?> --------------------------------------------------------------------------------