From: Max Semenik Date: Sat, 20 Feb 2010 20:00:28 +0000 (+0000) Subject: DatabaseSqlite: X-Git-Tag: 1.31.0-rc.0~37683 X-Git-Url: http://git.cyclocoop.org/%22%2C%20generer_url_ecrire%28?a=commitdiff_plain;h=83323b3e1353b25023184d0a97e06409fb3e6999;p=lhc%2Fweb%2Fwiklou.git DatabaseSqlite: * Tweaks and fixes to DDL conversion from MySQL. Now everything in tables.sql converts cleanly to SQLite's native type hints. * Handle error in duplicateTableStructure(). --- diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 1ecb3a0d38..831479fdec 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -522,19 +522,29 @@ class DatabaseSqlite extends DatabaseBase { // CREATE TABLE hacks to allow schema file sharing with MySQL // binary/varbinary column type -> blob - $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'blob\1', $s ); + $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s ); // no such thing as unsigned - $s = preg_replace( '/\bunsigned\b/i', '', $s ); - // INT -> INTEGER for primary keys - $s = preg_replacE( '/\bint\b/i', 'integer', $s ); + $s = preg_replace( '/\b(un)?signed\b/i', '', $s ); + // INT -> INTEGER + $s = preg_replace( '/\b(tiny|small|medium|big|)int\b/i', 'INTEGER', $s ); + // varchar -> TEXT + $s = preg_replace( '/\bvarchar\(\d+\)/i', 'TEXT', $s ); + // TEXT normalization + $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s ); + // BLOB normalization + $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s ); + // BOOL -> INTEGER + $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s ); + // DATETIME -> TEXT + $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s ); // No ENUM type - $s = preg_replace( '/enum\([^)]*\)/i', 'blob', $s ); + $s = preg_replace( '/enum\([^)]*\)/i', 'BLOB', $s ); // binary collation type -> nothing $s = preg_replace( '/\bbinary\b/i', '', $s ); // auto_increment -> autoincrement - $s = preg_replace( '/\bauto_increment\b/i', 'autoincrement', $s ); + $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s ); // No explicit options - $s = preg_replace( '/\)[^)]*$/', ')', $s ); + $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s ); } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) { // No truncated indexes $s = preg_replace( '/\(\d+\)/', '', $s ); @@ -553,7 +563,11 @@ class DatabaseSqlite extends DatabaseBase { function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) { $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name='$oldName' AND type='table'", $fname ); - $sql = $this->fetchObject( $res )->sql; + $obj = $this->fetchObject( $res ); + if ( !$obj ) { + throw new MWException( "Couldn't retrieve structure for table $oldName" ); + } + $sql = $obj->sql; $sql = preg_replace( '/\b' . preg_quote( $oldName ) . '\b/', $newName, $sql, 1 ); return $this->query( $sql, $fname ); } diff --git a/maintenance/tests/DatabaseSqliteTest.php b/maintenance/tests/DatabaseSqliteTest.php new file mode 100644 index 0000000000..9eb07dd23a --- /dev/null +++ b/maintenance/tests/DatabaseSqliteTest.php @@ -0,0 +1,53 @@ +lastQuery = $sql; + return true; + } + + function replaceVars( $s ) { + return parent::replaceVars( $s ); + } +} + +class DatabaseSqliteTest extends PHPUnit_Framework_TestCase { + var $db; + + function setup() { + if ( !extension_loaded( 'pdo_sqlite' ) ) { + $this->markTestIncomplete( 'No SQLite support detected' ); + } + $this->db = new MockDatabaseSqlite(); + } + + function replaceVars( $sql ) { + // normalize spacing to hide implementation details + return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) ); + } + + function testReplaceVars() { + $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" ); + + $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " + . "foo_name TEXT NOT NULL DEFAULT '');", + $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, + foo_name varchar(255) binary NOT NULL DEFAULT '') ENGINE=MyISAM;" ) + ); + + $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );", + $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" ) + ); + + $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );", + $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ), + 'Table name changed' + ); + } +} \ No newline at end of file