/** * testAlterSchemaIndexes method * * @group indices * @return void */ function testAlterSchemaIndexes() { App::import('Model', 'CakeSchema'); $this->Dbo->cacheSources = $this->Dbo->testing = false; $table = $this->Dbo->fullTableName('altertest'); $schema1 = new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true)))); $result = $this->Dbo->createSchema($schema1); $this->assertContains('`id` int(11) DEFAULT 0 NOT NULL,', $result); $this->assertContains('`name` varchar(50) NOT NULL,', $result); $this->assertContains('`group1` int(11) DEFAULT NULL', $result); $this->assertContains('`group2` int(11) DEFAULT NULL', $result); //Test that the string is syntactically correct $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($result, $query->queryString); $schema2 = new CakeSchema(array('name' => 'AlterTest2', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true), 'indexes' => array('name_idx' => array('column' => 'name', 'unique' => 0), 'group_idx' => array('column' => 'group1', 'unique' => 0), 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0), 'PRIMARY' => array('column' => 'id', 'unique' => 1))))); $result = $this->Dbo->alterSchema($schema2->compare($schema1)); $this->assertContains("ALTER TABLE {$table}", $result); $this->assertContains('ADD KEY name_idx (`name`),', $result); $this->assertContains('ADD KEY group_idx (`group1`),', $result); $this->assertContains('ADD KEY compound_idx (`group1`, `group2`),', $result); $this->assertContains('ADD PRIMARY KEY (`id`);', $result); //Test that the string is syntactically correct $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($result, $query->queryString); // Change three indexes, delete one and add another one $schema3 = new CakeSchema(array('name' => 'AlterTest3', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true), 'indexes' => array('name_idx' => array('column' => 'name', 'unique' => 1), 'group_idx' => array('column' => 'group2', 'unique' => 0), 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0), 'id_name_idx' => array('column' => array('id', 'name'), 'unique' => 0))))); $result = $this->Dbo->alterSchema($schema3->compare($schema2)); $this->assertContains("ALTER TABLE {$table}", $result); $this->assertContains('DROP PRIMARY KEY,', $result); $this->assertContains('DROP KEY name_idx,', $result); $this->assertContains('DROP KEY group_idx,', $result); $this->assertContains('DROP KEY compound_idx,', $result); $this->assertContains('ADD KEY id_name_idx (`id`, `name`),', $result); $this->assertContains('ADD UNIQUE KEY name_idx (`name`),', $result); $this->assertContains('ADD KEY group_idx (`group2`),', $result); $this->assertContains('ADD KEY compound_idx (`group2`, `group1`);', $result); $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($result, $query->queryString); // Compare us to ourself. $this->assertEqual($schema3->compare($schema3), array()); // Drop the indexes $result = $this->Dbo->alterSchema($schema1->compare($schema3)); $this->assertContains("ALTER TABLE {$table}", $result); $this->assertContains('DROP KEY name_idx,', $result); $this->assertContains('DROP KEY group_idx,', $result); $this->assertContains('DROP KEY compound_idx,', $result); $this->assertContains('DROP KEY id_name_idx;', $result); $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($result, $query->queryString); }
/** * testAlterSchemaIndexes method * * @group indices * @return void */ public function testAlterSchemaIndexes() { $this->Dbo->cacheSources = $this->Dbo->testing = FALSE; $table = $this->Dbo->fullTableName('altertest'); $schemaA = new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => FALSE, 'default' => 0), 'name' => array('type' => 'string', 'null' => FALSE, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => TRUE), 'group2' => array('type' => 'integer', 'null' => TRUE)))); $result = $this->Dbo->createSchema($schemaA); $this->assertContains('`id` int(11) DEFAULT 0 NOT NULL,', $result); $this->assertContains('`name` varchar(50) NOT NULL,', $result); $this->assertContains('`group1` int(11) DEFAULT NULL', $result); $this->assertContains('`group2` int(11) DEFAULT NULL', $result); //Test that the string is syntactically correct $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($query->queryString, $result); $schemaB = new CakeSchema(array('name' => 'AlterTest2', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => FALSE, 'default' => 0), 'name' => array('type' => 'string', 'null' => FALSE, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => TRUE), 'group2' => array('type' => 'integer', 'null' => TRUE), 'indexes' => array('name_idx' => array('column' => 'name', 'unique' => 0), 'group_idx' => array('column' => 'group1', 'unique' => 0), 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0), 'PRIMARY' => array('column' => 'id', 'unique' => 1))))); $result = $this->Dbo->alterSchema($schemaB->compare($schemaA)); $this->assertContains("ALTER TABLE {$table}", $result); $this->assertContains('ADD KEY `name_idx` (`name`),', $result); $this->assertContains('ADD KEY `group_idx` (`group1`),', $result); $this->assertContains('ADD KEY `compound_idx` (`group1`, `group2`),', $result); $this->assertContains('ADD PRIMARY KEY (`id`);', $result); //Test that the string is syntactically correct $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($query->queryString, $result); // Change three indexes, delete one and add another one $schemaC = new CakeSchema(array('name' => 'AlterTest3', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => FALSE, 'default' => 0), 'name' => array('type' => 'string', 'null' => FALSE, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => TRUE), 'group2' => array('type' => 'integer', 'null' => TRUE), 'indexes' => array('name_idx' => array('column' => 'name', 'unique' => 1), 'group_idx' => array('column' => 'group2', 'unique' => 0), 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0), 'id_name_idx' => array('column' => array('id', 'name'), 'unique' => 0))))); $result = $this->Dbo->alterSchema($schemaC->compare($schemaB)); $this->assertContains("ALTER TABLE {$table}", $result); $this->assertContains('DROP PRIMARY KEY,', $result); $this->assertContains('DROP KEY `name_idx`,', $result); $this->assertContains('DROP KEY `group_idx`,', $result); $this->assertContains('DROP KEY `compound_idx`,', $result); $this->assertContains('ADD KEY `id_name_idx` (`id`, `name`),', $result); $this->assertContains('ADD UNIQUE KEY `name_idx` (`name`),', $result); $this->assertContains('ADD KEY `group_idx` (`group2`),', $result); $this->assertContains('ADD KEY `compound_idx` (`group2`, `group1`);', $result); $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($query->queryString, $result); // Compare us to ourself. $this->assertEquals(array(), $schemaC->compare($schemaC)); // Drop the indexes $result = $this->Dbo->alterSchema($schemaA->compare($schemaC)); $this->assertContains("ALTER TABLE {$table}", $result); $this->assertContains('DROP KEY `name_idx`,', $result); $this->assertContains('DROP KEY `group_idx`,', $result); $this->assertContains('DROP KEY `compound_idx`,', $result); $this->assertContains('DROP KEY `id_name_idx`;', $result); $query = $this->Dbo->getConnection()->prepare($result); $this->assertEquals($query->queryString, $result); }
/** * Get the table hash from MySQL for a specific table * * @param DboSource $db * @return string */ protected function _hash($db) { $db_conn = $db->getConnection(); $sth = $db_conn->prepare("CHECKSUM TABLE " . $this->table); $sth->execute(); $result = $sth->fetch(PDO::FETCH_ASSOC); $sth->closeCursor(); $checksum = $result['Checksum']; return $checksum; }