Пример #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;
 }
Пример #2
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');
    }
Пример #3
0
 /**
  * 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'));
 }
Пример #4
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]);
    }
Пример #5
0
 /**
  * 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());
 }
Пример #6
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'));
 }