Пример #1
1
 protected function copyAll(Driver $src, Driver $dst)
 {
     foreach ($src->getDatabaseNames() as $db) {
         if ($db != 'test') {
             continue;
         }
         $dst->createDatabase($db);
         foreach ($src->getSchemaNames($db) as $schema) {
             $dst->createSchema($db, $schema);
             foreach ($src->getTableNames($db, $schema) as $table) {
                 $dst->setTable($db, $schema, $src->getTable($db, $schema, $table));
             }
         }
     }
 }
Пример #2
0
    public function testSetTable()
    {
        $pdo = $this->getPDOPassthrough(self::$describeTableData, 1);
        $drv = new MySQLDriver(['pdo' => $pdo]);
        $tbl = $drv->getTable('testDB1', 'ignored', 'testTbl1');
        $drv->setTable('ignored', 'ignored', $tbl);
        // Above should do nothing; No exec should happen.
        $this->assertNull($this->lastExecSQL);
        /* Trigger a create */
        $drv = new MySQLDriver(['pdo' => $this->getPDOPassthrough(false, 1)]);
        $drv->setTable('testDB2', 'ignored', $tbl);
        $this->assertEquals('CREATE TABLE `testDB2`.`testTbl1` (
`Id` INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
`Name` VARCHAR(9) NOT NULL DEFAULT "",
`Country` CHAR(8) NOT NULL DEFAULT "",
`District` TINYBLOB DEFAULT "",
`Population` DECIMAL(10, 2) NOT NULL DEFAULT "0",
`Average` FLOAT DEFAULT "",
`Update` TIMESTAMP NOT NULL DEFAULT "",
`Type` ENUM("foo") NOT NULL DEFAULT "foo"
) ENGINE=InnoDB DEFAULT CHARSET=utf8', $this->lastExecSQL);
        $drv = new MySQLDriver(['pdo' => $this->getPDOPassthrough(self::$describeTableData, 1)]);
        /* Drop a column and create a new */
        $tbl->columns[2]->name = "blah";
        /* Trigger a modification */
        $tbl->columns[3]->null = false;
        $tbl->columns[3]->type = new String(255, true, false);
        $tbl->columns[3]->default = "foo";
        /* Create a whole lot of columns. */
        $tbl->columns[] = new Column('tinyblob', new String(255, true, true));
        $tbl->columns[] = new Column('blob', new String(10000, true, true));
        $tbl->columns[] = new Column('mediumtext', new String(65537, true, false));
        $tbl->columns[] = new Column('longblob', new String(pow(2, 28), true, true));
        $tbl->columns[] = new Column('int7', new Integer(7));
        $tbl->columns[] = new Column('float7', new Float(7));
        $tbl->columns[] = new Column('dt1', new DateTime(true, false, false));
        $tbl->columns[] = new Column('dt2', new DateTime(false, true, true));
        $tbl->columns[] = new Column('bool', new Boolean());
        $tbl->columns[] = new Column('enum', new Enum('a', 'b', 'c'));
        /* The above columns produce the SQL below, which I believe match up. */
        // ALTER TABLE `testDB3`.`testTbl1`
        // DROP COLUMN `Country`,
        // ADD COLUMN `blah` CHAR(8) NOT NULL DEFAULT "",
        // ADD COLUMN `tinyblob` TINYBLOB,
        // ADD COLUMN `blob` BLOB,
        // ADD COLUMN `mediumtext` MEDIUMTEXT,
        // ADD COLUMN `longblob` LONGBLOB,
        // ADD COLUMN `int7` BIGINT,
        // ADD COLUMN `float7` DOUBLE,
        // ADD COLUMN `dt1` DATE,
        // ADD COLUMN `dt2` TIME,
        // ADD COLUMN `bool` TINYINT(1) UNSIGNED,
        // ADD COLUMN `enum` ENUM("a","b","c"),
        // MODIFY `District` TINYTEXT NOT NULL DEFAULT "foo"
        $drv->setTable('testDB3', 'ignored', $tbl);
        // Assert that a bunch of SQL happened.
        $this->assertEquals('ALTER TABLE `testDB3`.`testTbl1` DROP COLUMN `Country`,ADD COLUMN `blah` CHAR(8) NOT NULL DEFAULT "",ADD COLUMN `tinyblob` TINYBLOB,ADD COLUMN `blob` BLOB,ADD COLUMN `mediumtext` MEDIUMTEXT,ADD COLUMN `longblob` LONGBLOB,ADD COLUMN `int7` BIGINT,ADD COLUMN `float7` DOUBLE,ADD COLUMN `dt1` DATE,ADD COLUMN `dt2` TIME,ADD COLUMN `bool` TINYINT(1) UNSIGNED,ADD COLUMN `enum` ENUM("a","b","c"),MODIFY `District` TINYTEXT NOT NULL DEFAULT "foo"', $this->lastExecSQL);
        $tbl->columns = [new Column('invalid', null)];
        try {
            $drv->setTable('ignored', 'ignored', $tbl);
            $this->fail("Creating a column without a type should not work");
        } catch (DriverException $e) {
            /* Ignored */
        }
    }