Example #1
0
 public function convertColumnDescription(Table $table, $row)
 {
     switch ($row['DATA_TYPE']) {
         case 'DATE':
             $field = ['type' => 'datetime', 'length' => null];
             break;
         case 'TIMESTAMP(6)':
             $field = ['type' => 'string', 'length' => $row['DATA_LENGTH']];
             break;
         case 'NUMBER':
             if ($row['DATA_PRECISION'] == 1) {
                 $field = ['type' => 'boolean', 'length' => null];
             } else {
                 if ($row['DATA_SCALE'] > 0) {
                     $field = ['type' => 'decimal', 'length' => $row['DATA_PRECISION'], 'precision' => $row['DATA_SCALE']];
                 } else {
                     $field = ['type' => 'integer', 'length' => $row['DATA_PRECISION']];
                 }
             }
             break;
         case 'VARCHAR2':
             $field = ['type' => 'string', 'length' => $row['DATA_LENGTH']];
             break;
         default:
     }
     $field += ['null' => $row['NULLABLE'] === 'Y' ? true : false, 'default' => $row['DATA_DEFAULT']];
     $table->addColumn(strtolower($row['COLUMN_NAME']), $field);
 }
Example #2
0
 /**
  * Build the fixtures table schema from the fields property.
  *
  * @return void
  */
 protected function _schemaFromFields()
 {
     $this->_schema = new Table($this->table);
     foreach ($this->fields as $field => $data) {
         if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
             continue;
         }
         // Trigger errors on deprecated usage.
         if (is_array($data) && isset($data['key'])) {
             $msg = 'Usage of the `key` options in columns is not supported. Try using the upgrade shell to migrate your fixtures.';
             $msg .= ' You can download the upgrade shell from https://github.com/cakephp/upgrade.';
             trigger_error($msg, E_USER_NOTICE);
         }
         $this->_schema->addColumn($field, $data);
     }
     if (!empty($this->fields['_constraints'])) {
         foreach ($this->fields['_constraints'] as $name => $data) {
             $this->_schema->addConstraint($name, $data);
         }
     }
     if (!empty($this->fields['_indexes'])) {
         // Trigger errors on deprecated usage.
         if (empty($data['type'])) {
             $msg = 'Indexes must define a type. Try using the upgrade shell to migrate your fixtures.';
             $msg .= ' You can download the upgrade shell from https://github.com/cakephp/upgrade.';
             trigger_error($msg, E_USER_NOTICE);
         }
         foreach ($this->fields['_indexes'] as $name => $data) {
             $this->_schema->addIndex($name, $data);
         }
     }
     if (!empty($this->fields['_options'])) {
         $this->_schema->options($this->fields['_options']);
     }
 }
Example #3
0
 /**
  * Build the fixtures table schema from the fields property.
  *
  * @return void
  */
 protected function _schemaFromFields()
 {
     $connection = ConnectionManager::get($this->connection());
     $this->_schema = new Table($this->table);
     foreach ($this->fields as $field => $data) {
         if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
             continue;
         }
         $this->_schema->addColumn($field, $data);
     }
     if (!empty($this->fields['_constraints'])) {
         foreach ($this->fields['_constraints'] as $name => $data) {
             if (!$connection->supportsDynamicConstraints() || $data['type'] !== Table::CONSTRAINT_FOREIGN) {
                 $this->_schema->addConstraint($name, $data);
             } else {
                 $this->_constraints[$name] = $data;
             }
         }
     }
     if (!empty($this->fields['_indexes'])) {
         foreach ($this->fields['_indexes'] as $name => $data) {
             $this->_schema->addIndex($name, $data);
         }
     }
     if (!empty($this->fields['_options'])) {
         $this->_schema->options($this->fields['_options']);
     }
 }
Example #4
0
 /**
  * Build the fixtures table schema from the fields property.
  *
  * @return void
  */
 protected function _schemaFromFields()
 {
     $this->_schema = new Table($this->table);
     foreach ($this->fields as $field => $data) {
         if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
             continue;
         }
         $this->_schema->addColumn($field, $data);
     }
     if (!empty($this->fields['_constraints'])) {
         foreach ($this->fields['_constraints'] as $name => $data) {
             $this->_schema->addConstraint($name, $data);
         }
     }
     if (!empty($this->fields['_indexes'])) {
         foreach ($this->fields['_indexes'] as $name => $data) {
             $this->_schema->addIndex($name, $data);
         }
     }
     if (!empty($this->fields['_options'])) {
         $this->_schema->options($this->fields['_options']);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function convertColumnDescription(Table $table, $row)
 {
     $field = $this->_convertColumn($row['type']);
     if ($field['type'] === 'boolean') {
         if ($row['default'] === 'true') {
             $row['default'] = 1;
         }
         if ($row['default'] === 'false') {
             $row['default'] = 0;
         }
     }
     if (!empty($row['has_serial'])) {
         $field['autoIncrement'] = true;
     }
     $field += ['default' => $this->_defaultValue($row['default']), 'null' => $row['null'] === 'YES' ? true : false, 'comment' => $row['comment']];
     $field['length'] = $row['char_length'] ?: $field['length'];
     $table->addColumn($row['name'], $field);
 }
Example #6
0
 /**
  * Test truncateSql()
  *
  * @return void
  */
 public function testTruncateSql()
 {
     $driver = $this->_getMockedDriver();
     $connection = $this->getMockBuilder('Cake\\Database\\Connection')->disableOriginalConstructor()->getMock();
     $connection->expects($this->any())->method('driver')->will($this->returnValue($driver));
     $table = new Table('schema_articles');
     $table->addColumn('id', 'integer')->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
     $result = $table->truncateSql($connection);
     $this->assertCount(1, $result);
     $this->assertEquals('TRUNCATE "schema_articles" RESTART IDENTITY CASCADE', $result[0]);
 }
Example #7
0
 /**
  * {@inheritDoc}
  */
 public function convertColumnDescription(Table $table, $row)
 {
     $field = $this->_convertColumn($row['type']);
     $field += ['null' => !$row['notnull'], 'default' => $this->_defaultValue($row['dflt_value'])];
     $primary = $table->constraint('primary');
     if ($row['pk'] && empty($primary)) {
         $field['null'] = false;
         $field['autoIncrement'] = true;
     }
     // SQLite does not support autoincrement on composite keys.
     if ($row['pk'] && !empty($primary)) {
         $existingColumn = $primary['columns'][0];
         $table->addColumn($existingColumn, ['autoIncrement' => null] + $table->column($existingColumn));
     }
     $table->addColumn($row['name'], $field);
     if ($row['pk']) {
         $constraint = (array) $table->constraint('primary') + ['type' => Table::CONSTRAINT_PRIMARY, 'columns' => []];
         $constraint['columns'] = array_merge($constraint['columns'], [$row['name']]);
         $table->addConstraint('primary', $constraint);
     }
 }
Example #8
0
 /**
  * Test generating a column that is a primary key.
  *
  * @return void
  */
 public function testColumnSqlPrimaryKey()
 {
     $driver = $this->_getMockedDriver();
     $schema = new MysqlSchema($driver);
     $table = new Table('articles');
     $table->addColumn('id', ['type' => 'integer', 'null' => false])->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
     $result = $schema->columnSql($table, 'id');
     $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
     $table = new Table('articles');
     $table->addColumn('id', ['type' => 'biginteger', 'null' => false])->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
     $result = $schema->columnSql($table, 'id');
     $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
 }
 /**
  * {@inheritDoc}
  */
 public function convertColumnDescription(Table $table, $row)
 {
     $row = array_change_key_case($row);
     switch ($row['type']) {
         case 'DATE':
             $field = ['type' => 'datetime', 'length' => null];
             break;
         case 'TIMESTAMP':
         case 'TIMESTAMP(6)':
         case 'TIMESTAMP(9)':
             $field = ['type' => 'timestamp', 'length' => null];
             break;
         case 'NUMBER':
         case 'INTEGER':
         case 'PLS_INTEGER':
         case 'BINARY_INTEGER':
             if ($row['data_precision'] == 1) {
                 $field = ['type' => 'boolean', 'length' => null];
             } elseif ($row['data_scale'] > 0) {
                 $field = ['type' => 'decimal', 'length' => $row['data_precision'], 'precision' => $row['data_scale']];
             } else {
                 $field = ['type' => 'integer', 'length' => $row['data_precision']];
             }
             break;
         case 'FLOAT':
         case 'BINARY_FLOAT':
         case 'BINARY_DOUBLE':
             $field = ['type' => 'float', 'length' => $row['data_precision']];
             break;
         case 'NCHAR':
         case 'NVARCHAR2':
         case 'CHAR':
         case 'VARCHAR2':
         case 'LONG':
         case 'ROWID':
         case 'UROWID':
             $length = $row['char_length'];
             if ($length == 36) {
                 $field = ['type' => 'uuid', 'length' => null];
             } else {
                 $field = ['type' => 'string', 'length' => $length];
             }
             break;
         case 'NCLOB':
         case 'CLOB':
             $field = ['type' => 'text', 'length' => $row['char_length']];
             break;
         case 'RAW':
         case 'LONG RAW':
         case 'BLOB':
             $field = ['type' => 'binary', 'length' => $row['char_length']];
             break;
         default:
     }
     $field += ['null' => $row['null'] === 'Y' ? true : false, 'default' => $row['default'], 'comment' => $row['comment']];
     $table->addColumn($this->_transformValueCase($row['name']), $field);
 }
Example #10
0
 /**
  * Test generating a bigint column that is a primary key.
  *
  * @return void
  */
 public function testColumnSqlPrimaryKeyBigInt()
 {
     $driver = $this->_getMockedDriver();
     $schema = new SqliteSchema($driver);
     $table = new Table('articles');
     $table->addColumn('id', ['type' => 'biginteger', 'null' => false])->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
     $result = $schema->columnSql($table, 'id');
     $this->assertEquals($result, '"id" BIGINT NOT NULL');
     $result = $schema->constraintSql($table, 'primary');
     $this->assertEquals('CONSTRAINT "primary" PRIMARY KEY ("id")', $result, 'Bigint primary keys are not special.');
 }
 /**
  * Test truncateSql()
  *
  * @return void
  */
 public function testTruncateSql()
 {
     $driver = $this->_getMockedDriver();
     $connection = $this->getMock('Cake\\Database\\Connection', [], [], '', false);
     $connection->expects($this->any())->method('driver')->will($this->returnValue($driver));
     $table = new Table('schema_articles');
     $table->addColumn('id', 'integer')->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
     $result = $table->truncateSql($connection);
     $this->assertCount(2, $result);
     $this->assertEquals('DELETE FROM [schema_articles]', $result[0]);
     $this->assertEquals('DBCC CHECKIDENT([schema_articles], RESEED, 0)', $result[1]);
 }
Example #12
0
 /**
  * Prepare column
  *
  * @param DOMElement $table
  * @param DOMXPath   $xpath
  * @param Table      $schemaTable
  */
 protected function prepareColumn(DOMElement $table, DOMXPath $xpath, Table $schemaTable)
 {
     $columns = $xpath->query(sprintf('/database/table[@name="%s"]/column', $table->getAttribute('name')));
     /** @var \DOMElement $column */
     foreach ($columns as $column) {
         $schemaTable->addColumn($column->getAttribute('name'), ['type' => $column->getAttribute('type') !== '' ? $column->getAttribute('type') : null, 'length' => $column->getAttribute('length') !== '' ? $column->getAttribute('length') : null, 'precision' => $column->getAttribute('precision') !== '' ? $column->getAttribute('precision') : null, 'default' => $column->getAttribute('default') !== '' ? $column->getAttribute('default') : null, 'null' => $column->getAttribute('null') !== '' ? filter_var($column->getAttribute('null'), FILTER_VALIDATE_BOOLEAN, ['flags' => FILTER_NULL_ON_FAILURE]) : null, 'fixed' => $column->getAttribute('fixed') !== '' ? $column->getAttribute('fixed') : null, 'unsigned' => $column->getAttribute('unsigned') !== '' ? $column->getAttribute('unsigned') : null, 'comment' => $column->getAttribute('comment') !== '' ? $column->getAttribute('comment') : null]);
     }
 }
Example #13
0
 /**
  * Add a foreign key constraint with bad data
  *
  * @dataProvider badForeignKeyProvider
  * @expectedException \Cake\Database\Exception
  * @return void
  */
 public function testAddConstraintForeignKeyBadData($data)
 {
     $table = new Table('articles');
     $table->addColumn('author_id', 'integer')->addConstraint('author_id_idx', $data);
 }
Example #14
0
 /**
  * {@inheritDoc}
  */
 public function convertColumnDescription(Table $table, $row)
 {
     $field = $this->_convertColumn($row['Type']);
     $field += ['null' => $row['Null'] === 'YES' ? true : false, 'default' => $row['Default'], 'collate' => $row['Collation'], 'comment' => $row['Comment']];
     if (isset($row['Extra']) && $row['Extra'] === 'auto_increment') {
         $field['autoIncrement'] = true;
     }
     $table->addColumn($row['Field'], $field);
 }
<?php

/**
 * Queued Tasks schema file
 *
 * @author David Yell <*****@*****.**>
 * @author MGriesbach@gmail.com
 */
use Cake\Database\Schema\Table;
$t = new Table('queued_tasks');
$t->addColumn('id', ['type' => 'integer', 'length' => 10, 'null' => false, 'default' => null]);
$t->addColumn('job_type', ['type' => 'string', 'null' => false, 'length' => 45]);
$t->addColumn('data', ['type' => 'text', 'null' => true, 'default' => null]);
$t->addColumn('job_group', ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null]);
$t->addColumn('reference', ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null]);
$t->addColumn('created', ['type' => 'datetime', 'null' => true, 'default' => null]);
$t->addColumn('notbefore', ['type' => 'datetime', 'null' => true, 'default' => null]);
$t->addColumn('fetched', ['type' => 'datetime', 'null' => true, 'default' => null]);
$t->addColumn('progress', ['type' => 'float', 'length' => '3,2', 'null' => true, 'default' => null]);
$t->addColumn('status', ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null]);
$t->addColumn('completed', ['type' => 'datetime', 'null' => true, 'default' => null]);
$t->addColumn('failed', ['type' => 'integer', 'null' => false, 'default' => '0', 'length' => 3]);
$t->addColumn('failure_message', ['type' => 'text', 'null' => true, 'default' => null]);
$t->addColumn('workerkey', ['type' => 'string', 'null' => true, 'length' => 45]);
$t->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
$t->options(['collate' => 'utf8_unicode_ci']);
Example #16
0
 /**
  * {@inheritDoc}
  */
 public function convertIndexDescription(Table $table, $row)
 {
     $type = Table::INDEX_INDEX;
     $name = $row['relname'];
     if ($row['indisprimary']) {
         $name = $type = Table::CONSTRAINT_PRIMARY;
     }
     if ($row['indisunique'] && $type === Table::INDEX_INDEX) {
         $type = Table::CONSTRAINT_UNIQUE;
     }
     preg_match('/\\(([^\\)]+)\\)/', $row['statement'], $matches);
     $columns = $this->_convertColumnList($matches[1]);
     if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) {
         $table->addConstraint($name, ['type' => $type, 'columns' => $columns]);
         // If there is only one column in the primary key and it is integery,
         // make it autoincrement.
         $columnDef = $table->column($columns[0]);
         if ($type === Table::CONSTRAINT_PRIMARY && count($columns) === 1 && in_array($columnDef['type'], ['integer', 'biginteger'])) {
             $columnDef['autoIncrement'] = true;
             $table->addColumn($columns[0], $columnDef);
         }
         return;
     }
     $table->addIndex($name, ['type' => $type, 'columns' => $columns]);
 }
 /**
  * {@inheritDoc}
  */
 public function convertFieldDescription(Table $table, $row)
 {
     $field = $this->_convertColumn($row['type'], $row['char_length'], $row['precision'], $row['scale']);
     if (!empty($row['default'])) {
         $row['default'] = trim($row['default'], '()');
     }
     if ($field['type'] === 'boolean') {
         $row['default'] = (int) $row['default'];
     }
     $field += ['null' => $row['null'] === 'YES' ? true : false, 'default' => $row['default']];
     $table->addColumn($row['name'], $field);
 }
Example #18
0
 /**
  * {@inheritDoc}
  */
 public function convertColumnDescription(Table $table, $row)
 {
     $field = $this->_convertColumn($row['type']);
     $field += ['null' => !$row['notnull'], 'default' => $row['dflt_value'] === null ? null : trim($row['dflt_value'], "'")];
     if ($row['pk']) {
         $field['null'] = false;
         $field['autoIncrement'] = true;
     }
     $table->addColumn($row['name'], $field);
     if ($row['pk']) {
         $constraint = (array) $table->constraint('primary') + ['type' => Table::CONSTRAINT_PRIMARY, 'columns' => []];
         $constraint['columns'] = array_merge($constraint['columns'], [$row['name']]);
         $table->addConstraint('primary', $constraint);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function convertColumnDescription(Table $table, $row)
 {
     $field = $this->_convertColumn($row['FIELD_TYPE']);
     $field += ['null' => $row['FIELD_NULL'] === '1' ? true : false, 'default' => $row['FIELD_DEFAULT'], 'collate' => $row['FIELD_COLLATION'], 'comment' => $row['FIELD_COMMENT']];
     if (isset($row['FIELD_EXTRA']) && $row['FIELD_EXTRA'] === 'AUTO_INCREMENT') {
         $field['autoIncrement'] = true;
     }
     $table->addColumn(trim($row['FIELD_NAME']), $field);
 }