Beispiel #1
0
 /**
  * 2016-11-04
  */
 public function df_db_column_add_drop_2()
 {
     /** @var $name */
     $name = df_uid(4, 'test_');
     df_db_column_add(self::$TABLE, $name, "varchar(9) NOT null DEFAULT 'test'");
     $this->assertTrue(df_db_column_exists(self::$TABLE, $name));
     /** @var array(string => string|int|null) $info */
     $info = df_db_column_describe(self::$TABLE, $name);
     $this->assertEquals('test', $info['DEFAULT']);
     $this->assertEquals('9', $info['LENGTH']);
     df_db_column_drop(self::$TABLE, $name);
     $this->assertFalse(df_db_column_exists(self::$TABLE, $name));
 }
Beispiel #2
0
/**
 * 2016-11-04
 * «How to rename a database column?» https://mage2.pro/t/2240
 * Unfortunatyly, MySQL does not allow to rename a database column
 * without repeating the column's definition: http://stackoverflow.com/questions/8553130
 * The Magento 2 core classes do not have such method too.
 * So, we implement such function ourself.
 * @param string $table
 * @param string $from  The column should exist in the table!
 * @param string $to
 * @return void
 */
function df_db_column_rename($table, $from, $to)
{
    /**
     * 2016-11-04
     * @uses df_table() call is required here,
     * because @uses \Magento\Framework\DB\Adapter\Pdo\Mysql methods
     * does not add the custom table prefix to the $name.
     * The custom table prefix could be set my a Magento 2 administrator
     * during Magento 2 intallation (see the «table_prefix» key in the app/etc/env.php file).
     */
    $table = df_table($table);
    /** @var array(string => string|int|null) $definitionRaw */
    $definitionRaw = df_db_column_describe($table, $from);
    /**
    * 2016-11-04
    * @var array(string => string|int|null) $definition
    * Got an array like:
    		{
    			"name": "test_7781",
    			"type": "text",
    			"length": "255",
    			"options": [],
    			"comment": "Test 7781"
    		}
    */
    $definition = df_conn()->getColumnCreateByDescribe($definitionRaw);
    /**
     * 2016-11-04
     * The @uses \Magento\Framework\DB\Adapter\Pdo\Mysql::getColumnCreateByDescribe() method
     * sets the table's name as the table's comment:
     * https://github.com/magento/magento2/blob/2.1.2/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php#L1600
     * We remove this comment, because the table will be renamed.
     */
    unset($definition['comment']);
    df_conn()->changeColumn($table, $from, $to, $definition);
    /**
     * 2016-11-04
     * @see \Magento\Framework\DB\Adapter\Pdo\Mysql::resetDdlCache() call is not needed here,
     * because it has already been called
     * from @uses \Magento\Framework\DB\Adapter\Pdo\Mysql::changeColumn()
     * https://github.com/magento/magento2/blob/2.1.2/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php#L1010
     */
}