/** * 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']); } }
/** * 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']); } }
/** * 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']); } }
public function convertIndexDescription(Table $table, $row) { $type = null; $columns = $length = []; $name = $row['CONSTRAINT_NAME']; switch ($row['CONSTRAINT_TYPE']) { case 'P': $name = $type = Table::CONSTRAINT_PRIMARY; break; case 'U': $type = Table::CONSTRAINT_UNIQUE; break; default: return; //Not doing anything here with Oracle "Check" constraints or "Reference" constraints } $columns[] = strtolower($row['COLUMN_NAME']); $isIndex = $type === Table::INDEX_INDEX || $type === Table::INDEX_FULLTEXT; if ($isIndex) { $existing = $table->index($name); } else { $existing = $table->constraint($name); } if (!empty($existing)) { $columns = array_merge($existing['columns'], $columns); $length = array_merge($existing['length'], $length); } if ($isIndex) { $table->addIndex($name, ['type' => $type, 'columns' => $columns, 'length' => $length]); } else { $table->addConstraint($name, ['type' => $type, 'columns' => $columns, 'length' => $length]); } }
/** * {@inheritDoc} * * Since SQLite does not have a way to get metadata about all indexes at once, * additional queries are done here. Sqlite constraint names are not * stable, and the names for constraints will not match those used to create * the table. This is a limitation in Sqlite's metadata features. * */ public function convertIndexDescription(Table $table, $row) { $sql = sprintf('PRAGMA index_info(%s)', $this->_driver->quoteIdentifier($row['name'])); $statement = $this->_driver->prepare($sql); $statement->execute(); $columns = []; foreach ($statement->fetchAll('assoc') as $column) { $columns[] = $column['name']; } $statement->closeCursor(); if ($row['unique']) { $table->addConstraint($row['name'], ['type' => Table::CONSTRAINT_UNIQUE, 'columns' => $columns]); } else { $table->addIndex($row['name'], ['type' => Table::INDEX_INDEX, 'columns' => $columns]); } }
/** * {@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 convertIndexDescription(Table $table, $row) { $type = null; $columns = $length = []; $name = $row['Key_name']; if ($name === 'PRIMARY') { $name = $type = Table::CONSTRAINT_PRIMARY; } $columns[] = $row['Column_name']; if ($row['Index_type'] === 'FULLTEXT') { $type = Table::INDEX_FULLTEXT; } elseif ($row['Non_unique'] == 0 && $type !== 'primary') { $type = Table::CONSTRAINT_UNIQUE; } elseif ($type !== 'primary') { $type = Table::INDEX_INDEX; } if (!empty($row['Sub_part'])) { $length[$row['Column_name']] = $row['Sub_part']; } $isIndex = $type === Table::INDEX_INDEX || $type === Table::INDEX_FULLTEXT; if ($isIndex) { $existing = $table->index($name); } else { $existing = $table->constraint($name); } // MySQL multi column indexes come back as multiple rows. if (!empty($existing)) { $columns = array_merge($existing['columns'], $columns); $length = array_merge($existing['length'], $length); } if ($isIndex) { $table->addIndex($name, ['type' => $type, 'columns' => $columns, 'length' => $length]); } else { $table->addConstraint($name, ['type' => $type, 'columns' => $columns, 'length' => $length]); } }
/** * {@inheritDoc} */ public function convertIndexDescription(Table $table, $row) { $type = Table::INDEX_INDEX; $name = $row['index_name']; if ($row['is_primary_key']) { $name = $type = Table::CONSTRAINT_PRIMARY; } if ($row['is_unique_constraint'] && $type === Table::INDEX_INDEX) { $type = Table::CONSTRAINT_UNIQUE; } if ($type === Table::INDEX_INDEX) { $existing = $table->index($name); } else { $existing = $table->constraint($name); } $columns = [$row['column_name']]; if (!empty($existing)) { $columns = array_merge($existing['columns'], $columns); } if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) { $table->addConstraint($name, ['type' => $type, 'columns' => $columns]); return; } $table->addIndex($name, ['type' => $type, 'columns' => $columns]); }
/** * {@inheritDoc} */ public function convertIndexDescription(Table $table, $row) { $tableIndex = array_change_key_case($row); $type = null; $columns = $length = []; $keyName = $this->_transformValueCase($tableIndex['name']); $name = $this->_transformValueCase($tableIndex['column_name']); if (strtolower($tableIndex['is_primary']) == 'p') { $keyName = $type = Table::CONSTRAINT_PRIMARY; } elseif ($tableIndex['is_unique']) { $type = Table::CONSTRAINT_UNIQUE; } else { $type = Table::INDEX_INDEX; } $columns[] = $this->_transformValueCase($name); $isIndex = $type === Table::INDEX_INDEX; if ($isIndex) { $existing = $table->index($keyName); } else { $existing = $table->constraint($keyName); } if (!empty($existing)) { $columns = array_merge($existing['columns'], $columns); } if ($isIndex) { $table->addIndex($keyName, ['type' => $type, 'columns' => $columns]); } else { $table->addConstraint($keyName, ['type' => $type, 'columns' => $columns]); } }
/** * Prepare index * * @param DOMElement $table * @param DOMXPath $xpath * @param Table $schemaTable */ protected function prepareIndex(DOMElement $table, DOMXPath $xpath, Table $schemaTable) { $indexes = $xpath->query(sprintf('/database/table[@name="%s"]/index', $table->getAttribute('name'))); /** @var DOMElement $index */ foreach ($indexes as $index) { $indexColumns = $index->getElementsByTagName('index-column'); $indexName = $index->getAttribute('name'); $indexType = $index->getAttribute('type'); $tmpIndexColumns = []; /** @var DOMElement $indexColumn */ foreach ($indexColumns as $indexColumn) { $tmpIndexColumns[] = $indexColumn->getAttribute('name'); } if (empty(trim($indexType))) { $indexType = 'index'; } if (empty(trim($indexName))) { $indexName = sprintf('%s_%s_%s', $table->getAttribute('name'), implode('_', $tmpIndexColumns), $indexType); } $schemaTable->addIndex($indexName, ['type' => $indexType, 'columns' => $tmpIndexColumns]); } }
/** * Gets an schema instance for the given fixture class. * * @param string $fixtureClassName The fixture to be "converted" * @return \Cake\Database\Schema\Table Schema instance */ protected function _prepareSchema($fixtureClassName) { $fixture = new $fixtureClassName(); if (!empty($fixture->table)) { $tableName = $fixture->table; } else { $tableName = (string) Inflector::underscore(str_replace_last('Fixture', '', $fixtureClassName)); } list($fields, $constraints, $indexes, $options) = $this->_prepareSchemaProperties($fixture); $schema = new TableSchema($tableName, $fields); foreach ($constraints as $name => $attrs) { $schema->addConstraint($name, $attrs); } foreach ($indexes as $name => $attrs) { $schema->addIndex($name, $attrs); } if (!empty($options)) { $schema->options($options); } return $schema; }
/** * Test adding different kinds of indexes. * * @return void */ public function testAddIndexTypes() { $table = new Table('articles'); $table->addColumn('id', 'integer')->addColumn('title', 'string')->addColumn('author_id', 'integer'); $table->addIndex('author_idx', ['columns' => ['author_id'], 'type' => 'index'])->addIndex('texty', ['type' => 'fulltext', 'columns' => ['title']]); $this->assertEquals(['author_idx', 'texty'], $table->indexes()); }