/** * {@inheritDoc} */ public function create(ConnectionInterface $db) { if (empty($this->_schema)) { return false; } try { $queries = $this->_schema->createSql($db); foreach ($queries as $query) { $db->execute($query)->closeCursor(); } } catch (Exception $e) { $msg = sprintf('Fixture creation for "%s" failed "%s"', $this->table, $e->getMessage()); Log::error($msg); trigger_error($msg, E_USER_WARNING); return false; } return true; }
protected function _ensureTable($archiveTable) { try { $archiveTable->schema(); } catch (Exception $e) { $djSchema = TableRegistry::get('DelayedJobs.DelayedJobs')->schema(); $djColumns = $djSchema->columns(); $columns = []; foreach ($djColumns as $djColumn) { $columns[$djColumn] = $djSchema->column($djColumn); } $columns['payload']['type'] = 'binary'; $columns['options']['type'] = 'binary'; $archiveTableSchema = new Table($archiveTable->table(), $columns); $archiveTableSchema->addConstraint('primary', $djSchema->constraint('primary')); $createSql = $archiveTableSchema->createSql($archiveTable->connection()); foreach ($createSql as $createSqlQuery) { $archiveTable->connection()->query($createSqlQuery); } } }
/** * Test that if a table already exists in the test database, it will dropped * before being recreated * * @return void */ public function testResetDbIfTableExists() { $db = ConnectionManager::get('test'); $restore = $db->logQueries(); $db->logQueries(true); $this->manager->setDebug(true); $buffer = new ConsoleOutput(); Log::config('testQueryLogger', ['className' => 'Console', 'stream' => $buffer]); $table = new Table('articles', ['id' => ['type' => 'integer', 'unsigned' => true], 'title' => ['type' => 'string', 'length' => 255]]); $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); $sql = $table->createSql($db); foreach ($sql as $stmt) { $db->execute($stmt); } $test = $this->getMockBuilder('Cake\\TestSuite\\TestCase')->getMock(); $test->fixtures = ['core.articles']; $this->manager->fixturize($test); $this->manager->load($test); $db->logQueries($restore); $this->assertContains('DROP TABLE', implode('', $buffer->messages())); }
/** * test schema reflection without $import or $fields will reflect the schema * * @return void */ public function testInitNoImportNoFields() { $db = ConnectionManager::get('test'); $collection = $db->schemaCollection(); if (!in_array('letters', $collection->listTables())) { $table = new Table('letters', ['id' => ['type' => 'integer'], 'letter' => ['type' => 'string', 'length' => 1]]); $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); $sql = $table->createSql($db); foreach ($sql as $stmt) { $db->execute($stmt); } } $fixture = new LettersFixture(); $fixture->init(); $this->assertEquals(['id', 'letter'], $fixture->schema()->columns()); $db = $this->getMockBuilder('Cake\\Database\\Connection')->setMethods(['prepare', 'execute'])->disableOriginalConstructor()->getMock(); $db->expects($this->never())->method('prepare'); $db->expects($this->never())->method('execute'); $this->assertTrue($fixture->create($db)); $this->assertTrue($fixture->drop($db)); }
/** * Generate the SQL to create the Table without foreign keys. * * Uses the connection to access the schema dialect * to generate platform specific SQL. * * @param Connection $connection The connection to generate SQL for. * @return array List of SQL statements to create the table and the * required indexes. */ public function createSql(ConnectionInterface $connection) { $this->_extractForeignKeys($connection); return parent::createSql($connection); }