Introduction
Tws_SchemaManager is a PHP-based database installer and upgrade handler. Basic PHP classes can be used in CLI or web scripts to help handle database upgrades. The handlers call Changesets which handle the actual database modifications.
Overview
Handlers
Install Handlers are located in Tws/SchemaManager/<driver>.php. These just need the $_driver variable set with the driver name. All of the mainline code is stored in Tws/SchemaManager/Abstract.php file.
Available Methods
| Method | Description |
| getCurrentVersion | Returns the current version from the database |
| getDriver | Returns the driver name for the handler |
| getNamespace | Returns the file path for the Base and Delta Files |
| install | Calls the Base changeset and runs it |
| setNamespace | Sets the file path for Base and Delta files |
| upgrade | Calls the necessary Delta files for upgrading |
Changesets
Changesets handle the actual database modifications. They have just a simple upgrade method that can either contain embedded SQL statements or read in an SQL file. They can perform any work needed for the upgrade. Your Changesets extend Tws_SchemaManager_Changeset_Abstract and the class name is a single word.
Available Methods
| Method | Description |
| upgrade | Instructions for the upgrade |
Base Files
These files contain the version 1 of your database. They live in the Base folder and are named by adapter:
library\
MyApp\
SchemaManager\
Base\
Sqlite.phpclass Sqlite extends Tws_SchemaManager_Changeset_Abstract
{
public function upgrade() {
// Instructions for the upgrade
}
}Delta Files
Delta files work the same way but live in a different folder (the Delta folder) and have a corresponding number for each upgrade. The file must be <delta number>-<simple name>.php to be parsed correctly. The Simple name for each delta must also be unique.
library\
MyApp\
SchemaManager\
Delta\
002-UpgradeUsers.php
003-UpgradeStatus.phpclass UpgradeUsers extends Tws_SchemaManager_Changeset_Abstract
{
public function upgrade() {
// Instructions for the upgrade
}
}Installation
Installation of Tws_SchemaManager requires some changes to your application. You will need to do the following
- Add Tws_SchemaManager to your library
- Add a path for Base and Delta files in your library, ex:
- MyApp_SchemaManager_Base
- MyApp_SchemaManager_Delta
- Create a 'config' table with a Key and Value column
- Populate a 'version' key with a value of 1
- Create the necessary PHP files (see Usage)
Usage
Everything revolves around Changesets. At a high level, all you do is get a handler and either call install or upgrade. Place the code for upgrading or installing inside your install or upgrade routine in your script.
// Create a Zend_Db_Adapter_Abstract model, either through Bootstrap or manually
$handler = Tws_SchemaManager::factory($db);
$handler->setNamespace('MyApp_SchemaManager');
$handler->install();