Also rewrite ALTER TABLE, handle int(...) properly
authorMax Semenik <maxsem@users.mediawiki.org>
Sun, 21 Feb 2010 09:40:27 +0000 (09:40 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Sun, 21 Feb 2010 09:40:27 +0000 (09:40 +0000)
includes/db/DatabaseSqlite.php
maintenance/tests/DatabaseSqliteTest.php

index 831479f..1ce725c 100644 (file)
@@ -518,7 +518,7 @@ class DatabaseSqlite extends DatabaseBase {
 
        protected function replaceVars( $s ) {
                $s = parent::replaceVars( $s );
-               if ( preg_match( '/^\s*CREATE TABLE/i', $s ) ) {
+               if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
                        // CREATE TABLE hacks to allow schema file sharing with MySQL
 
                        // binary/varbinary column type -> blob
@@ -526,7 +526,7 @@ class DatabaseSqlite extends DatabaseBase {
                        // no such thing as unsigned
                        $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
                        // INT -> INTEGER
-                       $s = preg_replace( '/\b(tiny|small|medium|big|)int\b/i', 'INTEGER', $s );
+                       $s = preg_replace( '/\b(tiny|small|medium|big|)int(\([\s\d]*\)|\b)/i', 'INTEGER', $s );
                        // varchar -> TEXT
                        $s = preg_replace( '/\bvarchar\(\d+\)/i', 'TEXT', $s );
                        // TEXT normalization
index 9eb07dd..011ef79 100644 (file)
@@ -36,9 +36,9 @@ class DatabaseSqliteTest extends PHPUnit_Framework_TestCase {
                $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 '');",
+                       . "foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
                        $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;" )
+                       foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
                        );
 
                $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
@@ -49,5 +49,9 @@ class DatabaseSqliteTest extends PHPUnit_Framework_TestCase {
                        $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
                        'Table name changed'
                        );
+               
+               $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
+                       $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42")
+                       );
        }
 }
\ No newline at end of file