Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Deployment

Tom Janssens edited this page Jan 14, 2015 · 11 revisions

Code

To deploy the application, go to {police_repository}/scripts/capistrano and execute:

For staging:

cap staging deploy

For production:

cap production deploy

Database migration

To have capistrano run the Phpmig migration execute:

cap staging deploy:migrations

This will have Phpmig loop through the /scripts/phpmig/migrations directory, and execute every migration which has not been applied.

Rollback

To rollback the latest deploy execute:

cap staging deploy:rollback

Note: Capistrano rollback doesn't rollback database automatically. To rollback database changes, SSH into the server and execute phpmig rollback:

cd capistrano/current/scripts/phpmig
phpmig rollback

Database migrations

We are using Phpmig to generate and execute database migrations. The applied migrations are being stored in the data database's& migrations table. Phpmig queries this table to check which migrations have been executed (up state) and which haven't (down state).

Create a new migration file

To create a new migration file, browse to the /scripts/phpmig directory and execute the generate command. Please note that we bundle the migration files per year! In order to generate a new migration file in to the correct subfolder, use the following command:

bin/phpmig generate AddTestTable ./migrations/date "+%Y"

This will generate a file like 20140428120659_AddTestTable.php.

By default, Phpmig generates files which are using the default Phpmig Migration class. To include multi-site support we must use our custom class!

You must change the first line of the script from:

use Phpmig\Migration\Migration;

to:

use MyPhpmig\Police\Migration;

You can create your up() and down() queries and store them in the $this->_queries property. Our custom Migration class will take care of the rest. So up() and down() should look something as follows:

public function up()
{
    $this->_queries = "ALTER TABLE `about` ADD `test` INT  NULL  DEFAULT NULL  AFTER `attachments_attachment_id`;";

    parent::up();
 }

Selecting databases

If you extend our custom Migration class, the given up() and down() queries will be applied to all zones. Our Migration class will fetch the list of zones from the data.police_zones table. To select only a part of these zones, you can setup restrictions in the init() method using a couple of different methods :

First, you can simply filter them based on certain columns. Example

public function init()
{
    $this->getZones()->where('twitter', '!=', '') // Select zones which have a Twitter account
    ->where('language', '=', 2, 'AND'); // and speak French.
  
    return parent::init();
}

You can also create a list manually - this will override any existing list. This is useful when you only need to migrate a few zones.

$this->getZones()->set(array('5388' => 'Leuven')); // Only apply this migration to Leuven.

You can also add custom databases to the list. Make sure to call this after you have setup the necessary column filters as this will first fetch the table rows and then append your new zone. This is useful to add "special" databases, for example the 'data' or 'demo' datase. Example:

$this->getZones()->where('language', '=', 1); // First select all the Dutch speaking zones.
$this->getZones()->append('demo', 'Demo Database'); // Now add the demo database.

Don't forget to test this on staging properly! Always take your time when dealing with database migrations.

Managing migrations

From inside your phpmig directory ({repo}/scripts/phpmig), you can quickly see which migration files have been applied by calling status:

bin/phpmig status

To apply or rollback a specific migration, find it's Migration ID (the first part of the filename) in the status output. For example, migration id is 20120614234716. Then execute:

bin/phpmig up 20120614234716

or

bin/phpmig down 20120614234716

To run all migrations which are currently down (eg. after a merge with master branch for example), just run:

bin/phpmig migrate
Clone this wiki locally