dropSchema() public method

Generate a "drop table" statement for the given Schema object
public dropSchema ( CakeSchema $schema, string $table = null ) : string
$schema CakeSchema An instance of a subclass of CakeSchema
$table string Optional. If specified only the table name given will be generated. Otherwise, all tables defined in the schema are generated.
return string
Example #1
0
 /**
  * testDropSchemaNoSchema method
  *
  * @expectedException PHPUnit_Framework_Error
  * @throws PHPUnit_Framework_Error
  * @return void
  */
 public function testDropSchemaNoSchema()
 {
     try {
         $this->Dbo->dropSchema(null);
     } catch (Throwable $t) {
         throw new PHPUnit_Framework_Error($t);
     }
 }
Example #2
0
 /**
  * sql drop statement
  *
  * @param $datasource
  * @param $tablename
  * @param $exclude_missing_tables
  * @return string
  */
 function getDropSql($datasource, $tablename = null, $exclude_missing_tables = false)
 {
     if (!$this->_checkCurrentDatasource($datasource)) {
         $this->_setupDataSource();
     }
     $this->Schema->tables = $this->_getProcessTables($tablename, $exclude_missing_tables);
     $sql = $this->DataSource->dropSchema($this->Schema);
     return $this->out($sql);
 }
Example #3
0
 /**
  * testSchema method
  *
  * @access public
  * @return void
  */
 public function testSchema()
 {
     $Schema = new CakeSchema();
     $Schema->tables = array('table' => array(), 'anotherTable' => array());
     $this->expectError();
     $result = $this->Dbo->dropSchema(null);
     $this->assertTrue($result === null);
     $result = $this->Dbo->dropSchema($Schema, 'non_existing');
     $this->assertTrue(empty($result));
     $result = $this->Dbo->dropSchema($Schema, 'table');
     $this->assertPattern('/^\\s*DROP TABLE IF EXISTS\\s+' . $this->Dbo->fullTableName('table') . ';\\s*$/s', $result);
 }
 /**
  * Drop an existing table.
  *
  * @param string $table
  * @throws MissingTableException if the table does not exists in the database
  * @throws MigrationException if an sql error occurred
  * @return Migration
  */
 public function dropTable($table)
 {
     if (!in_array($this->_db->fullTableName($table, false, false), $this->_db->listSources())) {
         throw new MissingTableException(__d('migration', 'Table "%s" does not exist in database.', $this->_db->fullTableName($table, false, false)));
     }
     $this->_schema->tables = array($table => array());
     try {
         $this->_db->execute($this->_db->dropSchema($this->_schema));
     } catch (Exception $e) {
         throw new MigrationException(__d('migration', 'SQL Error: %s', $e->getMessage()));
     }
     return $this;
 }
 /**
  * Drop Table method
  *
  * @param string $type Type of operation to be done, in this case 'drop_table'
  * @param array $tables List of tables to be dropped
  * @return boolean Return true in case of success, otherwise false
  * @throws MigrationException
  */
 protected function _dropTable($type, $tables)
 {
     foreach ($tables as $table) {
         $this->Schema->tables = array($table => array());
         if ($this->_invokePrecheck('beforeAction', 'drop_table', array('table' => $table))) {
             $this->_invokeCallbacks('beforeAction', 'drop_table', array('table' => $table));
             if (@$this->db->execute($this->db->dropSchema($this->Schema)) === false) {
                 throw new MigrationException($this, sprintf(__d('migrations', 'SQL Error: %s'), $this->db->error));
             }
             $this->_invokeCallbacks('afterAction', 'drop_table', array('table' => $table));
         }
     }
     return true;
 }
 /**
  * Test the alter index capabilities of postgres
  *
  * @return void
  */
 public function testAlterIndexes()
 {
     $this->Dbo->cacheSources = false;
     $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))));
     $this->Dbo->rawQuery($this->Dbo->createSchema($schema1));
     $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('unique' => false, 'column' => 'name'), 'group_idx' => array('unique' => false, 'column' => 'group1'), 'compound_idx' => array('unique' => false, 'column' => array('group1', 'group2')), 'PRIMARY' => array('unique' => true, 'column' => 'id')))));
     $this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1)));
     $indexes = $this->Dbo->index('altertest');
     $this->assertEquals($schema2->tables['altertest']['indexes'], $indexes);
     // 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('unique' => true, 'column' => 'name'), 'group_idx' => array('unique' => false, 'column' => 'group2'), 'compound_idx' => array('unique' => false, 'column' => array('group2', 'group1')), 'another_idx' => array('unique' => false, 'column' => array('group1', 'name'))))));
     $this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2)));
     $indexes = $this->Dbo->index('altertest');
     $this->assertEquals($schema3->tables['altertest']['indexes'], $indexes);
     // Compare us to ourself.
     $this->assertEquals(array(), $schema3->compare($schema3));
     // Drop the indexes
     $this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3)));
     $indexes = $this->Dbo->index('altertest');
     $this->assertEquals(array(), $indexes);
     $this->Dbo->query($this->Dbo->dropSchema($schema1));
 }
 /**
  * testDropSchemaNoSchema method
  *
  * @expectedException PHPUnit_Framework_Error
  * @return void
  */
 public function testDropSchemaNoSchema()
 {
     $result = $this->Dbo->dropSchema(null);
 }
 /**
  * Run after all tests executed, should return SQL statement to drop table for this fixture.
  *
  * @param DboSource $db An instance of the database object used to create the fixture table
  * @return bool True on success, false on failure
  */
 public function drop($db)
 {
     if (empty($this->fields)) {
         return false;
     }
     $this->Schema->build(array($this->table => $this->fields));
     try {
         $db->execute($db->dropSchema($this->Schema), array('log' => false));
         $this->created = array_diff($this->created, array($db->configKeyName));
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
Example #9
0
 /**
  * testDropSchemaNoSchema method
  *
  * @expectedException PHPUnit_Framework_Error
  * @return void
  */
 public function testDropSchemaNoSchema()
 {
     $this->Dbo->dropSchema(NULL);
 }