Manager
Manager
Relational Database Schema Manager.
Constants
MACRO_VARIABLE
const MACRO_VARIABLE = 1
MACRO_LOOKUP
const MACRO_LOOKUP = 2
Properties
schemaInfoTable
public string $schemaInfoTable
dbi
private Adapter $dbi
dbiConfig
private Map $dbiConfig
dbDir
private string $dbDir
migrateDir
private string $migrateDir
dataFile
private string $dataFile
migrationLog
private array $migrationLog
ignoreTables
private array $ignoreTables
versions
private array $versions
currentVersion
public int $currentVersion
appliedVersions
public array $appliedVersions
public $
tableMap
private array $tableMap
Methods
__construct
public __construct({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$dbiConfig | Map | |
$logCallback | \Closure |
getVersion
Returns the currently applied schema version.
public getVersion({Array})
getVersions
Returns a list of available schema versions.
public getVersions({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$returnFullPath | bool | |
$appliedOnly | bool |
getLatestVersion
Returns the version number of the latest schema version.
public getLatestVersion({Array})
isLatest
Boolean indicator for when the current schema version is the latest.
public isLatest({Array})
hasUpdates
Boolean indicator for when there are migrations that have not been applied.
public hasUpdates({Array})
getSchema
public getSchema({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$maxVersion | int | |
$ | false |
truncate
public truncate({Array})
snapshot
Snapshot the database schema and create a new schema version with migration replay files.
public snapshot({Array})
This method is used to create the database schema migration files. These files are used by the \Hazaar\Adapter::migrate() method to bring a database up to a certain version. Using this method simplifies creating these migration files and removes the need to create them manually when there are trivial changes.
When developing your project
Currently only the following changes are supported:
- Table creation, removal and rename.
- Column creation, removal and alteration.
- Index creation and removal.
!!! notice
Table rename detection works by comparing new tables with removed tables for tables that have the same columns. Because of this, rename detection will not work if columns are added or removed at the same time the table is renamed. If you want to rename a table, make sure that this is the only operation being performed on the table for a single snapshot. Modifying other tables will not affect this. If you want to rename a table AND change it's column layout, make sure you do either the rename or the modifications first, then snapshot, then do the other operation before snapshotting again.
Parameters
Parameter | Type | Description |
---|---|---|
$comment | string | a comment to add to the migration file |
$test | bool | Test only. Does not make any changes. |
$override_version | int | Manually specify the version number. Default is to use current timestamp. |
getMissingVersions
public getMissingVersions({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$version | int | |
$appliedVersions | array |
migrate
Database migration method.
public migrate({Array})
This method does some fancy database migration magic. It makes use of the 'db' subdirectory in the project directory which should contain the schema.json file. This file is the current database schema definition.
A few things can occur here.
If the database schema does not exist, then a new schema will be created using the schema.json schema definition file.
This will create the database at the latest version of the schema.
If the database schema already exists, then the current version is checked against the version requested using the
$version parameter. If no version is requested ($version is NULL) then the latest version number is used.
If the version numbers are different, then a migration will be performed.
# If the requested version is greater than the current version, the migration mode will be 'up'.
# If the requested version is less than the current version, the migration mode will be 'down'.
All migration files between the two selected versions (current and requested) will be replayed using the migration mode.
This process can be used to bring a database schema up to the latest version using database migration files stored in the db/migrate project subdirectory. These migration files are typically created using the \Hazaar\Adapter::snapshot() method although they can be created manually. Take care when using manually created migration files.
The migration is performed in a database transaction (if the database supports it) so that if anything goes wrong there is no damage to the database. If something goes wrong, errors will be availabl in the migration log accessible with \Hazaar\Adapter::getMigrationLog(). Errors in the migration files can be fixed and the migration retried.
Parameters
Parameter | Type | Description |
---|---|---|
$version | int | the database schema version to migrate to |
$forceDataSync | bool | |
$test | bool | |
$keepTables | bool | |
$force_reinitialise | bool |
migrateReplay
public migrateReplay({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$version | int | |
$test | bool |
rollback
Undo a migration version.
public rollback({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$version | int | |
$test | bool | |
$rollbacks | array |
applySchema
Takes a schema definition and creates it in the database.
public applySchema({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$schema | array | |
$test | bool | |
$keepTables | bool |
applySchemaFromFile
public applySchemaFromFile({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$filename | string |
syncData
public syncData({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$dataSchema | array | |
$test | bool | |
$forceDataSync | bool |
setLogCallback
Sets a callback function that can be used to process log messages.
public setLogCallback({Array})
The callback function provided must accept two arguments. $time and $msg.
Example, to simply echo formatted log data:
'''php $manager->setLogCallback(function($time, $msg){ echo date('Y-m-d H:i:s', (int)$time) . " - $msg\n"; }); '''
Parameters
Parameter | Type | Description |
---|---|---|
$callback | \Closure | The 'callable' callback function. See: is_callable(); |
getMigrationLog
Returns the migration log.
public getMigrationLog({Array})
Snapshots and migrations are complex processes where many things happen in a single execution. This means stuff can go wrong and you will probably want to know what/why when they do.
When running \Hazaar\Adapter::snapshot() or \Hazaar\Adapter::migrate() a log of what has been done is stored internally in an array of timestamped messages. You can use the \Hazaar\Adapter::getMigrationLog() method to retrieve this log so that if anything goes wrong, you can see what and fix it/
createRoleIfNotExists
public createRoleIfNotExists({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$roleOrRoles | string |
checkpoint
public checkpoint({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$version | int |
createInfoTable
Creates the info table that stores the version info of the current database.
private createInfoTable({Array})
getColumn
private getColumn({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$needle | string | |
$haystack | array | |
$key | string |
colExists
private colExists({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$needle | string | |
$haystack | array | |
$key | string |
getTableDiffs
private getTableDiffs({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$new | array | |
$old | array |
processContent
private processContent({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$version | int | |
$type | string | |
$item | array |
replay
Reply a database migration schema file.
private replay({Array})
This should only be used internally by the migrate method to replay an individual schema migration file.
Parameters
Parameter | Type | Description |
---|---|---|
$schema | array | The JSON decoded schema to replay |
$test | bool | |
$globalVars | \stdClass | |
$version | int |
replayItems
private replayItems({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$type | string | |
$action | string | |
$items | array | |
$test | bool | |
$primaryKeysFirst | bool | |
$version | int |
loadDataFromFile
private loadDataFromFile({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$dataSchema | array | |
$file | string | |
$childElement | string |
processMacro
private processMacro({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$field | string | |
$ | false |
prepareRow
private prepareRow({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$row | \stdClass | |
$records | array | |
$refs | \stdClass | |
$vars | \stdClass |
processDataObject
private processDataObject({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$info | \stdClass | |
$records | array | |
$globalVars | \stdClass |
fixRow
Quick closure function to fix up the row ready for insert/update.
private fixRow({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$row | \stdClass | |
$tableDef | mixed |
log
Logs a message to the migration log.
private log({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$msg | string | The message to log |
initDBIFilesystem
private initDBIFilesystem({Array})
Generated by Hazaar API Doc Generator