Managing Migrations - kulbakin/anahita GitHub Wiki
Anahita comes with command line interface (CLI) that among other things can help you setup your database and assist with deployments and migration management. To access the cli, open your Terminal and navigate to the directory of your Anahita installation.
To start, let's take a look at db:migrate:list. To use this command execute the below code
$ php anahita db:migrate:list
It will return something like this:
anahita version 3
topics version 1
Basically, this is reporting back to use the current version of database schema for both Anahita and all enabled components (Topics in the example above). In the event that one of them obsolete, it would say something similar to this:
anahita version 3
topics version 0 behind 1
We can then migrate the database to the new version by executing the following command:
$ php anahita db:migrate:up
This will automatically update all tables to the their most recent version. You can specific exactly which component(s) you'd like to update by listing them after the command
$ php anahita db:migrate:up topics photos pages
These commands can be executed over SSH providing you an easy way to setup your database on your remote server or migrate the database after an update.
Anahita can also help you manage the database for your own custom components. Let's say we'vee created a new app called com_hellos and we want to setup our database using the CLI. To get started, we excecute the following command:
$ php anahita db:migrate:new hellos
This will create a new file called 1.php in administrator/components/com_hellos/schemas/migrations folder.
/**
* Schema Migration
*
* @package Com_Hellos
* @subpackage Schema_Migration
*/
class ComHellosSchemaMigration1 extends ComMigratorMigrationVersion
{
/**
* Called when migrating up
*/
public function up()
{
//add your migration here
}
/**
* Called when rolling back a migration
*/
public function down()
{
//add your migration here
}
}
Within the up and down functions, you can add your sql statements. In the up function, you'll want to put all the commands that create or update your tables and in the down function, you'll want to provide code to undo the operation. That way in case something goes horribly awry, you can undo the migration with the following command:
$ php anahita db:migrate:rollback hellos
To create the table for com_hellos, edit the up function like below:
public function up()
{
dbexec('
CREATE TABLE `#__hellos` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`hello` VARCHAR( 255 ) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
');
}
Inversely, we can add the following code to the down function:
public function down()
{
dbexec('DROP TABLE `#__hellos');
}
Save the file and you can create the table like this:
$ php anahita db:migrate:up hellos
If you need to make modifications to your table, run the new command again. This will create a 2.php file in the migrations folder where you can add the necessary sql to modify your tables and migrate your data. You can then excecute it again with the up command.