-
Notifications
You must be signed in to change notification settings - Fork 1
Bonfire Migrations
Migrations are simple files that hold the commands to apply and remove changes to your database. This allows you and your team to easily keep track of changes made for each new module. They may create tables, modify tables or fields, etc. But they are not limited to just changing the schema. You could use them to fix bad data in the database or populate new fields.
While you could make the changes to the database by hand, migrations provide a simple, consistent way for developers to stay on track with each other's changes. It also makes it simple to apply the changes in your development environment to your production environment.
Using migration files also creates a version of your database that can be included in your current code versioning, whether you use git, svn, or another solution. While you might not have your data backed up in the case of a devastating loss, you can at least recreate your database and start over.
Migrations are contained in sequentially numbered files so the system knows the order to apply them or remove them.
Bonfire recognizes three distinct areas where migrations can be applied: Core, Application, and Module. All of the files are exactly the same structure, but are placed in different areas to allow you to keep your numbering systems separate.
Core migrations are files that are necessary for the database schema of Bonfire core. This keeps any Bonfire updates completely separate from your application's needs, and helps to remove any conflict between your own migration file numbers and Bonfire's.
These are stored under application/db/migrations/core.
For your own application, you should use application-level migrations. This is the perfect place for changes apply to application-specific changes. However, if you are planning on re-using modules from one application to the next, you should consider placing them at the module level.
These migration files are stored under application/db/migrations.
Each module can contain its own migrations, that can be applied completely separate of any application or core migrations. This allows for you to easily re-use your modules in other applications.
Module-level migrations are stored in modules/my_module/migrations.
Unlike the migrations that CodeIgniter provides, Bonfire’s migrations support two different types of migration files, allowing you to work the way that works best for you.
- dbForge migrations are the standard CodeIgniter migrations. They are completely self-contained and all code that is to be run for the migration to happen is contained within the up() and down() methods.
- SQL migrations are a unique addition to Bonfire. They allow you to use raw SQL commands instead of dbforge/dbutil commands that are sometimes unwieldy. SQL migrations are best used when you know that you will only be using MySQL databases and can customize your SQL commands to that database.
A clean install has migrations enabled by default. However, it is recommended when you move to production to disable migrations for security.
To disable migrations, edit the following line in application/core modules/migrations/config/migrations.php to be false.
$config['migrations_enabled'] = true;
A migration is a subclass of Migration that implements two methods: up (perform the required transformations) and down (revert them). Within each migration you can use any of the methods that CodeIgniter provides, like the dbutils and dbforge classes.
Migration files MUST be numbered sequentially. The rest of the file name is up to you, but it is recommended that the name describe what actually happens in the file. Like Install_initial_tables, Permissions_upgrade, etc. It must end with the .php extension.
001_Install_initial_tables.php
002_Version_02_upgrades.php
003_Permissions_upgrade.php
The file is a standard PHP class, that must follow three simple rules:
- The class must be named the same as the file, except the number is replaced by Migration. For a file named001_Install_initial_tables.php, the class would be named Migration_Install_initial_tables. The name is case-sensitive.
- The class MUST extend the Migration class
- The class MUST include two methods: up() and down(). As the names imply, the up() method is ran whenever you are migrating up to that version. The down() method is ran whenever uninstalling that migration.
class Migration_Install_initial_tables extends Migration {
function up() {
...
}
//--------------------------------------------------------------------
function down() {
...
}
//--------------------------------------------------------------------
}
When creating a SQL migration, there are two changes that you must make to the above skeleton.
- You must provide a class variable named $migration_type that is equal to sql.
- In both the up() and down() methods, you must return your SQL commands as a single string. This SQL may contain multiple commands, as long as they are separated by a colon.
class Migration_Install_initial_tables extends Migration {
public $migration_type = 'sql';
function up() {
$sql = "...";
return $sql;
}
//--------------------------------------------------------------------
function down() {
$sql = "...";
return $sql;
}
//--------------------------------------------------------------------
}
When writing raw SQL queries you must take into account the database prefix for your installation. This can be found with a small piece of code:
$prefix = $this->db->dbprefix;
In your queries you would use it like:
$sql = "ALTER TABLE `{$prefix}activities` ADD COLUMN `deleted` TINYINT(12) DEFAULT '0' NOT NULL AFTER `created_on`";
Migrations can be run, both up and down, in the Bonfire admin pages. You can find them under Database / Database Tools / Migrations.
Getting Started With Bonfire
Installing Bonfire
Change Log
Bonfire Models
Bonfire Migrations
Bonfire Controllers
Contexts
Layouts and Views
Working With Assets
Changing Admin URL
Global Helpers
Keyboard Shortcuts
Module Builder
System Events
Performance Tips
How to Contribute
API Documentation Guidelines
Bonfire Guides Guidelines
Bonfire Bug Report Guide