Class Migrator

Description

The Migrator class executes migration files - PHP files with MDB2 instructions for database schema changes.

With migrations you have a versioning scheme for database schema changes and you can always go back and forward to a known database schema - on all of your database servers (development, production and test). The currect schema version number is stored in a special table in your database.

Located in /migrator.php (line 28)


	
			
Class Constant Summary
Variable Summary
Method Summary
Migrator __construct (string $dsn,  $target_version, string $migrations_dir, Log $logger, int $target)
void checkDBResult (MDB2_Result $result)
void connect ()
void filterAndSortMigrationFiles (string $direction)
void log ( $message, [ $level = "debug"])
void migrate (string $direction)
int migrationFileNumber (string $filename)
void run ()
void writeNewMigrationVersion ( $newVersion)
boolean _filter_downgrade (string $filename)
boolean _filter_upgrade (string $filename)
Variables
MDB2_Driver $db (line 83)
  • access: protected
string $dsn (line 60)

Data Source Name

Example for a valid data source: mysql://my_user:my_pass@localhost/mydb

Log $logger (line 89)

PEAR_Log or similar object

  • access: protected
string $migrations_dir (line 50)

Directory where th migration files are stored

  • access: protected
array $migration_files (line 66)

Filenames, sorted by number prefix

  • access: protected
int $migration_max_version (line 72)

Highest number prefix in $migration_files

  • access: protected
string $schema_version_table = "schema_version" (line 44)

Table name where the schema versions are stored

  • access: public
int $target_version (line 78)

Version to up/downgrade to

  • access: protected
Methods
Constructor __construct (line 101)

Initialize the internal variables.

If you subclass Migrator, you must call this constructor!

Migrator __construct (string $dsn,  $target_version, string $migrations_dir, Log $logger, int $target)
  • string $dsn: Data Source Name
  • int $target: Version to up- or downgrade to (-1 for most recent version)
  • string $migrations_dir: Directory path where the the migration files are
  • Log $logger: PEAR_Log or similar object (must implement debug, error, notice and info methods)
  • $target_version
checkDBResult (line 346)

If the result given is an error, throw an exception.

  • access: protected
void checkDBResult (MDB2_Result $result)
  • MDB2_Result $result
collectMigrationFiles (line 154)

Collects migration file names from $migrations_dir and stores them in the sorted array $migration_files.

$migration_max_version is set to the biggest migration file prefix number.

  • access: protected
void collectMigrationFiles ()
connect (line 173)

Initializes $db with an instance of a MDB2 database driver.

The MDB2 "Manager" module is also loaded for the driver.

  • access: protected
void connect ()
filterAndSortMigrationFiles (line 230)

Leave only files in $this->migration_files that are needen for up/downgrade.

  • access: protected
void filterAndSortMigrationFiles (string $direction)
  • string $direction: "up" or "down"
getMigrationDirection (line 211)
  • access: protected
void getMigrationDirection ()
initializeCurrentSchemaVersion (line 189)

Initializes $current_version with version number from $schema_version_table.

If the table doesn't exist, initialize with 0 and create the table.

  • access: protected
void initializeCurrentSchemaVersion ()
log (line 353)
void log ( $message, [ $level = "debug"])
  • $message
  • $level
migrate (line 247)

Load each class in $migration_files and call its "up" or "down" method

  • access: protected
void migrate (string $direction)
  • string $direction: "up" or "down"
migrationFileNumber (line 309)

Return the migration file number at the start of a filename.

  • access: protected
int migrationFileNumber (string $filename)
  • string $filename
run (line 131)

The main function that does the migration.

Connects to the database, reads the current schema version, decides to up- or downgrade, loads the matching migrations, executes them and updates the schema version.

void run ()
writeNewMigrationVersion (line 296)

Write the new version number into {$schema_version_table}.

  • access: protected
void writeNewMigrationVersion ( $newVersion)
  • $newVersion
_filter_downgrade (line 335)

Callback function for http://www.php.net/array_filter that returns true for migration files between $target_version and $current_version.

  • access: protected
boolean _filter_downgrade (string $filename)
  • string $filename
_filter_upgrade (line 322)

Callback function for http://www.php.net/array_filter that returns true for migration files between $current_version and $target_version.

  • access: protected
boolean _filter_upgrade (string $filename)
  • string $filename
Class Constants
ERROR_CLASS_NOT_FOUND = 5 (line 37)
ERROR_DB_CONNECT_FAILED = 2 (line 34)
ERROR_DB_QUERY_FAILED = 3 (line 35)
ERROR_INVALID_FILE_NAME = 4 (line 36)
ERROR_MIGRATION_DIR_MISSING = 1 (line 33)
ERROR_NO_MIGRATION_FILES = 6 (line 38)
VERSION = "0.7" (line 30)

Documentation generated on Sat, 09 Jun 2007 11:50:10 +0200 by phpDocumentor 1.3.2