/** * Set all sequence's current value to the lowest available field value. * * @return bool */ public function sequences() { $this->out(sprintf('%s - %s', date('H:i:s'), 'Set all sequence\'s current values')); $success = $this->connection->begin() !== false; $schema = Hash::get($this->connection->config(), 'schema') ?: 'public'; $conditions = ["table_schema = '{$schema}'"]; foreach ($this->connection->driver()->sequences($conditions) as $sequence) { $sequence['sequence'] = preg_replace('/^nextval\\(\'(.*)\'.*\\)$/', '\\1', $sequence['sequence']); $sql = "SELECT setval('{$sequence['sequence']}', COALESCE(MAX({$sequence['column']}),0)+1, false) FROM {$sequence['table']};"; $success = $success && $this->connection->query($sql)->fetchAll('assoc') !== false; } if ($success) { $success = $this->connection->commit() !== false && $success; } else { $success = $this->connection->rollback() !== false && $success; } if ($this->command === __FUNCTION__) { $this->_stop($success ? self::SUCCESS : self::ERROR); } return $success; }
/** * setup method * * @return void */ public function setUp() { parent::setUp(); $this->Connection = ConnectionManager::get('test'); $connectionConfig = $this->Connection->config(); $this->Connection->execute('DROP TABLE IF EXISTS phinxlog'); $this->config = new Config(['paths' => ['migrations' => __FILE__], 'environments' => ['default_migration_table' => 'phinxlog', 'default_database' => 'cakephp_test', 'default' => ['adapter' => getenv('DB'), 'host' => '127.0.0.1', 'name' => !empty($connectionConfig['database']) ? $connectionConfig['database'] : '', 'user' => !empty($connectionConfig['username']) ? $connectionConfig['username'] : '', 'pass' => !empty($connectionConfig['password']) ? $connectionConfig['password'] : '']]]); $application = new MigrationsDispatcher('testing'); $output = new StreamOutput(fopen('php://memory', 'a', false)); $this->command = $application->find('mark_migrated'); $Environment = new Environment('default', $this->config['environments']['default']); $Manager = $this->getMock('\\Phinx\\Migration\\Manager', [], [$this->config, $output]); $Manager->expects($this->any())->method('getEnvironment')->will($this->returnValue($Environment)); $this->command->setManager($Manager); }
/** * Get the column metadata for a table. * * Caching will be applied if `cacheMetadata` key is present in the Connection * configuration options. Defaults to _cake_model_ when true. * * ### Options * * - `forceRefresh` - Set to true to force rebuilding the cached metadata. * Defaults to false. * * @param string $name The name of the table to describe. * @param array $options The options to use, see above. * @return \Cake\Database\Schema\Table Object with column metadata. * @throws \Cake\Database\Exception when table cannot be described. */ public function describe($name, array $options = []) { $config = $this->_connection->config(); if (strpos($name, '.')) { list($config['schema'], $name) = explode('.', $name); } $table = new Table($name); $this->_reflect('Column', $name, $config, $table); if (count($table->columns()) === 0) { throw new Exception(sprintf('Cannot describe %s. It has 0 columns.', $name)); } $this->_reflect('Index', $name, $config, $table); $this->_reflect('ForeignKey', $name, $config, $table); $this->_reflect('Options', $name, $config, $table); return $table; }
/** * Get the column metadata for a table. * * Caching will be applied if `cacheMetadata` key is present in the Connection * configuration options. Defaults to _cake_model_ when true. * * @param string $name The name of the table to describe. * @return \Cake\Database\Schema\Table Object with column metadata. * @throws \Cake\Database\Exception when table cannot be described. */ public function describe($name) { $cacheConfig = $this->cacheMetadata(); if ($cacheConfig) { $cacheKey = $this->_connection->configName() . '_' . $name; $cached = Cache::read($cacheKey, $cacheConfig); if ($cached !== false) { return $cached; } } $config = $this->_connection->config(); list($sql, $params) = $this->_dialect->describeTableSql($name, $config); $statement = $this->_executeSql($sql, $params); if (count($statement) === 0) { throw new Exception(sprintf('Cannot describe %s. It has 0 columns.', $name)); } $table = new Table($name); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertFieldDescription($table, $row); } list($sql, $params) = $this->_dialect->describeIndexSql($name, $config); $statement = $this->_executeSql($sql, $params); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertIndexDescription($table, $row); } list($sql, $params) = $this->_dialect->describeForeignKeySql($name, $config); $statement = $this->_executeSql($sql, $params); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertForeignKeyDescription($table, $row); } $statement->closeCursor(); if (!empty($cacheConfig)) { Cache::write($cacheKey, $table, $cacheConfig); } return $table; }
/** * Builds oracle connection based on generic cakephp connection class. * * @param \Cake\Database\Connection $connection Connection object. * @return OracleConnection */ public static function build(Connection $connection) { $config = $connection->config(); $config['driver'] = $connection->driver(); return new OracleConnection($config); }
/** * Constructor * * @param \Phinx\Db\Adapter\AdapterInterface $adapter The original adapter to decorate. * @param \Cake\Database\Connection $connection The connection to actually use. */ public function __construct(AdapterInterface $adapter, Connection $connection) { $this->adapter = $adapter; $this->connection = $connection; $pdo = $adapter->getConnection(); if ($pdo->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_EXCEPTION) { $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } $connection->cacheMetadata(false); if ($connection->driver() instanceof Postgres) { $config = $connection->config(); $schema = empty($config['schema']) ? 'public' : $config['schema']; $pdo->exec('SET search_path TO ' . $schema); } $connection->driver()->connection($pdo); }