PHP Class MigrationShell

Inheritance: extends AppShell
Mostrar archivo Open project: cakedc/migrations

Public Properties

Property Type Description
$Version MigrationVersion MigrationVersion instance
$connection null | string Connection used for the migration_schema table of the migration versions
$migrationConnection null | string This can be used to override the connection of migration file
$path string Current path to load and save migrations
$skip array Skip a version or it can skip many version using comma as separate.
$type string Type of migration, can be 'app' or a plugin name

Protected Properties

Property Type Description
$_messages array Messages used to display action being performed

Public Methods

Method Description
afterMigration ( &$Migration, string $direction ) : void Callback used to create a new line after the migration
beforeAction ( &$Migration, string $type, array $data ) : void Callback used to display actions being performed
beforeMigration ( &$Migration, string $direction ) : void Callback used to display what migration is being runned
generate ( ) : void Generate a new migration file
getOptionParser ( ) : string Get the option parser.
main ( ) : void Override main
run ( ) : void Run the migrations
startup ( ) : void Override startup
status ( ) : void Displays a status of all plugin and app migrations

Protected Methods

Method Description
_connectionNamesEnum ( ) : array Get a list of connection names.
_execute ( array $options, boolean $once ) : boolean Execute migration
_fieldNamesArray ( array $fields ) : array Return list of field names from array of field/index definitions
_finalizeGeneratedMigration ( &$migration, &$migrationName, &$fromSchema ) : void Finalizes the generated migration - offers to preview it, prompts for a name, writes the file, and updates db version if needed.
_findSchemaFile ( Folder $folder, string $schemaFiles, string $name ) : mixed Finds schema file
_fromComparison ( array $migration, array $comparison, array $oldTables, array $currentTables ) : array Generate a migration string using comparison
_generateDump ( &$migration ) : void Generate a dump of the current database.
_generateFromCliArgs ( &$migration, &$migrationName, &$comparison ) : void Generate a migration from arguments passed in at the command line
_generateFromComparison ( &$migration, &$oldSchema, &$comparison ) : void Generate a migration by comparing schema.php with the database.
_generateFromInverseComparison ( &$migration, &$oldSchema, &$comparison ) : void Generate a migration by comparing the database with schema.php.
_generateMigration ( string $name, string $class, array $migration ) : string Generate a migration
_generateTemplate ( string $template, array $vars ) : string Include and generate a template string based on a template file
_getFieldType ( string $field, string $type, array $validTypes ) : string Return valid field type based on name of field
_getPath ( string $type = null ) : string Return the path used
_getSchema ( string $type = null ) : mixed Load and construct the schema class if exists
_getSchemaClassName ( string $name = null, boolean $suffix = true ) : string Gets the schema class name
_migrationChanges ( array $migration ) : boolean _migrationsChanges method
_outputLog ( array $log ) : void Output the SQL log in dry mode
_overwriteSchema ( ) : void Overwrite the schema.php file
_parseCommandLineFields ( string $name ) : array Parse fields from the command line for use with generating new migration files
_parseSingleCommandLineField ( &$fields, &$indexes, string $field, array $validTypes ) : void Parse a single argument from the command line into the fields array
_promptForMigrationName ( ) : string Prompt the user for a name for their new migration.
_promptVersionOptions ( array $mapping, string $latestVersion ) : array Output prompt with different migration versions to choose from
_readSchema ( ) : array Reads the schema data
_showInfo ( array $mapping, string $type = null ) : void Shows a list of available migrations
_singleStepOptions ( array $mapping, string $latestVersion, array $default = [] ) : array Single step options for up/down migrations
_startMigrationConnection ( ) : string Set a migration connection
_updateSchema ( ) : void Update the schema, making a call to schema shell
_values ( array $values ) : string Format a array/string into a one-line syntax
_writeMigration ( string $name, integer $version, array $migration ) : boolean Write a migration with given name

Method Details

_connectionNamesEnum() protected method

Get a list of connection names.
protected _connectionNamesEnum ( ) : array
return array The list of connection names

_execute() protected method

Execute migration
protected _execute ( array $options, boolean $once ) : boolean
$options array Options for migration
$once boolean True to only run once, false to retry
return boolean True if success

_fieldNamesArray() protected method

Return list of field names from array of field/index definitions
protected _fieldNamesArray ( array $fields ) : array
$fields array Field/index definitions
return array List of field names

_finalizeGeneratedMigration() protected method

Finalizes the generated migration - offers to preview it, prompts for a name, writes the file, and updates db version if needed.
protected _finalizeGeneratedMigration ( &$migration, &$migrationName, &$fromSchema ) : void
return void

_findSchemaFile() protected method

Finds schema file
protected _findSchemaFile ( Folder $folder, string $schemaFiles, string $name ) : mixed
$folder Folder Folder object with schema folder path.
$schemaFiles string Schema files inside schema folder.
$name string Schema-class name.
return mixed null in case of no file found, schema file.

_fromComparison() protected method

Generate a migration string using comparison
protected _fromComparison ( array $migration, array $comparison, array $oldTables, array $currentTables ) : array
$migration array Migration instructions array
$comparison array Result from CakeSchema::compare()
$oldTables array List of tables on schema.php file
$currentTables array List of current tables on database
return array

_generateDump() protected method

Generate a dump of the current database.
protected _generateDump ( &$migration ) : void
return void (The variables passed by reference are changed; nothing is returned)

_generateFromCliArgs() protected method

Generate a migration from arguments passed in at the command line
protected _generateFromCliArgs ( &$migration, &$migrationName, &$comparison ) : void
return void (The variables passed by reference are changed; nothing is returned)

_generateFromComparison() protected method

Generate a migration by comparing schema.php with the database.
protected _generateFromComparison ( &$migration, &$oldSchema, &$comparison ) : void
return void (The variables passed by reference are changed; nothing is returned)

_generateFromInverseComparison() protected method

Generate a migration by comparing the database with schema.php.
protected _generateFromInverseComparison ( &$migration, &$oldSchema, &$comparison ) : void
return void (The variables passed by reference are changed; nothing is returned)

_generateMigration() protected method

Generate a migration
protected _generateMigration ( string $name, string $class, array $migration ) : string
$name string Name of migration
$class string Class name of migration
$migration array Migration instructions array
return string

_generateTemplate() protected method

Include and generate a template string based on a template file
protected _generateTemplate ( string $template, array $vars ) : string
$template string Template file name
$vars array List of variables to be used on tempalte
return string

_getFieldType() protected method

Return valid field type based on name of field
protected _getFieldType ( string $field, string $type, array $validTypes ) : string
$field string Name of field
$type string Current type
$validTypes array List of valid types
return string Recognized type (eg. integer vs bigint)

_getPath() protected method

Return the path used
protected _getPath ( string $type = null ) : string
$type string Can be 'app' or a plugin name
return string Path used

_getSchema() protected method

Load and construct the schema class if exists
protected _getSchema ( string $type = null ) : mixed
$type string Can be 'app' or a plugin name
return mixed False in case of no file found, schema object

_getSchemaClassName() protected method

Gets the schema class name
protected _getSchemaClassName ( string $name = null, boolean $suffix = true ) : string
$name string Can be 'app' or a plugin name
$suffix boolean Return the class name with or without the "Schema" suffix, default is true
return string Returns the schema class name

_migrationChanges() protected method

_migrationsChanges method
protected _migrationChanges ( array $migration ) : boolean
$migration array list of migrations
return boolean

_outputLog() protected method

Output the SQL log in dry mode
protected _outputLog ( array $log ) : void
$log array List of queries per migration
return void

_overwriteSchema() protected method

Overwrite the schema.php file
protected _overwriteSchema ( ) : void
return void

_parseCommandLineFields() protected method

Parse fields from the command line for use with generating new migration files
protected _parseCommandLineFields ( string $name ) : array
$name string Name of migration
return array

_parseSingleCommandLineField() protected method

Parse a single argument from the command line into the fields array
protected _parseSingleCommandLineField ( &$fields, &$indexes, string $field, array $validTypes ) : void
$field string A single command line argument - eg. 'id:primary_key' or 'name:string'
$validTypes array Valid data types for the relevant database - eg. string, integer, biginteger, etc.
return void

_promptForMigrationName() protected method

Prompt the user for a name for their new migration.
protected _promptForMigrationName ( ) : string
return string

_promptVersionOptions() protected method

Output prompt with different migration versions to choose from
protected _promptVersionOptions ( array $mapping, string $latestVersion ) : array
$mapping array Migration version mappings
$latestVersion string Latest migration version
return array User-chosen options for migration

_readSchema() protected method

Reads the schema data
protected _readSchema ( ) : array
return array

_showInfo() protected method

Shows a list of available migrations
protected _showInfo ( array $mapping, string $type = null ) : void
$mapping array Migration mapping
$type string Can be 'app' or a plugin name
return void

_singleStepOptions() protected method

Single step options for up/down migrations
protected _singleStepOptions ( array $mapping, string $latestVersion, array $default = [] ) : array
$mapping array Migration version mappings
$latestVersion string Latest migration version
$default array Default options for migration
return array Modified options for migration

_startMigrationConnection() protected method

Set a migration connection
protected _startMigrationConnection ( ) : string
return string The name of the migration connection.

_updateSchema() protected method

Update the schema, making a call to schema shell
protected _updateSchema ( ) : void
return void

_values() protected method

Format a array/string into a one-line syntax
protected _values ( array $values ) : string
$values array Array to be converted
return string

_writeMigration() protected method

Write a migration with given name
protected _writeMigration ( string $name, integer $version, array $migration ) : boolean
$name string Name of migration
$version integer The version number (timestamp)
$migration array Migration instructions array
return boolean

afterMigration() public method

Callback used to create a new line after the migration
public afterMigration ( &$Migration, string $direction ) : void
$direction string Direction being runned
return void

beforeAction() public method

Callback used to display actions being performed
public beforeAction ( &$Migration, string $type, array $data ) : void
$type string Type of action. i.e: create_table, drop_table, etc.
$data array Data to send to the callback
return void

beforeMigration() public method

Additionally, shows the generation date of the migration, if the version is greater than '2000-01-01'.
public beforeMigration ( &$Migration, string $direction ) : void
$direction string Direction being runned
return void

generate() public method

Generate a new migration file
public generate ( ) : void
return void

getOptionParser() public method

Get the option parser.
public getOptionParser ( ) : string
return string

main() public method

Override main
public main ( ) : void
return void

run() public method

Run the migrations
public run ( ) : void
return void

startup() public method

Override startup
public startup ( ) : void
return void

status() public method

Displays a status of all plugin and app migrations
public status ( ) : void
return void

Property Details

$Version public_oe property

MigrationVersion instance
public MigrationVersion $Version
return MigrationVersion

$_messages protected_oe property

Messages used to display action being performed
protected array $_messages
return array

$connection public_oe property

Connection used for the migration_schema table of the migration versions
public null|string $connection
return null | string

$migrationConnection public_oe property

This can be used to override the connection of migration file
public null|string $migrationConnection
return null | string

$path public_oe property

Current path to load and save migrations
public string $path
return string

$skip public_oe property

Skip a version or it can skip many version using comma as separate.
public array $skip
return array

$type public_oe property

Type of migration, can be 'app' or a plugin name
public string $type
return string