Esempio n. 1
0
 /**
  * Alter table.
  *
  * @since 1.0.0
  * @access public
  *
  * @param Freyja\Database\Schema\Table $table Table to be altered.
  *
  * @throws Freyja\Exceptions\RuntimeException if raised by
  * `Freyja\Database\Schema\Table::getAlteration()`.
  * @throws Freyja\Exceptions\ExceptionInterface the same exception raised by
  * Freyja\Database\Database::execute() if the query fails.
  */
 public function alter(Table $table)
 {
     if (!$this->hasTable($table)) {
     } else {
         try {
             // Table exists, alter it.
             $this->database->execute($table);
             // Query ok (if no exception was raised), update schema property.
             $alteration = $table->getAlteration();
         } catch (ExceptionInterface $e) {
             throw $e;
         }
         foreach ($alteration as $type => $fields) {
             switch ($type) {
                 case 'ADD':
                     $this->schema['tables'][$table->getName()]['fields'] = array_merge($this->schema['tables'][$table->getName()]['fields'], $fields);
                     break;
                 case 'DROP COLUMN':
                     foreach (array_keys($fields) as $name) {
                         unset($this->schema['tables'][$table->getName()]['fields'][$name]);
                     }
                     break;
             }
         }
         // Write new schema in yaml file.
         $this->updateSchema();
     }
 }
Esempio n. 2
0
 /**
  * Test for `Schema::create()`.
  *
  * @since 1.0.0
  * @access public
  *
  * @requires function Freyja\Database\Schema\Schema::__construct
  * @requires function Freyja\Database\Database::__construct
  * @requires function Freyja\Database\Database::connect
  * @requires function Freyja\Database\Driver\MySqlDriver::connect
  * @requires function Freyja\Database\Schema\Schema::create
  */
 public function testCreate()
 {
     // Load data.
     $ds = $this->getDataSet(array('customers'));
     $this->loadDataSet($ds);
     // Set accessbiility to object property.
     $reflection_schema = new ReflectionProperty('Freyja\\Database\\Schema\\Schema', 'schema');
     $reflection_schema->setAccessible(true);
     $prod_id = new Field('product_id');
     $prod_id->varchar(200);
     $name = new Field('name');
     $name->varchar(200);
     $quantity = new Field('quantity');
     $quantity->varchar(200);
     $prods = new Table('products', array($prod_id, $name, $quantity));
     $db = new Database(new MySqlDriver());
     $schema = new Schema($db->connect('localhost', 'test', 'travis', ''));
     $schema->create($prods);
     $query = new MySqlQuery();
     $query->table('products')->insert(array('product_id', 'name', 'quantity'), array(array(null, null, null)));
     $db->execute($query);
     $query_table = $this->getConnection()->createQueryTable('products', 'SELECT * FROM products');
     $expected_table = new \PHPUnit_Extensions_Database_DataSet_YamlDataSet(dirname(__FILE__) . '/fixtures/products.yml');
     $this->assertTablesEqual($query_table, $expected_table->getTable('products'));
     $expected_schema = array('fields' => array('product_id' => array('type' => 'VARCHAR(200)', 'default' => null, 'NOT NULL' => false, 'UNSIGNED' => false, 'AUTO_INCREMENT' => false), 'name' => array('type' => 'VARCHAR(200)', 'default' => null, 'NOT NULL' => false, 'UNSIGNED' => false, 'AUTO_INCREMENT' => false), 'quantity' => array('type' => 'VARCHAR(200)', 'default' => null, 'NOT NULL' => false, 'UNSIGNED' => false, 'AUTO_INCREMENT' => false)), 'primary' => array(), 'foreign' => array(), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'engine' => 'InnoDB');
     $retr_schema = $reflection_schema->getValue($schema);
     $this->assertEquals($expected_schema, $retr_schema['tables']['products'], 'Failed asserting that Schema::create() correctly update the database schema.');
     $this->getConnection()->getConnection()->exec('DROP TABLE IF EXISTS products');
 }