Friday, July 27, 2012

Ruckusing

Definitions
Database migrations - scripts to keep track of schema modifications.
Ruckusing - a database migration framework written in PHP5, for managing database migrations.

Why use database migration frameworks like ruckusing?  Again, of course because it's convenient:
  • Consistency - Schema consistency is very important.  With migrations, there is a guarantee that the project is in a consistent state across all developers and/or copies, that all developers are in sync - especially if the db is shared
  • Keeping track - You know what version your database is on, and the scripts will take care of keeping track of them for you.
  • Portability - You can easily move to a different RDBMS, because database communications are abstracted.
  • Migrating up and down - easily switch into earlier or later states of your schema
  • Tasks - you have a record of the modifications your schema underwent throughout the project

Here are simple (and hopefully short?) steps on using ruckusing:
  1. Configure the database: edit config/database.inc.php, and enter your database details (yes, you must have a database already).  For example,

        //----------------------------
        // DATABASE CONFIGURATION
        //----------------------------
        $ruckusing_db_config = array(
            'development' =>; array(
                'type'      =>; 'mysql',
                'host'      =>; 'localhost',
                'port'      =>; 3306,
                'database'  =>; 'myproject',
                'user'      =>; 'user',
                'password'  =>; 'password'
            )
        );


  2. Initialize schema migrations info table: in migrations/ (ruckusing top-level directory) run

        php main.php db:setup

    This will create the table schema_migrations in your database.  This table will keep track of what version your schema is currently on.

  3. Create a script: to generate a migration script, in migrations/ run

        php generate.php action_name

    This will generate a blank script in the migrations/db/migrate/ directory, with the filename format of <timestamp>_ActionName.php.  Ideally, you name your script according to the purpose it will serve; for example, the command

        php generate.php create_users

    will generate a blank script with filename <timestamp>_CreateUsers.php (i.e., 20110517061724_CreateUsers.php).  Though not required, it is better that you name your script after its purpose, so you can easily identify what that script does.

  4. Edit your script: open your generated script in the migrations/db/migrate/ directory.  You will see two functions:

    • public function up() - called when migrating up; edit this function to contain the schema changes you want to perform (building up).  Example:

          public function up() {
              $user = $this->;create_table("users");
              $user->;column("name", "string");
              $user->;column("email", "string", array('limit' =>; 100));
              $user->;finish();

              $this->;add_index("users", "email", array('unique' =>; true));
          } //up()


      or if you want, you can also use the execute() method, to execute direct SQL statements:

          public function up() {
              $sql = >>>SQL
                  CREATE TABLE users (
                      id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
                      name VARCHAR(255),
                      email VARCHAR(100),
                      UNIQUE(email)
          >>>;SQL;
              $this->;execute($sql);
          } //up()


    • public function down() - called when migrating down; to undo what the up() function does (tearing down).

          public function down() {
              $this->drop_table("users");
          } //down()

  5. Migrating: after modifying your scripts, in migrations/ run

        php main.php db:migrate

    to migrate to the latest version.  This command will execute all the scripts starting from the current version your schema is on up to the latest.  In our example, this command will run the create users script, and thus create the users table.

    Additionally, you can also choose to migrate to a specific version, both higher and lower.  You can do this in two ways:

    • by specifying the version number you want to migrate to.  For example, the command

          php main.php db:migrate VERSION=3

      will either a.) migrate from the current version of your schema up to the third migration, if your schema is on a lower version than 3, or b.) migrate down to the third migration, if your schema is on a higher version than 3.

    • by specifying the timestamp from the filename of the script; for example:

          php main.php db:migrate VERSION=20110517061724

      where 20110517061724 is the timestamp from the script 20110517061724_CreateUsers.php.  The behavior is the same as that of specifying the version number.

    So if you run

        php main.php db:migrate VERSION=0

    the framework will migrate all the way down to zero - all the down() functions of all the migration scripts are executed, and all the schema modifications that were done from the first to the latest version will be reverted.

  6. Other useful stuff: here are some of the other stuff you can do with ruckusing:
    • php main.php db:version - returns the current version of your schema
    • php main.php db:schema - dumps the current schema

Simple, right?  Much easier than managing all the sql files you've created for all your schema modifications, syncing databases accross machines, keeping track of what schema version you are on, etc.  For more details, visit http://code.google.com/p/ruckusing/ and https://github.com/ruckus/ruckusing-migrations.

:D

Thursday, July 26, 2012

SVN Switch

svn switch (sw) - update your working copy to a different URL.

Usage:
     svn switch <source> <dest>
or
     svn switch <source>
if the destination is your current location.

Examples:
Switching the entire working copy:
     svn switch http://examplesvn.com/svn/project/trunk
     svn sw http://examplesvn.com/svn/project/branches/branch_name

You can also choose to switch only a part/directory of your working copy:
     svn sw http://examplesvn.com/svn/project/branches/branch_name/subdir

Important!  When switching, always be in the directory that you want to switch; if you want to switch your entire working copy, then be in the top-level directory of your working copy.

Why switch?  It's convenient!  When working on multiple branches of your project, you don't need to checkout each branch to a different directory or location.  Also, since switch works like update, your local changes will also be preserved when you switch.

However, although this may be the case, I personally think it is better that you commit your local changes first before switching, because you'd want to avoid..

The S Status
Files with the S status (when you do a svn status) are from a different version - from a different location in your repository than the directory containing them.  This usually happens when svn switch gets interrupted in the middle, because it somehow gets confused from the local changes or conflicts.


As a final note, here are the things you'd want to think of first before switching:
  • svn info - check the URL your working copy is currently in
  • svn status (st) - check for local modifications, conflicts, or the S status
  • commit local changes first, to avoid conflicts and unexpected behaviors

Tuesday, July 24, 2012

Hello World!

Welcome to my nerdy blog!  Where I shall put all nerdy stuff I can think of and want to keep a record of.  Most will be computer- and programming-related, as that's what I'm currently doing for a living.

This blog is actually intended to be my own reference to the stuff I already know and/or have discovered, but keep on forgetting.  Hahaha.