This commit is contained in:
wangjinlei
2020-11-12 17:15:37 +08:00
parent 824380664c
commit 1abf99316f
893 changed files with 278997 additions and 0 deletions

View File

@@ -0,0 +1,181 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Migration
*/
namespace Phinx\Db\Adapter;
/**
* Adapter factory and registry.
*
* Used for registering adapters and creating instances of adapters.
*
* @author Woody Gilk <woody.gilk@gmail.com>
*/
class AdapterFactory
{
/**
* @var AdapterFactory
*/
protected static $instance;
/**
* Get the factory singleton instance.
*
* @return AdapterFactory
*/
public static function instance()
{
if (!static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Class map of database adapters, indexed by PDO::ATTR_DRIVER_NAME.
*
* @var array
*/
protected $adapters = array(
'mysql' => 'Phinx\Db\Adapter\MysqlAdapter',
'pgsql' => 'Phinx\Db\Adapter\PostgresAdapter',
'sqlite' => 'Phinx\Db\Adapter\SQLiteAdapter',
'sqlsrv' => 'Phinx\Db\Adapter\SqlServerAdapter',
);
/**
* Class map of adapters wrappers, indexed by name.
*
* @var array
*/
protected $wrappers = array(
'prefix' => 'Phinx\Db\Adapter\TablePrefixAdapter',
'proxy' => 'Phinx\Db\Adapter\ProxyAdapter',
);
/**
* Add or replace an adapter with a fully qualified class name.
*
* @throws \RuntimeException
* @param string $name
* @param string $class
* @return $this
*/
public function registerAdapter($name, $class)
{
if (!is_subclass_of($class, 'Phinx\Db\Adapter\AdapterInterface')) {
throw new \RuntimeException(sprintf(
'Adapter class "%s" must implement Phinx\\Db\\Adapter\\AdapterInterface',
$class
));
}
$this->adapters[$name] = $class;
return $this;
}
/**
* Get an adapter class by name.
*
* @throws \RuntimeException
* @param string $name
* @return string
*/
protected function getClass($name)
{
if (empty($this->adapters[$name])) {
throw new \RuntimeException(sprintf(
'Adapter "%s" has not been registered',
$name
));
}
return $this->adapters[$name];
}
/**
* Get an adapter instance by name.
*
* @param string $name
* @param array $options
* @return AdapterInterface
*/
public function getAdapter($name, array $options)
{
$class = $this->getClass($name);
return new $class($options);
}
/**
* Add or replace a wrapper with a fully qualified class name.
*
* @throws \RuntimeException
* @param string $name
* @param string $class
* @return $this
*/
public function registerWrapper($name, $class)
{
if (!is_subclass_of($class, 'Phinx\Db\Adapter\WrapperInterface')) {
throw new \RuntimeException(sprintf(
'Wrapper class "%s" must be implement Phinx\\Db\\Adapter\\WrapperInterface',
$class
));
}
$this->wrappers[$name] = $class;
return $this;
}
/**
* Get a wrapper class by name.
*
* @throws \RuntimeException
* @param string $name
* @return string
*/
protected function getWrapperClass($name)
{
if (empty($this->wrappers[$name])) {
throw new \RuntimeException(sprintf(
'Wrapper "%s" has not been registered',
$name
));
}
return $this->wrappers[$name];
}
/**
* Get a wrapper instance by name.
*
* @param string $name
* @param AdapterInterface $adapter
* @return AdapterInterface
*/
public function getWrapper($name, AdapterInterface $adapter)
{
$class = $this->getWrapperClass($name);
return new $class($adapter);
}
}

View File

@@ -0,0 +1,510 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db\Adapter
*/
namespace Phinx\Db\Adapter;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\Index;
use Phinx\Db\Table\ForeignKey;
use Phinx\Migration\MigrationInterface;
/**
* Adapter Interface.
*
* @author Rob Morgan <robbym@gmail.com>
*/
interface AdapterInterface
{
const PHINX_TYPE_STRING = 'string';
const PHINX_TYPE_CHAR = 'char';
const PHINX_TYPE_TEXT = 'text';
const PHINX_TYPE_INTEGER = 'integer';
const PHINX_TYPE_BIG_INTEGER = 'biginteger';
const PHINX_TYPE_FLOAT = 'float';
const PHINX_TYPE_DECIMAL = 'decimal';
const PHINX_TYPE_DATETIME = 'datetime';
const PHINX_TYPE_TIMESTAMP = 'timestamp';
const PHINX_TYPE_TIME = 'time';
const PHINX_TYPE_DATE = 'date';
const PHINX_TYPE_BINARY = 'binary';
const PHINX_TYPE_VARBINARY = 'varbinary';
const PHINX_TYPE_BLOB = 'blob';
const PHINX_TYPE_BOOLEAN = 'boolean';
const PHINX_TYPE_JSON = 'json';
const PHINX_TYPE_JSONB = 'jsonb';
const PHINX_TYPE_UUID = 'uuid';
const PHINX_TYPE_FILESTREAM = 'filestream';
// Geospatial database types
const PHINX_TYPE_GEOMETRY = 'geometry';
const PHINX_TYPE_POINT = 'point';
const PHINX_TYPE_LINESTRING = 'linestring';
const PHINX_TYPE_POLYGON = 'polygon';
// only for mysql so far
const PHINX_TYPE_ENUM = 'enum';
const PHINX_TYPE_SET = 'set';
/**
* Get all migrated version numbers.
*
* @return array
*/
public function getVersions();
/**
* Get all migration log entries, indexed by version number.
*
* @return array
*/
public function getVersionLog();
/**
* Set adapter configuration options.
*
* @param array $options
* @return AdapterInterface
*/
public function setOptions(array $options);
/**
* Get all adapter options.
*
* @return array
*/
public function getOptions();
/**
* Check if an option has been set.
*
* @param string $name
* @return boolean
*/
public function hasOption($name);
/**
* Get a single adapter option, or null if the option does not exist.
*
* @param string $name
* @return mixed
*/
public function getOption($name);
/**
* Sets the console input.
*
* @param InputInterface $input Input
* @return AdapterInterface
*/
public function setInput(InputInterface $input);
/**
* Gets the console input.
*
* @return InputInterface
*/
public function getInput();
/**
* Sets the console output.
*
* @param OutputInterface $output Output
* @return AdapterInterface
*/
public function setOutput(OutputInterface $output);
/**
* Gets the console output.
*
* @return OutputInterface
*/
public function getOutput();
/**
* Records a migration being run.
*
* @param MigrationInterface $migration Migration
* @param string $direction Direction
* @param int $startTime Start Time
* @param int $endTime End Time
* @return AdapterInterface
*/
public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime);
/**
* Toggle a migration breakpoint.
*
* @param MigrationInterface $migration
*
* @return AdapterInterface
*/
public function toggleBreakpoint(MigrationInterface $migration);
/**
* Reset all migration breakpoints.
*
* @return int The number of breakpoints reset
*/
public function resetAllBreakpoints();
/**
* Does the schema table exist?
*
* @deprecated use hasTable instead.
* @return boolean
*/
public function hasSchemaTable();
/**
* Creates the schema table.
*
* @return void
*/
public function createSchemaTable();
/**
* Returns the adapter type.
*
* @return string
*/
public function getAdapterType();
/**
* Initializes the database connection.
*
* @throws \RuntimeException When the requested database driver is not installed.
* @return void
*/
public function connect();
/**
* Closes the database connection.
*
* @return void
*/
public function disconnect();
/**
* Does the adapter support transactions?
*
* @return boolean
*/
public function hasTransactions();
/**
* Begin a transaction.
*
* @return void
*/
public function beginTransaction();
/**
* Commit a transaction.
*
* @return void
*/
public function commitTransaction();
/**
* Rollback a transaction.
*
* @return void
*/
public function rollbackTransaction();
/**
* Executes a SQL statement and returns the number of affected rows.
*
* @param string $sql SQL
* @return int
*/
public function execute($sql);
/**
* Executes a SQL statement and returns the result as an array.
*
* @param string $sql SQL
* @return array
*/
public function query($sql);
/**
* Executes a query and returns only one row as an array.
*
* @param string $sql SQL
* @return array
*/
public function fetchRow($sql);
/**
* Executes a query and returns an array of rows.
*
* @param string $sql SQL
* @return array
*/
public function fetchAll($sql);
/**
* Inserts data into a table.
*
* @param Table $table where to insert data
* @param array $row
* @return void
*/
public function insert(Table $table, $row);
/**
* Quotes a table name for use in a query.
*
* @param string $tableName Table Name
* @return string
*/
public function quoteTableName($tableName);
/**
* Quotes a column name for use in a query.
*
* @param string $columnName Table Name
* @return string
*/
public function quoteColumnName($columnName);
/**
* Checks to see if a table exists.
*
* @param string $tableName Table Name
* @return boolean
*/
public function hasTable($tableName);
/**
* Creates the specified database table.
*
* @param Table $table Table
* @return void
*/
public function createTable(Table $table);
/**
* Renames the specified database table.
*
* @param string $tableName Table Name
* @param string $newName New Name
* @return void
*/
public function renameTable($tableName, $newName);
/**
* Drops the specified database table.
*
* @param string $tableName Table Name
* @return void
*/
public function dropTable($tableName);
/**
* Returns table columns
*
* @param string $tableName Table Name
* @return Column[]
*/
public function getColumns($tableName);
/**
* Checks to see if a column exists.
*
* @param string $tableName Table Name
* @param string $columnName Column Name
* @return boolean
*/
public function hasColumn($tableName, $columnName);
/**
* Adds the specified column to a database table.
*
* @param Table $table Table
* @param Column $column Column
* @return void
*/
public function addColumn(Table $table, Column $column);
/**
* Renames the specified column.
*
* @param string $tableName Table Name
* @param string $columnName Column Name
* @param string $newColumnName New Column Name
* @return void
*/
public function renameColumn($tableName, $columnName, $newColumnName);
/**
* Change a table column type.
*
* @param string $tableName Table Name
* @param string $columnName Column Name
* @param Column $newColumn New Column
* @return Table
*/
public function changeColumn($tableName, $columnName, Column $newColumn);
/**
* Drops the specified column.
*
* @param string $tableName Table Name
* @param string $columnName Column Name
* @return void
*/
public function dropColumn($tableName, $columnName);
/**
* Checks to see if an index exists.
*
* @param string $tableName Table Name
* @param mixed $columns Column(s)
* @return boolean
*/
public function hasIndex($tableName, $columns);
/**
* Checks to see if an index specified by name exists.
*
* @param string $tableName Table Name
* @param string $indexName
* @return boolean
*/
public function hasIndexByName($tableName, $indexName);
/**
* Adds the specified index to a database table.
*
* @param Table $table Table
* @param Index $index Index
* @return void
*/
public function addIndex(Table $table, Index $index);
/**
* Drops the specified index from a database table.
*
* @param string $tableName
* @param mixed $columns Column(s)
* @return void
*/
public function dropIndex($tableName, $columns);
/**
* Drops the index specified by name from a database table.
*
* @param string $tableName
* @param string $indexName
* @return void
*/
public function dropIndexByName($tableName, $indexName);
/**
* Checks to see if a foreign key exists.
*
* @param string $tableName
* @param string[] $columns Column(s)
* @param string $constraint Constraint name
* @return boolean
*/
public function hasForeignKey($tableName, $columns, $constraint = null);
/**
* Adds the specified foreign key to a database table.
*
* @param Table $table
* @param ForeignKey $foreignKey
* @return void
*/
public function addForeignKey(Table $table, ForeignKey $foreignKey);
/**
* Drops the specified foreign key from a database table.
*
* @param string $tableName
* @param string[] $columns Column(s)
* @param string $constraint Constraint name
* @return void
*/
public function dropForeignKey($tableName, $columns, $constraint = null);
/**
* Returns an array of the supported Phinx column types.
*
* @return array
*/
public function getColumnTypes();
/**
* Checks that the given column is of a supported type.
*
* @param Column $column
* @return boolean
*/
public function isValidColumnType(Column $column);
/**
* Converts the Phinx logical type to the adapter's SQL type.
*
* @param string $type
* @param integer $limit
* @return string
*/
public function getSqlType($type, $limit = null);
/**
* Creates a new database.
*
* @param string $name Database Name
* @param array $options Options
* @return void
*/
public function createDatabase($name, $options = array());
/**
* Checks to see if a database exists.
*
* @param string $name Database Name
* @return boolean
*/
public function hasDatabase($name);
/**
* Drops the specified database.
*
* @param string $name Database Name
* @return void
*/
public function dropDatabase($name);
}

View File

@@ -0,0 +1,507 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db\Adapter
*/
namespace Phinx\Db\Adapter;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\Index;
use Phinx\Db\Table\ForeignKey;
use Phinx\Migration\MigrationInterface;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Adapter Wrapper.
*
* Proxy commands through to another adapter, allowing modification of
* parameters during calls.
*
* @author Woody Gilk <woody.gilk@gmail.com>
*/
abstract class AdapterWrapper implements AdapterInterface, WrapperInterface
{
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* {@inheritdoc}
*/
public function __construct(AdapterInterface $adapter)
{
$this->setAdapter($adapter);
}
/**
* {@inheritdoc}
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* {@inheritdoc}
*/
public function setOptions(array $options)
{
$this->adapter->setOptions($options);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOptions()
{
return $this->adapter->getOptions();
}
/**
* {@inheritdoc}
*/
public function hasOption($name)
{
return $this->adapter->hasOption($name);
}
/**
* {@inheritdoc}
*/
public function getOption($name)
{
return $this->adapter->getOption($name);
}
/**
* {@inheritdoc}
*/
public function setInput(InputInterface $input)
{
$this->adapter->setInput($input);
return $this;
}
/**
* {@inheritdoc}
*/
public function getInput()
{
return $this->adapter->getInput();
}
/**
* {@inheritdoc}
*/
public function setOutput(OutputInterface $output)
{
$this->adapter->setOutput($output);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOutput()
{
return $this->adapter->getOutput();
}
/**
* {@inheritdoc}
*/
public function connect()
{
return $this->getAdapter()->connect();
}
/**
* {@inheritdoc}
*/
public function disconnect()
{
return $this->getAdapter()->disconnect();
}
/**
* {@inheritdoc}
*/
public function execute($sql)
{
return $this->getAdapter()->execute($sql);
}
/**
* {@inheritdoc}
*/
public function query($sql)
{
return $this->getAdapter()->query($sql);
}
/**
* {@inheritdoc}
*/
public function insert(Table $table, $row)
{
return $this->getAdapter()->insert($table, $row);
}
/**
* {@inheritdoc}
*/
public function fetchRow($sql)
{
return $this->getAdapter()->fetchRow($sql);
}
/**
* {@inheritdoc}
*/
public function fetchAll($sql)
{
return $this->getAdapter()->fetchAll($sql);
}
/**
* {@inheritdoc}
*/
public function getVersions()
{
return $this->getAdapter()->getVersions();
}
/**
* {@inheritdoc}
*/
public function getVersionLog()
{
return $this->getAdapter()->getVersionLog();
}
/**
* {@inheritdoc}
*/
public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
{
$this->getAdapter()->migrated($migration, $direction, $startTime, $endTime);
return $this;
}
/**
* @inheritDoc
*/
public function toggleBreakpoint(MigrationInterface $migration)
{
$this->getAdapter()->toggleBreakpoint($migration);
return $this;
}
/**
* @inheritDoc
*/
public function resetAllBreakpoints()
{
return $this->getAdapter()->resetAllBreakpoints();
}
/**
* {@inheritdoc}
*/
public function hasSchemaTable()
{
return $this->getAdapter()->hasSchemaTable();
}
/**
* {@inheritdoc}
*/
public function createSchemaTable()
{
return $this->getAdapter()->createSchemaTable();
}
/**
* {@inheritdoc}
*/
public function getColumnTypes()
{
return $this->getAdapter()->getColumnTypes();
}
/**
* {@inheritdoc}
*/
public function isValidColumnType(Column $column)
{
return $this->getAdapter()->isValidColumnType($column);
}
/**
* {@inheritdoc}
*/
public function hasTransactions()
{
return $this->getAdapter()->hasTransactions();
}
/**
* {@inheritdoc}
*/
public function beginTransaction()
{
return $this->getAdapter()->beginTransaction();
}
/**
* {@inheritdoc}
*/
public function commitTransaction()
{
return $this->getAdapter()->commitTransaction();
}
/**
* {@inheritdoc}
*/
public function rollbackTransaction()
{
return $this->getAdapter()->rollbackTransaction();
}
/**
* {@inheritdoc}
*/
public function quoteTableName($tableName)
{
return $this->getAdapter()->quoteTableName($tableName);
}
/**
* {@inheritdoc}
*/
public function quoteColumnName($columnName)
{
return $this->getAdapter()->quoteColumnName($columnName);
}
/**
* {@inheritdoc}
*/
public function hasTable($tableName)
{
return $this->getAdapter()->hasTable($tableName);
}
/**
* {@inheritdoc}
*/
public function createTable(Table $table)
{
return $this->getAdapter()->createTable($table);
}
/**
* {@inheritdoc}
*/
public function renameTable($tableName, $newTableName)
{
return $this->getAdapter()->renameTable($tableName, $newTableName);
}
/**
* {@inheritdoc}
*/
public function dropTable($tableName)
{
return $this->getAdapter()->dropTable($tableName);
}
/**
* {@inheritdoc}
*/
public function getColumns($tableName)
{
return $this->getAdapter()->getColumns($tableName);
}
/**
* {@inheritdoc}
*/
public function hasColumn($tableName, $columnName)
{
return $this->getAdapter()->hasColumn($tableName, $columnName);
}
/**
* {@inheritdoc}
*/
public function addColumn(Table $table, Column $column)
{
return $this->getAdapter()->addColumn($table, $column);
}
/**
* {@inheritdoc}
*/
public function renameColumn($tableName, $columnName, $newColumnName)
{
return $this->getAdapter()->renameColumn($tableName, $columnName, $newColumnName);
}
/**
* {@inheritdoc}
*/
public function changeColumn($tableName, $columnName, Column $newColumn)
{
return $this->getAdapter()->changeColumn($tableName, $columnName, $newColumn);
}
/**
* {@inheritdoc}
*/
public function dropColumn($tableName, $columnName)
{
return $this->getAdapter()->dropColumn($tableName, $columnName);
}
/**
* {@inheritdoc}
*/
public function hasIndex($tableName, $columns)
{
return $this->getAdapter()->hasIndex($tableName, $columns);
}
/**
* {@inheritdoc}
*/
public function hasIndexByName($tableName, $indexName)
{
return $this->getAdapter()->hasIndexByName($tableName, $indexName);
}
/**
* {@inheritdoc}
*/
public function addIndex(Table $table, Index $index)
{
return $this->getAdapter()->addIndex($table, $index);
}
/**
* {@inheritdoc}
*/
public function dropIndex($tableName, $columns, $options = array())
{
return $this->getAdapter()->dropIndex($tableName, $columns, $options);
}
/**
* {@inheritdoc}
*/
public function dropIndexByName($tableName, $indexName)
{
return $this->getAdapter()->dropIndexByName($tableName, $indexName);
}
/**
* {@inheritdoc}
*/
public function hasForeignKey($tableName, $columns, $constraint = null)
{
return $this->getAdapter()->hasForeignKey($tableName, $columns, $constraint);
}
/**
* {@inheritdoc}
*/
public function addForeignKey(Table $table, ForeignKey $foreignKey)
{
return $this->getAdapter()->addForeignKey($table, $foreignKey);
}
/**
* {@inheritdoc}
*/
public function dropForeignKey($tableName, $columns, $constraint = null)
{
return $this->getAdapter()->dropForeignKey($tableName, $columns, $constraint);
}
/**
* {@inheritdoc}
*/
public function getSqlType($type, $limit = null)
{
return $this->getAdapter()->getSqlType($type, $limit);
}
/**
* {@inheritdoc}
*/
public function createDatabase($name, $options = array())
{
return $this->getAdapter()->createDatabase($name, $options);
}
/**
* {@inheritdoc}
*/
public function hasDatabase($name)
{
return $this->getAdapter()->hasDatabase($name);
}
/**
* {@inheritdoc}
*/
public function dropDatabase($name)
{
return $this->getAdapter()->dropDatabase($name);
}
/**
* @inheritDoc
*/
public function castToBool($value)
{
return $this->getAdapter()->castToBool($value);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,587 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db\Adapter
*/
namespace Phinx\Db\Adapter;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Migration\MigrationInterface;
/**
* Phinx PDO Adapter.
*
* @author Rob Morgan <robbym@gmail.com>
*/
abstract class PdoAdapter implements AdapterInterface
{
/**
* @var array
*/
protected $options = array();
/**
* @var InputInterface
*/
protected $input;
/**
* @var OutputInterface
*/
protected $output;
/**
* @var string
*/
protected $schemaTableName = 'migrations';
/**
* @var \PDO
*/
protected $connection;
/**
* @var float
*/
protected $commandStartTime;
/**
* Class Constructor.
*
* @param array $options Options
* @param InputInterface $input Input Interface
* @param OutputInterface $output Output Interface
*/
public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null)
{
$this->setOptions($options);
if (null !== $input) {
$this->setInput($input);
}
if (null !== $output) {
$this->setOutput($output);
}
}
/**
* {@inheritdoc}
*/
public function setOptions(array $options)
{
$this->options = $options;
if (isset($options['default_migration_table'])) {
$this->setSchemaTableName($options['default_migration_table']);
}
if (isset($options['connection'])) {
$this->setConnection($options['connection']);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getOptions()
{
return $this->options;
}
/**
* {@inheritdoc}
*/
public function hasOption($name)
{
return isset($this->options[$name]);
}
/**
* {@inheritdoc}
*/
public function getOption($name)
{
if (!$this->hasOption($name)) {
return;
}
return $this->options[$name];
}
/**
* {@inheritdoc}
*/
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
/**
* {@inheritdoc}
*/
public function getInput()
{
return $this->input;
}
/**
* {@inheritdoc}
*/
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
/**
* {@inheritdoc}
*/
public function getOutput()
{
if (null === $this->output) {
$output = new OutputInterface('nothing');
$this->setOutput($output);
}
return $this->output;
}
/**
* Sets the schema table name.
*
* @param string $schemaTableName Schema Table Name
* @return PdoAdapter
*/
public function setSchemaTableName($schemaTableName)
{
$this->schemaTableName = $schemaTableName;
return $this;
}
/**
* Gets the schema table name.
*
* @return string
*/
public function getSchemaTableName()
{
return $this->schemaTableName;
}
/**
* Sets the database connection.
*
* @param \PDO $connection Connection
* @return AdapterInterface
*/
public function setConnection(\PDO $connection)
{
$this->connection = $connection;
// Create the schema table if it doesn't already exist
if (!$this->hasSchemaTable()) {
$this->createSchemaTable();
} else {
$table = new Table($this->getSchemaTableName(), array(), $this);
if (!$table->hasColumn('migration_name')) {
$table
->addColumn('migration_name', 'string',
array('limit' => 100, 'after' => 'version', 'default' => null, 'null' => true)
)
->save();
}
if (!$table->hasColumn('breakpoint')) {
$table
->addColumn('breakpoint', 'boolean', array('default' => false))
->save();
}
}
return $this;
}
/**
* Gets the database connection
*
* @return \PDO
*/
public function getConnection()
{
if (null === $this->connection) {
$this->connect();
}
return $this->connection;
}
/**
* Sets the command start time
*
* @param int $time
* @return AdapterInterface
*/
public function setCommandStartTime($time)
{
$this->commandStartTime = $time;
return $this;
}
/**
* Gets the command start time
*
* @return int
*/
public function getCommandStartTime()
{
return $this->commandStartTime;
}
/**
* Start timing a command.
*
* @return void
*/
public function startCommandTimer()
{
$this->setCommandStartTime(microtime(true));
}
/**
* Stop timing the current command and write the elapsed time to the
* output.
*
* @return void
*/
public function endCommandTimer()
{
$end = microtime(true);
if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
$this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $this->getCommandStartTime()));
}
}
/**
* Write a Phinx command to the output.
*
* @param string $command Command Name
* @param array $args Command Args
* @return void
*/
public function writeCommand($command, $args = array())
{
if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
if (count($args)) {
$outArr = array();
foreach ($args as $arg) {
if (is_array($arg)) {
$arg = array_map(function ($value) {
return '\'' . $value . '\'';
}, $arg);
$outArr[] = '[' . implode(', ', $arg) . ']';
continue;
}
$outArr[] = '\'' . $arg . '\'';
}
$this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
return;
}
$this->getOutput()->writeln(' -- ' . $command);
}
}
/**
* {@inheritdoc}
*/
public function connect()
{
}
/**
* {@inheritdoc}
*/
public function disconnect()
{
}
/**
* {@inheritdoc}
*/
public function execute($sql)
{
return $this->getConnection()->exec($sql);
}
/**
* {@inheritdoc}
*/
public function query($sql)
{
return $this->getConnection()->query($sql);
}
/**
* {@inheritdoc}
*/
public function fetchRow($sql)
{
$result = $this->query($sql);
return $result->fetch();
}
/**
* {@inheritdoc}
*/
public function fetchAll($sql)
{
$rows = array();
$result = $this->query($sql);
while ($row = $result->fetch()) {
$rows[] = $row;
}
return $rows;
}
/**
* {@inheritdoc}
*/
public function insert(Table $table, $row)
{
$this->startCommandTimer();
$this->writeCommand('insert', array($table->getName()));
$sql = sprintf(
"INSERT INTO %s ",
$this->quoteTableName($table->getName())
);
$columns = array_keys($row);
$sql .= "(". implode(', ', array_map(array($this, 'quoteColumnName'), $columns)) . ")";
$sql .= " VALUES (" . implode(', ', array_fill(0, count($columns), '?')) . ")";
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($row));
$this->endCommandTimer();
}
/**
* {@inheritdoc}
*/
public function getVersions()
{
$rows = $this->getVersionLog();
return array_keys($rows);
}
/**
* {@inheritdoc}
*/
public function getVersionLog()
{
$result = array();
$rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY version ASC', $this->getSchemaTableName()));
foreach ($rows as $version) {
$result[$version['version']] = $version;
}
return $result;
}
/**
* {@inheritdoc}
*/
public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
{
if (strcasecmp($direction, MigrationInterface::UP) === 0) {
// up
$sql = sprintf(
"INSERT INTO %s (%s, %s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s', %s);",
$this->getSchemaTableName(),
$this->quoteColumnName('version'),
$this->quoteColumnName('migration_name'),
$this->quoteColumnName('start_time'),
$this->quoteColumnName('end_time'),
$this->quoteColumnName('breakpoint'),
$migration->getVersion(),
substr($migration->getName(), 0, 100),
$startTime,
$endTime,
$this->castToBool(false)
);
$this->query($sql);
} else {
// down
$sql = sprintf(
"DELETE FROM %s WHERE %s = '%s'",
$this->getSchemaTableName(),
$this->quoteColumnName('version'),
$migration->getVersion()
);
$this->query($sql);
}
return $this;
}
/**
* @inheritDoc
*/
public function toggleBreakpoint(MigrationInterface $migration)
{
$this->query(
sprintf(
'UPDATE %1$s SET %2$s = CASE %2$s WHEN %3$s THEN %4$s ELSE %3$s END WHERE %5$s = \'%6$s\';',
$this->getSchemaTableName(),
$this->quoteColumnName('breakpoint'),
$this->castToBool(true),
$this->castToBool(false),
$this->quoteColumnName('version'),
$migration->getVersion()
)
);
return $this;
}
/**
* @inheritDoc
*/
public function resetAllBreakpoints()
{
return $this->execute(
sprintf(
'UPDATE %1$s SET %2$s = %3$s WHERE %2$s <> %3$s;',
$this->getSchemaTableName(),
$this->quoteColumnName('breakpoint'),
$this->castToBool(false)
)
);
}
/**
* {@inheritdoc}
*/
public function hasSchemaTable()
{
return $this->hasTable($this->getSchemaTableName());
}
/**
* {@inheritdoc}
*/
public function createSchemaTable()
{
try {
$options = array(
'id' => false,
'primary_key' => 'version'
);
$table = new Table($this->getSchemaTableName(), $options, $this);
if ($this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql'
&& version_compare($this->getConnection()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.0', '>=')) {
$table->addColumn('version', 'biginteger', array('limit' => 14))
->addColumn('migration_name', 'string', array('limit' => 100, 'default' => null, 'null' => true))
->addColumn('start_time', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))
->addColumn('end_time', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))
->addColumn('breakpoint', 'boolean', array('default' => false))
->save();
} else {
$table->addColumn('version', 'biginteger')
->addColumn('migration_name', 'string', array('limit' => 100, 'default' => null, 'null' => true))
->addColumn('start_time', 'timestamp')
->addColumn('end_time', 'timestamp')
->addColumn('breakpoint', 'boolean', array('default' => false))
->save();
}
} catch (\Exception $exception) {
throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function getAdapterType()
{
return $this->getOption('adapter');
}
/**
* {@inheritdoc}
*/
public function getColumnTypes()
{
return array(
'string',
'char',
'text',
'integer',
'biginteger',
'float',
'decimal',
'datetime',
'timestamp',
'time',
'date',
'blob',
'binary',
'varbinary',
'boolean',
'uuid',
// Geospatial data types
'geometry',
'point',
'linestring',
'polygon',
);
}
/**
* {@inheritdoc}
*/
public function isValidColumnType(Column $column) {
return in_array($column->getType(), $this->getColumnTypes());
}
/**
* Cast a value to a boolean appropriate for the adapter.
*
* @param mixed $value The value to be cast
*
* @return mixed
*/
public function castToBool($value)
{
return (bool) $value ? 1 : 0;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,325 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db\Adapter
*/
namespace Phinx\Db\Adapter;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\Index;
use Phinx\Db\Table\ForeignKey;
use Phinx\Migration\IrreversibleMigrationException;
/**
* Phinx Proxy Adapter.
*
* Used for recording migration commands to automatically reverse them.
*
* @author Rob Morgan <robbym@gmail.com>
*/
class ProxyAdapter extends AdapterWrapper
{
/**
* @var array
*/
protected $commands;
/**
* {@inheritdoc}
*/
public function getAdapterType()
{
return 'ProxyAdapter';
}
/**
* {@inheritdoc}
*/
public function createTable(Table $table)
{
$this->recordCommand('createTable', array($table->getName()));
}
/**
* {@inheritdoc}
*/
public function renameTable($tableName, $newTableName)
{
$this->recordCommand('renameTable', array($tableName, $newTableName));
}
/**
* {@inheritdoc}
*/
public function dropTable($tableName)
{
$this->recordCommand('dropTable', array($tableName));
}
/**
* {@inheritdoc}
*/
public function addColumn(Table $table, Column $column)
{
$this->recordCommand('addColumn', array($table, $column));
}
/**
* {@inheritdoc}
*/
public function renameColumn($tableName, $columnName, $newColumnName)
{
$this->recordCommand('renameColumn', array($tableName, $columnName, $newColumnName));
}
/**
* {@inheritdoc}
*/
public function changeColumn($tableName, $columnName, Column $newColumn)
{
$this->recordCommand('changeColumn', array($tableName, $columnName, $newColumn));
}
/**
* {@inheritdoc}
*/
public function dropColumn($tableName, $columnName)
{
$this->recordCommand('dropColumn', array($tableName, $columnName));
}
/**
* {@inheritdoc}
*/
public function addIndex(Table $table, Index $index)
{
$this->recordCommand('addIndex', array($table, $index));
}
/**
* {@inheritdoc}
*/
public function dropIndex($tableName, $columns, $options = array())
{
$this->recordCommand('dropIndex', array($tableName, $columns, $options));
}
/**
* {@inheritdoc}
*/
public function dropIndexByName($tableName, $indexName)
{
$this->recordCommand('dropIndexByName', array($tableName, $indexName));
}
/**
* {@inheritdoc}
*/
public function addForeignKey(Table $table, ForeignKey $foreignKey)
{
$this->recordCommand('addForeignKey', array($table, $foreignKey));
}
/**
* {@inheritdoc}
*/
public function dropForeignKey($tableName, $columns, $constraint = null)
{
$this->recordCommand('dropForeignKey', array($columns, $constraint));
}
/**
* {@inheritdoc}
*/
public function createDatabase($name, $options = array())
{
$this->recordCommand('createDatabase', array($name, $options));
}
/**
* Record a command for execution later.
*
* @param string $name Command Name
* @param array $arguments Command Arguments
* @return void
*/
public function recordCommand($name, $arguments)
{
$this->commands[] = array(
'name' => $name,
'arguments' => $arguments
);
}
/**
* Sets an array of recorded commands.
*
* @param array $commands Commands
* @return ProxyAdapter
*/
public function setCommands($commands)
{
$this->commands = $commands;
return $this;
}
/**
* Gets an array of the recorded commands.
*
* @return array
*/
public function getCommands()
{
return $this->commands;
}
/**
* Gets an array of the recorded commands in reverse.
*
* @throws IrreversibleMigrationException if a command cannot be reversed.
* @return array
*/
public function getInvertedCommands()
{
if (null === $this->getCommands()) {
return array();
}
$invCommands = array();
$supportedCommands = array(
'createTable', 'renameTable', 'addColumn',
'renameColumn', 'addIndex', 'addForeignKey'
);
foreach (array_reverse($this->getCommands()) as $command) {
if (!in_array($command['name'], $supportedCommands)) {
throw new IrreversibleMigrationException(sprintf(
'Cannot reverse a "%s" command',
$command['name']
));
}
$invertMethod = 'invert' . ucfirst($command['name']);
$invertedCommand = $this->$invertMethod($command['arguments']);
$invCommands[] = array(
'name' => $invertedCommand['name'],
'arguments' => $invertedCommand['arguments']
);
}
return $invCommands;
}
/**
* Execute the recorded commands.
*
* @return void
*/
public function executeCommands()
{
$commands = $this->getCommands();
foreach ($commands as $command) {
call_user_func_array(array($this->getAdapter(), $command['name']), $command['arguments']);
}
}
/**
* Execute the recorded commands in reverse.
*
* @return void
*/
public function executeInvertedCommands()
{
$commands = $this->getInvertedCommands();
foreach ($commands as $command) {
call_user_func_array(array($this->getAdapter(), $command['name']), $command['arguments']);
}
}
/**
* Returns the reverse of a createTable command.
*
* @param array $args Method Arguments
* @return array
*/
public function invertCreateTable($args)
{
return array('name' => 'dropTable', 'arguments' => array($args[0]));
}
/**
* Returns the reverse of a renameTable command.
*
* @param array $args Method Arguments
* @return array
*/
public function invertRenameTable($args)
{
return array('name' => 'renameTable', 'arguments' => array($args[1], $args[0]));
}
/**
* Returns the reverse of a addColumn command.
*
* @param array $args Method Arguments
* @return array
*/
public function invertAddColumn($args)
{
return array('name' => 'dropColumn', 'arguments' => array($args[0]->getName(), $args[1]->getName()));
}
/**
* Returns the reverse of a renameColumn command.
*
* @param array $args Method Arguments
* @return array
*/
public function invertRenameColumn($args)
{
return array('name' => 'renameColumn', 'arguments' => array($args[0], $args[2], $args[1]));
}
/**
* Returns the reverse of a addIndex command.
*
* @param array $args Method Arguments
* @return array
*/
public function invertAddIndex($args)
{
return array('name' => 'dropIndex', 'arguments' => array($args[0]->getName(), $args[1]->getColumns()));
}
/**
* Returns the reverse of a addForeignKey command.
*
* @param array $args Method Arguments
* @return array
*/
public function invertAddForeignKey($args)
{
return array('name' => 'dropForeignKey', 'arguments' => array($args[0]->getName(), $args[1]->getColumns()));
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,272 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db\Adapter
*/
namespace Phinx\Db\Adapter;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\Index;
use Phinx\Db\Table\ForeignKey;
/**
* Table prefix/suffix adapter.
*
* Used for inserting a prefix or suffix into table names.
*
* @author Samuel Fisher <sam@sfisher.co>
*/
class TablePrefixAdapter extends AdapterWrapper
{
/**
* {@inheritdoc}
*/
public function getAdapterType()
{
return 'TablePrefixAdapter';
}
/**
* {@inheritdoc}
*/
public function hasTable($tableName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::hasTable($adapterTableName);
}
/**
* {@inheritdoc}
*/
public function createTable(Table $table)
{
$adapterTable = clone $table;
$adapterTableName = $this->getAdapterTableName($table->getName());
$adapterTable->setName($adapterTableName);
foreach ($adapterTable->getForeignKeys() as $fk) {
$adapterReferenceTable = $fk->getReferencedTable();
$adapterReferenceTableName = $this->getAdapterTableName($adapterReferenceTable->getName());
$adapterReferenceTable->setName($adapterReferenceTableName);
}
return parent::createTable($adapterTable);
}
/**
* {@inheritdoc}
*/
public function renameTable($tableName, $newTableName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
$adapterNewTableName = $this->getAdapterTableName($newTableName);
return parent::renameTable($adapterTableName, $adapterNewTableName);
}
/**
* {@inheritdoc}
*/
public function dropTable($tableName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::dropTable($adapterTableName);
}
/**
* {@inheritdoc}
*/
public function getColumns($tableName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::getColumns($adapterTableName);
}
/**
* {@inheritdoc}
*/
public function hasColumn($tableName, $columnName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::hasColumn($adapterTableName, $columnName);
}
/**
* {@inheritdoc}
*/
public function addColumn(Table $table, Column $column)
{
$adapterTable = clone $table;
$adapterTableName = $this->getAdapterTableName($table->getName());
$adapterTable->setName($adapterTableName);
return parent::addColumn($adapterTable, $column);
}
/**
* {@inheritdoc}
*/
public function renameColumn($tableName, $columnName, $newColumnName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::renameColumn($adapterTableName, $columnName, $newColumnName);
}
/**
* {@inheritdoc}
*/
public function changeColumn($tableName, $columnName, Column $newColumn)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::changeColumn($adapterTableName, $columnName, $newColumn);
}
/**
* {@inheritdoc}
*/
public function dropColumn($tableName, $columnName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::dropColumn($adapterTableName, $columnName);
}
/**
* {@inheritdoc}
*/
public function hasIndex($tableName, $columns)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::hasIndex($adapterTableName, $columns);
}
/**
* {@inheritdoc}
*/
public function hasIndexByName($tableName, $indexName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::hasIndexByName($adapterTableName, $indexName);
}
/**
* {@inheritdoc}
*/
public function addIndex(Table $table, Index $index)
{
$adapterTable = clone $table;
$adapterTableName = $this->getAdapterTableName($table->getName());
$adapterTable->setName($adapterTableName);
return parent::addIndex($adapterTable, $index);
}
/**
* {@inheritdoc}
*/
public function dropIndex($tableName, $columns, $options = array())
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::dropIndex($adapterTableName, $columns, $options);
}
/**
* {@inheritdoc}
*/
public function dropIndexByName($tableName, $indexName)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::dropIndexByName($adapterTableName, $indexName);
}
/**
* {@inheritdoc}
*/
public function hasForeignKey($tableName, $columns, $constraint = null)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::hasForeignKey($adapterTableName, $columns, $constraint);
}
/**
* {@inheritdoc}
*/
public function addForeignKey(Table $table, ForeignKey $foreignKey)
{
$adapterTable = clone $table;
$adapterTableName = $this->getAdapterTableName($table->getName());
$adapterTable->setName($adapterTableName);
return parent::addForeignKey($adapterTable, $foreignKey);
}
/**
* {@inheritdoc}
*/
public function dropForeignKey($tableName, $columns, $constraint = null)
{
$adapterTableName = $this->getAdapterTableName($tableName);
return parent::dropForeignKey($adapterTableName, $columns, $constraint);
}
/**
* {@inheritdoc}
*/
public function insert(Table $table, $row)
{
$adapterTable = clone $table;
$adapterTableName = $this->getAdapterTableName($table->getName());
$adapterTable->setName($adapterTableName);
return parent::insert($adapterTable, $row);
}
/**
* Gets the table prefix.
*
* @return string
*/
public function getPrefix()
{
return (string) $this->getOption('table_prefix');
}
/**
* Gets the table suffix.
*
* @return string
*/
public function getSuffix()
{
return (string) $this->getOption('table_suffix');
}
/**
* Applies the prefix and suffix to the table name.
*
* @param string $tableName
* @return string
*/
public function getAdapterTableName($tableName)
{
return $this->getPrefix() . $tableName . $this->getSuffix();
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db\Adapter
*/
namespace Phinx\Db\Adapter;
/**
* Wrapper Interface.
*
* @author Woody Gilk <woody.gilk@gmail.com>
*/
interface WrapperInterface
{
/**
* Class constructor, must always wrap another adapter.
*
* @param AdapterInterface $adapter
*/
public function __construct(AdapterInterface $adapter);
/**
* Sets the database adapter to proxy commands to.
*
* @param AdapterInterface $adapter
* @return AdapterInterface
*/
public function setAdapter(AdapterInterface $adapter);
/**
* Gets the database adapter.
*
* @throws \RuntimeException if the adapter has not been set
* @return AdapterInterface
*/
public function getAdapter();
}

View File

@@ -0,0 +1,674 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db
*/
namespace Phinx\Db;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\Index;
use Phinx\Db\Table\ForeignKey;
use Phinx\Db\Adapter\AdapterInterface;
/**
*
* This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
*/
class Table
{
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $options = array();
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* @var array
*/
protected $columns = array();
/**
* @var array
*/
protected $indexes = array();
/**
* @var ForeignKey[]
*/
protected $foreignKeys = array();
/**
* @var array
*/
protected $data = array();
/**
* Class Constuctor.
*
* @param string $name Table Name
* @param array $options Options
* @param AdapterInterface $adapter Database Adapter
*/
public function __construct($name, $options = array(), AdapterInterface $adapter = null)
{
$this->setName($name);
$this->setOptions($options);
if (null !== $adapter) {
$this->setAdapter($adapter);
}
}
/**
* Sets the table name.
*
* @param string $name Table Name
* @return Table
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Gets the table name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets the table options.
*
* @param array $options
* @return Table
*/
public function setOptions($options)
{
$this->options = $options;
return $this;
}
/**
* Gets the table options.
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Sets the database adapter.
*
* @param AdapterInterface $adapter Database Adapter
* @return Table
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* Gets the database adapter.
*
* @return AdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Does the table exist?
*
* @return boolean
*/
public function exists()
{
return $this->getAdapter()->hasTable($this->getName());
}
/**
* Drops the database table.
*
* @return void
*/
public function drop()
{
$this->getAdapter()->dropTable($this->getName());
}
/**
* Renames the database table.
*
* @param string $newTableName New Table Name
* @return Table
*/
public function rename($newTableName)
{
$this->getAdapter()->renameTable($this->getName(), $newTableName);
$this->setName($newTableName);
return $this;
}
/**
* Sets an array of columns waiting to be committed.
* Use setPendingColumns
*
* @deprecated
* @param array $columns Columns
* @return Table
*/
public function setColumns($columns)
{
$this->setPendingColumns($columns);
}
/**
* Gets an array of the table columns.
*
* @return Column[]
*/
public function getColumns()
{
return $this->getAdapter()->getColumns($this->getName());
}
/**
* Sets an array of columns waiting to be committed.
*
* @param array $columns Columns
* @return Table
*/
public function setPendingColumns($columns)
{
$this->columns = $columns;
return $this;
}
/**
* Gets an array of columns waiting to be committed.
*
* @return Column[]
*/
public function getPendingColumns()
{
return $this->columns;
}
/**
* Sets an array of columns waiting to be indexed.
*
* @param array $indexes Indexes
* @return Table
*/
public function setIndexes($indexes)
{
$this->indexes = $indexes;
return $this;
}
/**
* Gets an array of indexes waiting to be committed.
*
* @return array
*/
public function getIndexes()
{
return $this->indexes;
}
/**
* Sets an array of foreign keys waiting to be commited.
*
* @param ForeignKey[] $foreignKeys foreign keys
* @return Table
*/
public function setForeignKeys($foreignKeys)
{
$this->foreignKeys = $foreignKeys;
return $this;
}
/**
* Gets an array of foreign keys waiting to be commited.
*
* @return array|ForeignKey[]
*/
public function getForeignKeys()
{
return $this->foreignKeys;
}
/**
* Sets an array of data to be inserted.
*
* @param array $data Data
* @return Table
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Gets the data waiting to be inserted.
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Resets all of the pending table changes.
*
* @return void
*/
public function reset()
{
$this->setPendingColumns(array());
$this->setIndexes(array());
$this->setForeignKeys(array());
$this->setData(array());
}
/**
* Add a table column.
*
* Type can be: string, text, integer, float, decimal, datetime, timestamp,
* time, date, binary, boolean.
*
* Valid options can be: limit, default, null, precision or scale.
*
* @param string|Column $columnName Column Name
* @param string $type Column Type
* @param array $options Column Options
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @return Table
*/
public function addColumn($columnName, $type = null, $options = array())
{
// we need an adapter set to add a column
if (null === $this->getAdapter()) {
throw new \RuntimeException('An adapter must be specified to add a column.');
}
// create a new column object if only strings were supplied
if (!$columnName instanceof Column) {
$column = new Column();
$column->setName($columnName);
$column->setType($type);
$column->setOptions($options); // map options to column methods
} else {
$column = $columnName;
}
// Delegate to Adapters to check column type
if (!$this->getAdapter()->isValidColumnType($column)) {
throw new \InvalidArgumentException(sprintf(
'An invalid column type "%s" was specified for column "%s".',
$column->getType(),
$column->getName()
));
}
$this->columns[] = $column;
return $this;
}
/**
* Remove a table column.
*
* @param string $columnName Column Name
* @return Table
*/
public function removeColumn($columnName)
{
$this->getAdapter()->dropColumn($this->getName(), $columnName);
return $this;
}
/**
* Rename a table column.
*
* @param string $oldName Old Column Name
* @param string $newName New Column Name
* @return Table
*/
public function renameColumn($oldName, $newName)
{
$this->getAdapter()->renameColumn($this->getName(), $oldName, $newName);
return $this;
}
/**
* Change a table column type.
*
* @param string $columnName Column Name
* @param string|Column $newColumnType New Column Type
* @param array $options Options
* @return Table
*/
public function changeColumn($columnName, $newColumnType, $options = array())
{
// create a column object if one wasn't supplied
if (!$newColumnType instanceof Column) {
$newColumn = new Column();
$newColumn->setType($newColumnType);
$newColumn->setOptions($options);
} else {
$newColumn = $newColumnType;
}
// if the name was omitted use the existing column name
if (null === $newColumn->getName() || strlen($newColumn->getName()) === 0) {
$newColumn->setName($columnName);
}
$this->getAdapter()->changeColumn($this->getName(), $columnName, $newColumn);
return $this;
}
/**
* Checks to see if a column exists.
*
* @param string $columnName Column Name
* @param array $options Options
* @return boolean
*/
public function hasColumn($columnName, $options = array())
{
return $this->getAdapter()->hasColumn($this->getName(), $columnName, $options);
}
/**
* Add an index to a database table.
*
* In $options you can specific unique = true/false or name (index name).
*
* @param string|array|Index $columns Table Column(s)
* @param array $options Index Options
* @return Table
*/
public function addIndex($columns, $options = array())
{
// create a new index object if strings or an array of strings were supplied
if (!$columns instanceof Index) {
$index = new Index();
if (is_string($columns)) {
$columns = array($columns); // str to array
}
$index->setColumns($columns);
$index->setOptions($options);
} else {
$index = $columns;
}
$this->indexes[] = $index;
return $this;
}
/**
* Removes the given index from a table.
*
* @param array $columns Columns
* @param array $options Options
* @return Table
*/
public function removeIndex($columns, $options = array())
{
$this->getAdapter()->dropIndex($this->getName(), $columns, $options);
return $this;
}
/**
* Removes the given index identified by its name from a table.
*
* @param string $name Index name
* @return Table
*/
public function removeIndexByName($name)
{
$this->getAdapter()->dropIndexByName($this->getName(), $name);
return $this;
}
/**
* Checks to see if an index exists.
*
* @param string|array $columns Columns
* @param array $options Options
* @return boolean
*/
public function hasIndex($columns, $options = array())
{
return $this->getAdapter()->hasIndex($this->getName(), $columns, $options);
}
/**
* Add a foreign key to a database table.
*
* In $options you can specify on_delete|on_delete = cascade|no_action ..,
* on_update, constraint = constraint name.
*
* @param string|array $columns Columns
* @param string|Table $referencedTable Referenced Table
* @param string|array $referencedColumns Referenced Columns
* @param array $options Options
* @return Table
*/
public function addForeignKey($columns, $referencedTable, $referencedColumns = array('id'), $options = array())
{
if (is_string($referencedColumns)) {
$referencedColumns = array($referencedColumns); // str to array
}
$fk = new ForeignKey();
if ($referencedTable instanceof Table) {
$fk->setReferencedTable($referencedTable);
} else {
$fk->setReferencedTable(new Table($referencedTable, array(), $this->adapter));
}
$fk->setColumns($columns)
->setReferencedColumns($referencedColumns)
->setOptions($options);
$this->foreignKeys[] = $fk;
return $this;
}
/**
* Removes the given foreign key from the table.
*
* @param string|array $columns Column(s)
* @param null|string $constraint Constraint names
* @return Table
*/
public function dropForeignKey($columns, $constraint = null)
{
if (is_string($columns)) {
$columns = array($columns);
}
if ($constraint) {
$this->getAdapter()->dropForeignKey($this->getName(), array(), $constraint);
} else {
$this->getAdapter()->dropForeignKey($this->getName(), $columns);
}
return $this;
}
/**
* Checks to see if a foreign key exists.
*
* @param string|array $columns Column(s)
* @param null|string $constraint Constraint names
* @return boolean
*/
public function hasForeignKey($columns, $constraint = null)
{
return $this->getAdapter()->hasForeignKey($this->getName(), $columns, $constraint);
}
/**
* Add timestamp columns created_at and updated_at to the table.
*
* @param string $createdAtColumnName
* @param string $updatedAtColumnName
*
* @return Table
*/
public function addTimestamps($createdAtColumnName = 'created_at', $updatedAtColumnName = 'updated_at')
{
$createdAtColumnName = is_null($createdAtColumnName) ? 'created_at' : $createdAtColumnName;
$updatedAtColumnName = is_null($updatedAtColumnName) ? 'updated_at' : $updatedAtColumnName;
$this->addColumn($createdAtColumnName, 'timestamp', array(
'default' => 'CURRENT_TIMESTAMP',
'update' => ''
))
->addColumn($updatedAtColumnName, 'timestamp', array(
'null' => true,
'default' => null
));
return $this;
}
/**
* Insert data into the table.
*
* @param $data array of data in the form:
* array(
* array("col1" => "value1", "col2" => "anotherValue1"),
* array("col2" => "value2", "col2" => "anotherValue2"),
* )
* or array("col1" => "value1", "col2" => "anotherValue1")
*
* @return Table
*/
public function insert($data)
{
// handle array of array situations
if (isset($data[0]) && is_array($data[0])) {
foreach ($data as $row) {
$this->data[] = $row;
}
return $this;
}
$this->data[] = $data;
return $this;
}
/**
* Creates a table from the object instance.
*
* @return void
*/
public function create()
{
$this->getAdapter()->createTable($this);
$this->saveData();
$this->reset(); // reset pending changes
}
/**
* Updates a table from the object instance.
*
* @throws \RuntimeException
* @return void
*/
public function update()
{
if (!$this->exists()) {
throw new \RuntimeException('Cannot update a table that doesn\'t exist!');
}
// update table
foreach ($this->getPendingColumns() as $column) {
$this->getAdapter()->addColumn($this, $column);
}
foreach ($this->getIndexes() as $index) {
$this->getAdapter()->addIndex($this, $index);
}
foreach ($this->getForeignKeys() as $foreignKey) {
$this->getAdapter()->addForeignKey($this, $foreignKey);
}
$this->saveData();
$this->reset(); // reset pending changes
}
/**
* Commit the pending data waiting for insertion.
*
* @return void
*/
public function saveData()
{
foreach ($this->getData() as $row) {
$this->getAdapter()->insert($this, $row);
}
}
/**
* Commits the table changes.
*
* If the table doesn't exist it is created otherwise it is updated.
*
* @return void
*/
public function save()
{
if ($this->exists()) {
$this->update(); // update the table
} else {
$this->create(); // create the table
}
$this->reset(); // reset pending changes
}
}

View File

@@ -0,0 +1,550 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db
*/
namespace Phinx\Db\Table;
/**
*
* This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
*/
class Column
{
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $type;
/**
* @var integer
*/
protected $limit = null;
/**
* @var boolean
*/
protected $null = false;
/**
* @var mixed
*/
protected $default = null;
/**
* @var boolean
*/
protected $identity = false;
/**
* @var integer
*/
protected $precision;
/**
* @var integer
*/
protected $scale;
/**
* @var string
*/
protected $after;
/**
* @var string
*/
protected $update;
/**
* @var string
*/
protected $comment;
/**
* @var boolean
*/
protected $signed = true;
/**
* @var boolean
*/
protected $timezone = false;
/**
* @var array
*/
protected $properties = array();
/**
* @var array
*/
protected $values;
/**
* Sets the column name.
*
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Gets the column name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets the column type.
*
* @param string $type
* @return $this
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Gets the column type.
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Sets the column limit.
*
* @param integer $limit
* @return $this
*/
public function setLimit($limit)
{
$this->limit = $limit;
return $this;
}
/**
* Gets the column limit.
*
* @return integer
*/
public function getLimit()
{
return $this->limit;
}
/**
* Sets whether the column allows nulls.
*
* @param boolean $null
* @return $this
*/
public function setNull($null)
{
$this->null = (bool) $null;
return $this;
}
/**
* Gets whether the column allows nulls.
*
* @return boolean
*/
public function getNull()
{
return $this->null;
}
/**
* Does the column allow nulls?
*
* @return boolean
*/
public function isNull()
{
return $this->getNull();
}
/**
* Sets the default column value.
*
* @param mixed $default
* @return $this
*/
public function setDefault($default)
{
$this->default = $default;
return $this;
}
/**
* Gets the default column value.
*
* @return mixed
*/
public function getDefault()
{
return $this->default;
}
/**
* Sets whether or not the column is an identity column.
*
* @param boolean $identity
* @return $this
*/
public function setIdentity($identity)
{
$this->identity = $identity;
return $this;
}
/**
* Gets whether or not the column is an identity column.
*
* @return boolean
*/
public function getIdentity()
{
return $this->identity;
}
/**
* Is the column an identity column?
*
* @return boolean
*/
public function isIdentity()
{
return $this->getIdentity();
}
/**
* Sets the name of the column to add this column after.
*
* @param string $after After
* @return $this
*/
public function setAfter($after)
{
$this->after = $after;
return $this;
}
/**
* Returns the name of the column to add this column after.
*
* @return string
*/
public function getAfter()
{
return $this->after;
}
/**
* Sets the 'ON UPDATE' mysql column function.
*
* @param string $update On Update function
* @return $this
*/
public function setUpdate($update)
{
$this->update = $update;
return $this;
}
/**
* Returns the value of the ON UPDATE column function.
*
* @return string
*/
public function getUpdate()
{
return $this->update;
}
/**
* Sets the column precision for decimal.
*
* @param integer $precision
* @return $this
*/
public function setPrecision($precision)
{
$this->precision = $precision;
return $this;
}
/**
* Gets the column precision for decimal.
*
* @return integer
*/
public function getPrecision()
{
return $this->precision;
}
/**
* Sets the column scale for decimal.
*
* @param integer $scale
* @return $this
*/
public function setScale($scale)
{
$this->scale = $scale;
return $this;
}
/**
* Gets the column scale for decimal.
*
* @return integer
*/
public function getScale()
{
return $this->scale;
}
/**
* Sets the column comment.
*
* @param string $comment
* @return $this
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Gets the column comment.
*
* @return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Sets whether field should be signed.
*
* @param bool $signed
* @return $this
*/
public function setSigned($signed)
{
$this->signed = (bool) $signed;
return $this;
}
/**
* Gets whether field should be signed.
*
* @return string
*/
public function getSigned()
{
return $this->signed;
}
/**
* Should the column be signed?
*
* @return boolean
*/
public function isSigned()
{
return $this->getSigned();
}
/**
* Sets whether the field should have a timezone identifier.
* Used for date/time columns only!
*
* @param bool $timezone
* @return $this
*/
public function setTimezone($timezone)
{
$this->timezone = (bool) $timezone;
return $this;
}
/**
* Gets whether field has a timezone identifier.
*
* @return boolean
*/
public function getTimezone()
{
return $this->timezone;
}
/**
* Should the column have a timezone?
*
* @return boolean
*/
public function isTimezone()
{
return $this->getTimezone();
}
/**
* Sets field properties.
*
* @param array $properties
*
* @return $this
*/
public function setProperties($properties)
{
$this->properties = $properties;
return $this;
}
/**
* Gets field properties
*
* @return array
*/
public function getProperties()
{
return $this->properties;
}
/**
* Sets field values.
*
* @param mixed (array|string) $values
*
* @return $this
*/
public function setValues($values)
{
if (!is_array($values)) {
$values = preg_split('/,\s*/', $values);
}
$this->values = $values;
return $this;
}
/**
* Gets field values
*
* @return string
*/
public function getValues()
{
return $this->values;
}
/**
* Gets all allowed options. Each option must have a corresponding `setFoo` method.
*
* @return array
*/
protected function getValidOptions()
{
return array(
'limit',
'default',
'null',
'identity',
'precision',
'scale',
'after',
'update',
'comment',
'signed',
'timezone',
'properties',
'values',
);
}
/**
* Gets all aliased options. Each alias must reference a valid option.
*
* @return array
*/
protected function getAliasedOptions()
{
return array(
'length' => 'limit',
);
}
/**
* Utility method that maps an array of column options to this objects methods.
*
* @param array $options Options
* @return $this
*/
public function setOptions($options)
{
$validOptions = $this->getValidOptions();
$aliasOptions = $this->getAliasedOptions();
foreach ($options as $option => $value) {
if (isset($aliasOptions[$option])) {
// proxy alias -> option
$option = $aliasOptions[$option];
}
if (!in_array($option, $validOptions, true)) {
throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
}
$method = 'set' . ucfirst($option);
$this->$method($value);
}
return $this;
}
}

View File

@@ -0,0 +1,252 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db
* @author Leonid Kuzmin <lndkuzmin@gmail.com>
*/
namespace Phinx\Db\Table;
use Phinx\Db\Table;
class ForeignKey
{
const CASCADE = 'CASCADE';
const RESTRICT = 'RESTRICT';
const SET_NULL = 'SET NULL';
const NO_ACTION = 'NO ACTION';
/**
* @var array
*/
protected $columns = array();
/**
* @var Table
*/
protected $referencedTable;
/**
* @var array
*/
protected $referencedColumns = array();
/**
* @var string
*/
protected $onDelete;
/**
* @var string
*/
protected $onUpdate;
/**
* @var string|boolean
*/
protected $constraint;
/**
* Sets the foreign key columns.
*
* @param array|string $columns
* @return ForeignKey
*/
public function setColumns($columns)
{
if (is_string($columns)) {
$columns = array($columns);
}
$this->columns = $columns;
return $this;
}
/**
* Gets the foreign key columns.
*
* @return array
*/
public function getColumns()
{
return $this->columns;
}
/**
* Sets the foreign key referenced table.
*
* @param Table $table
* @return ForeignKey
*/
public function setReferencedTable(Table $table)
{
$this->referencedTable = $table;
return $this;
}
/**
* Gets the foreign key referenced table.
*
* @return Table
*/
public function getReferencedTable()
{
return $this->referencedTable;
}
/**
* Sets the foreign key referenced columns.
*
* @param array $referencedColumns
* @return ForeignKey
*/
public function setReferencedColumns(array $referencedColumns)
{
$this->referencedColumns = $referencedColumns;
return $this;
}
/**
* Gets the foreign key referenced columns.
*
* @return array
*/
public function getReferencedColumns()
{
return $this->referencedColumns;
}
/**
* Sets ON DELETE action for the foreign key.
*
* @param string $onDelete
* @return ForeignKey
*/
public function setOnDelete($onDelete)
{
$this->onDelete = $this->normalizeAction($onDelete);
return $this;
}
/**
* Gets ON DELETE action for the foreign key.
*
* @return string
*/
public function getOnDelete()
{
return $this->onDelete;
}
/**
* Gets ON UPDATE action for the foreign key.
*
* @return string
*/
public function getOnUpdate()
{
return $this->onUpdate;
}
/**
* Sets ON UPDATE action for the foreign key.
*
* @param string $onUpdate
* @return ForeignKey
*/
public function setOnUpdate($onUpdate)
{
$this->onUpdate = $this->normalizeAction($onUpdate);
return $this;
}
/**
* Sets constraint for the foreign key.
*
* @param string $constraint
* @return ForeignKey
*/
public function setConstraint($constraint)
{
$this->constraint = $constraint;
return $this;
}
/**
* Gets constraint name for the foreign key.
*
* @return string
*/
public function getConstraint()
{
return $this->constraint;
}
/**
* Utility method that maps an array of index options to this objects methods.
*
* @param array $options Options
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @return ForeignKey
*/
public function setOptions($options)
{
// Valid Options
$validOptions = array('delete', 'update', 'constraint');
foreach ($options as $option => $value) {
if (!in_array($option, $validOptions, true)) {
throw new \RuntimeException(sprintf('"%s" is not a valid foreign key option.', $option));
}
// handle $options['delete'] as $options['update']
if ('delete' === $option) {
$this->setOnDelete($value);
} elseif ('update' === $option) {
$this->setOnUpdate($value);
} else {
$method = 'set' . ucfirst($option);
$this->$method($value);
}
}
return $this;
}
/**
* From passed value checks if it's correct and fixes if needed
*
* @param string $action
* @throws \InvalidArgumentException
* @return string
*/
protected function normalizeAction($action)
{
$constantName = 'static::' . str_replace(' ', '_', strtoupper(trim($action)));
if (!defined($constantName)) {
throw new \InvalidArgumentException('Unknown action passed: ' . $action);
}
return constant($constantName);
}
}

View File

@@ -0,0 +1,185 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Db
*/
namespace Phinx\Db\Table;
class Index
{
/**
* @var string
*/
const UNIQUE = 'unique';
/**
* @var string
*/
const INDEX = 'index';
/**
* @var string
*/
const FULLTEXT = 'fulltext';
/**
* @var array
*/
protected $columns;
/**
* @var string
*/
protected $type = self::INDEX;
/**
* @var string
*/
protected $name = null;
/**
* @var integer
*/
protected $limit = null;
/**
* Sets the index columns.
*
* @param array $columns
* @return Index
*/
public function setColumns($columns)
{
$this->columns = $columns;
return $this;
}
/**
* Gets the index columns.
*
* @return array
*/
public function getColumns()
{
return $this->columns;
}
/**
* Sets the index type.
*
* @param string $type
* @return Index
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Gets the index type.
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Sets the index name.
*
* @param string $name
* @return Index
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Gets the index name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets the index limit.
*
* @param integer $limit
* @return Index
*/
public function setLimit($limit)
{
$this->limit = $limit;
return $this;
}
/**
* Gets the index limit.
*
* @return integer
*/
public function getLimit()
{
return $this->limit;
}
/**
* Utility method that maps an array of index options to this objects methods.
*
* @param array $options Options
* @throws \RuntimeException
* @return Index
*/
public function setOptions($options)
{
// Valid Options
$validOptions = array('type', 'unique', 'name', 'limit');
foreach ($options as $option => $value) {
if (!in_array($option, $validOptions, true)) {
throw new \RuntimeException(sprintf('"%s" is not a valid index option.', $option));
}
// handle $options['unique']
if (strcasecmp($option, self::UNIQUE) === 0) {
if ((bool) $value) {
$this->setType(self::UNIQUE);
}
continue;
}
$method = 'set' . ucfirst($option);
$this->$method($value);
}
return $this;
}
}

View File

@@ -0,0 +1,273 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Migration
*/
namespace Phinx\Migration;
use Phinx\Db\Table;
use Phinx\Db\Adapter\AdapterInterface;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Abstract Migration Class.
*
* It is expected that the migrations you write extend from this class.
*
* This abstract class proxies the various database methods to your specified
* adapter.
*
* @author Rob Morgan <robbym@gmail.com>
*/
abstract class AbstractMigration implements MigrationInterface
{
/**
* @var float
*/
protected $version;
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* @var OutputInterface
*/
protected $output;
/**
* @var InputInterface
*/
protected $input;
/**
* Class Constructor.
*
* @param int $version Migration Version
* @param InputInterface|null $input
* @param OutputInterface|null $output
*/
final public function __construct($version, InputInterface $input = null, OutputInterface $output = null)
{
$this->version = $version;
if (!is_null($input)){
$this->setInput($input);
}
if (!is_null($output)){
$this->setOutput($output);
}
$this->init();
}
/**
* Initialize method.
*
* @return void
*/
protected function init()
{
}
/**
* {@inheritdoc}
*/
public function up()
{
}
/**
* {@inheritdoc}
*/
public function down()
{
}
/**
* {@inheritdoc}
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* {@inheritdoc}
*/
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
/**
* {@inheritdoc}
*/
public function getInput()
{
return $this->input;
}
/**
* {@inheritdoc}
*/
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
/**
* {@inheritdoc}
*/
public function getOutput()
{
return $this->output;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return get_class($this);
}
/**
* {@inheritdoc}
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* {@inheritdoc}
*/
public function getVersion()
{
return $this->version;
}
/**
* {@inheritdoc}
*/
public function execute($sql)
{
return $this->getAdapter()->execute($sql);
}
/**
* {@inheritdoc}
*/
public function query($sql)
{
return $this->getAdapter()->query($sql);
}
/**
* {@inheritdoc}
*/
public function fetchRow($sql)
{
return $this->getAdapter()->fetchRow($sql);
}
/**
* {@inheritdoc}
*/
public function fetchAll($sql)
{
return $this->getAdapter()->fetchAll($sql);
}
/**
* {@inheritdoc}
*/
public function insert($table, $data)
{
// convert to table object
if (is_string($table)) {
$table = new Table($table, array(), $this->getAdapter());
}
return $table->insert($data)->save();
}
/**
* {@inheritdoc}
*/
public function createDatabase($name, $options)
{
$this->getAdapter()->createDatabase($name, $options);
}
/**
* {@inheritdoc}
*/
public function dropDatabase($name)
{
$this->getAdapter()->dropDatabase($name);
}
/**
* {@inheritdoc}
*/
public function hasTable($tableName)
{
return $this->getAdapter()->hasTable($tableName);
}
/**
* {@inheritdoc}
*/
public function table($tableName, $options = array())
{
return new Table($tableName, $options, $this->getAdapter());
}
/**
* A short-hand method to drop the given database table.
*
* @param string $tableName Table Name
* @return void
*/
public function dropTable($tableName)
{
$this->table($tableName)->drop();
}
}

View File

@@ -0,0 +1,97 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Migration
*/
namespace Phinx\Migration;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
abstract class AbstractTemplateCreation implements CreationInterface
{
/**
* @var InputInterface
*/
protected $input;
/**
* @var OutputInterface
*/
protected $output;
/**
* Class Constructor.
*
* @param InputInterface|null $input
* @param OutputInterface|null $output
*/
public function __construct(InputInterface $input = null, OutputInterface $output = null)
{
if (!is_null($input)) {
$this->setInput($input);
}
if (!is_null($output)) {
$this->setOutput($output);
}
}
/**
* {@inheritdoc}
*/
public function getInput()
{
return $this->input;
}
/**
* {@inheritdoc}
*/
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
/**
* {@inheritdoc}
*/
public function getOutput()
{
return $this->output;
}
/**
* {@inheritdoc}
*/
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Migration
*/
namespace Phinx\Migration;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Migration interface
*
* @author Richard Quadling <RQuadling@GMail.com>
*/
interface CreationInterface
{
/**
* CreationInterface constructor.
*
* @param InputInterface|null $input
* @param OutputInterface|null $output
*/
public function __construct(InputInterface $input = null, OutputInterface $output = null);
/**
* @param InputInterface $input
*
* @return CreationInterface
*/
public function setInput(InputInterface $input);
/**
* @param OutputInterface $output
*
* @return CreationInterface
*/
public function setOutput(OutputInterface $output);
/**
* @return InputInterface
*/
public function getInput();
/**
* @return OutputInterface
*/
public function getOutput();
/**
* Get the migration template.
*
* This will be the content that Phinx will amend to generate the migration file.
*
* @return string The content of the template for Phinx to amend.
*/
public function getMigrationTemplate();
/**
* Post Migration Creation.
*
* Once the migration file has been created, this method will be called, allowing any additional
* processing, specific to the template to be performed.
*
* @param string $migrationFilename The name of the newly created migration.
* @param string $className The class name.
* @param string $baseClassName The name of the base class.
* @return void
*/
public function postMigrationCreation($migrationFilename, $className, $baseClassName);
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Migration
*/
namespace Phinx\Migration;
/**
* Exception class thrown when migrations cannot be reversed using the 'change'
* feature.
*
* @author Rob Morgan <robbym@gmail.com>
*/
class IrreversibleMigrationException extends \Exception
{
}

View File

@@ -0,0 +1,32 @@
<?php
use $useClassName;
class $className extends $baseClassName
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
}
}

View File

@@ -0,0 +1,215 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Migration
*/
namespace Phinx\Migration;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Table;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Migration interface
*
* @author Rob Morgan <robbym@gmail.com>
*/
interface MigrationInterface
{
/**
* @var string
*/
const CHANGE = 'change';
/**
* @var string
*/
const UP = 'up';
/**
* @var string
*/
const DOWN = 'down';
/**
* Migrate Up
*
* @return void
*/
public function up();
/**
* Migrate Down
*
* @return void
*/
public function down();
/**
* Sets the database adapter.
*
* @param AdapterInterface $adapter Database Adapter
* @return MigrationInterface
*/
public function setAdapter(AdapterInterface $adapter);
/**
* Gets the database adapter.
*
* @return AdapterInterface
*/
public function getAdapter();
/**
* Sets the input object to be used in migration object
*
* @param InputInterface $input
* @return MigrationInterface
*/
public function setInput(InputInterface $input);
/**
* Gets the input object to be used in migration object
*
* @return InputInterface
*/
public function getInput();
/**
* Sets the output object to be used in migration object
*
* @param OutputInterface $output
* @return MigrationInterface
*/
public function setOutput(OutputInterface $output);
/**
* Gets the output object to be used in migration object
*
* @return OutputInterface
*/
public function getOutput();
/**
* Gets the name.
*
* @return string
*/
public function getName();
/**
* Sets the migration version number.
*
* @param float $version Version
* @return MigrationInterface
*/
public function setVersion($version);
/**
* Gets the migration version number.
*
* @return float
*/
public function getVersion();
/**
* Executes a SQL statement and returns the number of affected rows.
*
* @param string $sql SQL
* @return int
*/
public function execute($sql);
/**
* Executes a SQL statement and returns the result as an array.
*
* @param string $sql SQL
* @return array
*/
public function query($sql);
/**
* Executes a query and returns only one row as an array.
*
* @param string $sql SQL
* @return array
*/
public function fetchRow($sql);
/**
* Executes a query and returns an array of rows.
*
* @param string $sql SQL
* @return array
*/
public function fetchAll($sql);
/**
* Insert data into a table.
*
* @param string $tableName
* @param array $data
* @return void
*/
public function insert($tableName, $data);
/**
* Create a new database.
*
* @param string $name Database Name
* @param array $options Options
* @return void
*/
public function createDatabase($name, $options);
/**
* Drop a database.
*
* @param string $name Database Name
* @return void
*/
public function dropDatabase($name);
/**
* Checks to see if a table exists.
*
* @param string $tableName Table Name
* @return boolean
*/
public function hasTable($tableName);
/**
* Returns an instance of the <code>\Table</code> class.
*
* You can use this class to create and manipulate tables.
*
* @param string $tableName Table Name
* @param array $options Options
* @return Table
*/
public function table($tableName, $options);
}

View File

@@ -0,0 +1,215 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Seed
*/
namespace Phinx\Seed;
use Phinx\Db\Table;
use Phinx\Db\Adapter\AdapterInterface;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Abstract Seed Class.
*
* It is expected that the seeds you write extend from this class.
*
* This abstract class proxies the various database methods to your specified
* adapter.
*
* @author Rob Morgan <robbym@gmail.com>
*/
abstract class AbstractSeed implements SeedInterface
{
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* @var InputInterface
*/
protected $input;
/**
* @var OutputInterface
*/
protected $output;
/**
* Class Constructor.
*
* @param InputInterface $input
* @param OutputInterface $output
*/
final public function __construct(InputInterface $input = null, OutputInterface $output = null)
{
if (!is_null($input)){
$this->setInput($input);
}
if (!is_null($output)){
$this->setOutput($output);
}
$this->init();
}
/**
* Initialize method.
*
* @return void
*/
protected function init()
{
}
/**
* {@inheritdoc}
*/
public function run()
{
}
/**
* {@inheritdoc}
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* {@inheritdoc}
*/
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
/**
* {@inheritdoc}
*/
public function getInput()
{
return $this->input;
}
/**
* {@inheritdoc}
*/
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
/**
* {@inheritdoc}
*/
public function getOutput()
{
return $this->output;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return get_class($this);
}
/**
* {@inheritdoc}
*/
public function execute($sql)
{
return $this->getAdapter()->execute($sql);
}
/**
* {@inheritdoc}
*/
public function query($sql)
{
return $this->getAdapter()->query($sql);
}
/**
* {@inheritdoc}
*/
public function fetchRow($sql)
{
return $this->getAdapter()->fetchRow($sql);
}
/**
* {@inheritdoc}
*/
public function fetchAll($sql)
{
return $this->getAdapter()->fetchAll($sql);
}
/**
* {@inheritdoc}
*/
public function insert($table, $data)
{
// convert to table object
if (is_string($table)) {
$table = new Table($table, array(), $this->getAdapter());
}
return $table->insert($data)->save();
}
/**
* {@inheritdoc}
*/
public function hasTable($tableName)
{
return $this->getAdapter()->hasTable($tableName);
}
/**
* {@inheritdoc}
*/
public function table($tableName, $options = array())
{
return new Table($tableName, $options, $this->getAdapter());
}
}

View File

@@ -0,0 +1,19 @@
<?php
use $useClassName;
class $className extends $baseClassName
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
}
}

View File

@@ -0,0 +1,166 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Seed
*/
namespace Phinx\Seed;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Table;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Seed interface
*
* @author Rob Morgan <robbym@gmail.com>
*/
interface SeedInterface
{
/**
* @var string
*/
const RUN = 'run';
/**
* Run the seeder.
*
* @return void
*/
public function run();
/**
* Sets the database adapter.
*
* @param AdapterInterface $adapter Database Adapter
* @return MigrationInterface
*/
public function setAdapter(AdapterInterface $adapter);
/**
* Gets the database adapter.
*
* @return AdapterInterface
*/
public function getAdapter();
/**
* Sets the input object to be used in migration object
*
* @param InputInterface $input
* @return MigrationInterface
*/
public function setInput(InputInterface $input);
/**
* Gets the input object to be used in migration object
*
* @return InputInterface
*/
public function getInput();
/**
* Sets the output object to be used in migration object
*
* @param OutputInterface $output
* @return MigrationInterface
*/
public function setOutput(OutputInterface $output);
/**
* Gets the output object to be used in migration object
*
* @return OutputInterface
*/
public function getOutput();
/**
* Gets the name.
*
* @return string
*/
public function getName();
/**
* Executes a SQL statement and returns the number of affected rows.
*
* @param string $sql SQL
* @return int
*/
public function execute($sql);
/**
* Executes a SQL statement and returns the result as an array.
*
* @param string $sql SQL
* @return array
*/
public function query($sql);
/**
* Executes a query and returns only one row as an array.
*
* @param string $sql SQL
* @return array
*/
public function fetchRow($sql);
/**
* Executes a query and returns an array of rows.
*
* @param string $sql SQL
* @return array
*/
public function fetchAll($sql);
/**
* Insert data into a table.
*
* @param string $tableName
* @param array $data
* @return void
*/
public function insert($tableName, $data);
/**
* Checks to see if a table exists.
*
* @param string $tableName Table Name
* @return boolean
*/
public function hasTable($tableName);
/**
* Returns an instance of the <code>\Table</code> class.
*
* You can use this class to create and manipulate tables.
*
* @param string $tableName Table Name
* @param array $options Options
* @return Table
*/
public function table($tableName, $options);
}

View File

@@ -0,0 +1,190 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2015 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Util
*/
namespace Phinx\Util;
class Util
{
/**
* @var string
*/
const DATE_FORMAT = 'YmdHis';
/**
* @var string
*/
const MIGRATION_FILE_NAME_PATTERN = '/^\d+_([\w_]+).php$/i';
/**
* @var string
*/
const SEED_FILE_NAME_PATTERN = '/^([A-Z][a-z0-9]+).php$/i';
/**
* Gets the current timestamp string, in UTC.
*
* @return string
*/
public static function getCurrentTimestamp()
{
$dt = new \DateTime('now', new \DateTimeZone('UTC'));
return $dt->format(static::DATE_FORMAT);
}
/**
* Gets an array of all the existing migration class names.
*
* @return string
*/
public static function getExistingMigrationClassNames($path)
{
$classNames = array();
if (!is_dir($path)) {
return $classNames;
}
// filter the files to only get the ones that match our naming scheme
$phpFiles = glob($path . DIRECTORY_SEPARATOR . '*.php');
foreach ($phpFiles as $filePath) {
if (preg_match('/([0-9]+)_([_a-z0-9]*).php/', basename($filePath))) {
$classNames[] = static::mapFileNameToClassName(basename($filePath));
}
}
return $classNames;
}
/**
* Get the version from the beginning of a file name.
*
* @param string $fileName File Name
* @return string
*/
public static function getVersionFromFileName($fileName)
{
$matches = array();
preg_match('/^[0-9]+/', basename($fileName), $matches);
return $matches[0];
}
/**
* Turn migration names like 'CreateUserTable' into file names like
* '12345678901234_create_user_table.php' or 'LimitResourceNamesTo30Chars' into
* '12345678901234_limit_resource_names_to_30_chars.php'.
*
* @param string $className Class Name
* @return string
*/
public static function mapClassNameToFileName($className)
{
$arr = preg_split('/(?=[A-Z])/', $className);
unset($arr[0]); // remove the first element ('')
$fileName = static::getCurrentTimestamp() . '_' . strtolower(implode($arr, '_')) . '.php';
return $fileName;
}
/**
* Turn file names like '12345678901234_create_user_table.php' into class
* names like 'CreateUserTable'.
*
* @param string $fileName File Name
* @return string
*/
public static function mapFileNameToClassName($fileName)
{
$matches = array();
if (preg_match(static::MIGRATION_FILE_NAME_PATTERN, $fileName, $matches)) {
$fileName = $matches[1];
}
return str_replace(' ', '', ucwords(str_replace('_', ' ', $fileName)));
}
/**
* Check if a migration class name is unique regardless of the
* timestamp.
*
* This method takes a class name and a path to a migrations directory.
*
* Migration class names must be in CamelCase format.
* e.g: CreateUserTable or AddIndexToPostsTable.
*
* Single words are not allowed on their own.
*
* @param string $className Class Name
* @param string $path Path
* @return boolean
*/
public static function isUniqueMigrationClassName($className, $path)
{
$existingClassNames = static::getExistingMigrationClassNames($path);
return !(in_array($className, $existingClassNames));
}
/**
* Check if a migration/seed class name is valid.
*
* Migration & Seed class names must be in CamelCase format.
* e.g: CreateUserTable, AddIndexToPostsTable or UserSeeder.
*
* Single words are not allowed on their own.
*
* @param string $className Class Name
* @return boolean
*/
public static function isValidPhinxClassName($className)
{
return (bool) preg_match('/^([A-Z][a-z0-9]+)+$/', $className);
}
/**
* Check if a migration file name is valid.
*
* @param string $fileName File Name
* @return boolean
*/
public static function isValidMigrationFileName($fileName)
{
$matches = array();
return preg_match(static::MIGRATION_FILE_NAME_PATTERN, $fileName, $matches);
}
/**
* Check if a seed file name is valid.
*
* @param string $fileName File Name
* @return boolean
*/
public static function isValidSeedFileName($fileName)
{
$matches = array();
return preg_match(static::SEED_FILE_NAME_PATTERN, $fileName, $matches);
}
}