\Nano4\DB\UpdateSchema

Database Schema Update Management class.

Use this to build tools to automatically update your database schemas.

This class expects that you have a special database table that stores the current schema version of each table. Your initial table creation should inject the current version of the schema into the metadata table.

Summary

Methods
Properties
Constants
__construct()
listTables()
getTable()
updateAll()
tableExists()
$schemaDir
$updateDir
$updateFile
$metaTable
$metaName
$metaVer
$defVer
No constants found
No protected methods found
$db
$tables
N/A
No private methods found
No private properties found
N/A

Properties

$schemaDir

$schemaDir : 

The directory where schema files are found.

Type

$updateDir

$updateDir : 

The sub-directory of $schemaDir where update folders are found.

Type

$updateFile

$updateFile : 

The name of the update configuration file.

Type

$metaTable

$metaTable : 

The name of the schema metadata table.

Type

$metaName

$metaName : 

The name of the column containing the table name in the $metaTable.

Type

$metaVer

$metaVer : 

The name of the column containing the schema version in the $metaTable.

Type

$defVer

$defVer : 

The default version if no updates are found.

Type

$db

$db : 

Type

$tables

$tables : 

Type

Methods

__construct()

__construct(Object  $db, String  $schemaDir, Array  $opts = array()) 

Build a Nano4\DB\Update\Schema object.

Parameters

Object $db

The database object instance.

String $schemaDir

The directory with the schema files.

Array $opts

Any extra options.

The $db object must be a subclass of Nano4\DB\Simple object that includes the Nano4\DB\Simple\NativeDB trait.

The $schemaDir should have the current version of each table as $tablename.sql and have a folder called "updates" containing a folder for each table that has been updated. Inside, it should have a file called update.json which needs an array called "versions". Each member of the array can be either a floating point number, or an object. If it is a number, we assume a file called $oldver-$newver.sql exists, which will be called to perform the update. If it is an object, then it requires a property called "version" which should be a floating point number, and can also have the following optional properties:

"requires" An object where each property key is the name of a table and each property value is the version of that table that is required before we can perform the update. If the value is specified as true instead of a version number, then the version doesn't matter, and the mere existence of the table will suffice.

"pre-run" An array of ["filename", "function_name"] where the filename is a .php file in the relative folder, and the function name is a function to call after requirements have been fulfilled, but before the update SQL for this table has been called. The SQL file should use the UpdateSchema namespace, and the function is called function($db, $this) where $this is the UpdateSchema object instance.

"post-run" The same syntax as "pre-run", except if defined this is called after the update SQL for this table has been called.

"sql-file" If specified, it overrides the usual $oldver-$newver.sql filename. If set to false we skip the SQL step (only useful if the pre-run or post-run PHP scripts perform all the changes required.)

The $opts can override defaults, and specify custom behaviours.

"metadata_table" Override the name of the table containing the current schema version for every application table. Defaults to "schema_metadata".

"metadata_name" Override the name of the column containing the table name within the metadata table. Defaults to "name".

"metadata_version" Override the name of the column containing the schema version within the metadata table. Defaults to "version".

"updates_dir" The sub-folder of the schema folder that contains the list of updated tables. Defaults to "updates".

"update_file" The update config file for each table. Defaults to "update.json".

"default_version" The default version if no updates exist. Defaults to "1.0".

listTables()

listTables(Array  $opts = array()) : Array

Get a list of any tables with updates.

Parameters

Array $opts

Any options to pass to the Table class (optional.)

Returns

Array —

An array of Table objects.

getTable()

getTable(\Nano4\DB\Update\Str  $name, Array  $opts = array()) : \Nano4\DB\Update\Table

Get a Table object for the specified table.

Parameters

\Nano4\DB\Update\Str $name

The name of the table.

Array $opts

Any options to pass to the Table class (optional.)

Returns

\Nano4\DB\Update\Table

The table object.

updateAll()

updateAll(Array  $opts = array()) : Array

Update all tables that are out of date.

Parameters

Array $opts

Any options to pass to the Table class (optional.)

Returns

Array —

An array of Table objects.

tableExists()

tableExists(\Nano4\DB\Update\Str  $name) : Bool

See if a database table exists.

Parameters

\Nano4\DB\Update\Str $name

The name of the table to look for.

Returns

Bool —

Returns true if the table exists.