Example #1
0
 /**
  * {@inheritDoc}
  *
  */
 public function describe($name, array $options = [])
 {
     $options += ['forceRefresh' => false];
     $cacheConfig = $this->cacheMetadata();
     $cacheKey = $this->cacheKey($name);
     if (!empty($cacheConfig) && !$options['forceRefresh']) {
         $cached = Cache::read($cacheKey, $cacheConfig);
         if ($cached !== false) {
             return $cached;
         }
     }
     $table = parent::describe($name, $options);
     if (!empty($cacheConfig)) {
         Cache::write($cacheKey, $table, $cacheConfig);
     }
     return $table;
 }
 /**
  * Test describing a table with indexes
  *
  * @return void
  */
 public function testDescribeTableIndexes()
 {
     $connection = ConnectionManager::get('test');
     $this->_createTables($connection);
     $schema = new SchemaCollection($connection);
     $result = $schema->describe('schema_articles');
     $this->assertInstanceOf('Cake\\Database\\Schema\\Table', $result);
     $expected = ['primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], 'content_idx' => ['type' => 'unique', 'columns' => ['title', 'body'], 'length' => []]];
     $this->assertCount(3, $result->constraints());
     $expected = ['primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], 'content_idx' => ['type' => 'unique', 'columns' => ['title', 'body'], 'length' => []], 'author_idx' => ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['schema_authors', 'id'], 'length' => [], 'update' => 'cascade', 'delete' => 'restrict']];
     $this->assertEquals($expected['primary'], $result->constraint('primary'));
     $this->assertEquals($expected['content_idx'], $result->constraint('content_idx'));
     $this->assertEquals($expected['author_idx'], $result->constraint('author_idx'));
     $this->assertCount(1, $result->indexes());
     $expected = ['type' => 'index', 'columns' => ['author_id'], 'length' => []];
     $this->assertEquals($expected, $result->index('author_idx'));
 }
Example #3
0
    /**
     * Test describing a table with indexes with nulls first
     *
     * @return void
     */
    public function testDescribeTableIndexesNullsFirst()
    {
        $this->_needsConnection();
        $connection = ConnectionManager::get('test');
        $connection->execute('DROP TABLE IF EXISTS schema_index');
        $table = <<<SQL
CREATE TABLE schema_index (
  id serial NOT NULL,
  user_id integer NOT NULL,
  group_id integer NOT NULL,
  grade double precision
)
WITH (
  OIDS=FALSE
)
SQL;
        $connection->execute($table);
        $index = <<<SQL
CREATE INDEX schema_index_nulls
  ON schema_index
  USING btree
  (group_id, grade DESC NULLS FIRST);
SQL;
        $connection->execute($index);
        $schema = new SchemaCollection($connection);
        $result = $schema->describe('schema_index');
        $this->assertCount(1, $result->indexes());
        $expected = ['type' => 'index', 'columns' => ['group_id', 'grade'], 'length' => []];
        $this->assertEquals($expected, $result->index('schema_index_nulls'));
        $connection->execute('DROP TABLE schema_index');
    }
Example #4
0
 /**
  * Gets a list of table to baked based on the Collection instance passed and the options passed to
  * the shell call.
  *
  * @param \Cake\Database\Schema\Collection $collection Instance of the collection of a specific database
  * connection.
  * @param array $options Array of options passed to a shell call.
  * @return array
  */
 protected function getTablesToBake(Collection $collection, $options = [])
 {
     $options = array_merge(['require-table' => false, 'plugin' => null], $options);
     $tables = $collection->listTables();
     if ($options['require-table'] === true || $options['plugin']) {
         $tableNamesInModel = $this->getTableNames($options['plugin']);
         if (empty($tableNamesInModel)) {
             return [];
         }
         foreach ($tableNamesInModel as $num => $table) {
             if (!in_array($table, $tables)) {
                 unset($tableNamesInModel[$num]);
             }
         }
         $tables = $tableNamesInModel;
     } else {
         foreach ($tables as $num => $table) {
             if (in_array($table, $this->skipTables) || strpos($table, $this->skipTablesRegex) !== false) {
                 unset($tables[$num]);
                 continue;
             }
         }
     }
     return $tables;
 }
Example #5
0
    public function testDescribeNonPrimaryAutoIncrement()
    {
        $this->_needsConnection();
        $connection = ConnectionManager::get('test');
        $sql = <<<SQL
CREATE TABLE `odd_primary_key` (
`id` BIGINT UNSIGNED NOT NULL,
`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `other_field` (`other_field`)
)
SQL;
        $connection->execute($sql);
        $schema = new SchemaCollection($connection);
        $table = $schema->describe('odd_primary_key');
        $connection->execute('DROP TABLE odd_primary_key');
        $column = $table->column('id');
        $this->assertNull($column['autoIncrement'], 'should not autoincrement');
        $this->assertTrue($column['unsigned'], 'should be unsigned');
        $column = $table->column('other_field');
        $this->assertTrue($column['autoIncrement'], 'should not autoincrement');
        $this->assertFalse($column['unsigned'], 'should not be unsigned');
        $output = $table->createSql($connection);
        $this->assertContains('`id` BIGINT UNSIGNED NOT NULL,', $output[0]);
        $this->assertContains('`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,', $output[0]);
    }
Example #6
0
 /**
  * Gets a list of table to baked based on the Collection instance passed and the options passed to
  * the shell call.
  *
  * @param \Cake\Database\Schema\Collection $collection Instance of the collection of a specific database
  * connection.
  * @param array $options Array of options passed to a shell call.
  * @return array
  */
 protected function getTablesToBake(Collection $collection, $options = [])
 {
     $options = array_merge(['require-table' => false, 'plugin' => null], $options);
     $tables = $collection->listTables();
     if (empty($tables)) {
         return $tables;
     }
     if ($options['require-table'] === true || $options['plugin']) {
         $tableNamesInModel = $this->getTableNames($options['plugin']);
         if (empty($tableNamesInModel)) {
             return [];
         }
         foreach ($tableNamesInModel as $num => $table) {
             if (strpos($table, '.') !== false) {
                 $splitted = array_reverse(explode('.', $table, 2));
                 $config = ConnectionManager::config($this->connection);
                 $key = isset($config['schema']) ? 'schema' : 'database';
                 if ($config[$key] === $splitted[1]) {
                     $table = $splitted[0];
                 }
             }
             if (!in_array($table, $tables)) {
                 unset($tableNamesInModel[$num]);
             }
         }
         $tables = $tableNamesInModel;
     } else {
         foreach ($tables as $num => $table) {
             if (in_array($table, $this->skipTables) || strpos($table, $this->skipTablesRegex) !== false) {
                 unset($tables[$num]);
                 continue;
             }
         }
     }
     return $tables;
 }
 /**
  * Test describing a table creates options
  *
  * @return void
  */
 public function testDescribeTableOptions()
 {
     $connection = ConnectionManager::get('test');
     $this->_createTables($connection);
     $schema = new SchemaCollection($connection);
     $result = $schema->describe('schema_articles');
     $this->assertArrayHasKey('engine', $result->options());
     $this->assertArrayHasKey('collation', $result->options());
 }
Example #8
0
 /**
  * Test that describing non-existent tables fails.
  *
  * Tests for positive describe() calls are in each platformSchema
  * test case.
  *
  * @expectedException \Cake\Database\Exception
  * @return void
  */
 public function testDescribeIncorrectTable()
 {
     $schema = new Collection($this->connection);
     $this->assertNull($schema->describe('derp'));
 }