http://www.mediawiki.org/wiki/Special:Code/MediaWiki/62752

Revision: 62752
Author:   maxsem
Date:     2010-02-20 20:00:28 +0000 (Sat, 20 Feb 2010)

Log Message:
-----------
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().

Modified Paths:
--------------
    trunk/phase3/includes/db/DatabaseSqlite.php

Added Paths:
-----------
    trunk/phase3/maintenance/tests/DatabaseSqliteTest.php

Modified: trunk/phase3/includes/db/DatabaseSqlite.php
===================================================================
--- trunk/phase3/includes/db/DatabaseSqlite.php 2010-02-20 19:47:24 UTC (rev 
62751)
+++ trunk/phase3/includes/db/DatabaseSqlite.php 2010-02-20 20:00:28 UTC (rev 
62752)
@@ -522,19 +522,29 @@
                        // 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 @@
 
        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 );
        }

Added: trunk/phase3/maintenance/tests/DatabaseSqliteTest.php
===================================================================
--- trunk/phase3/maintenance/tests/DatabaseSqliteTest.php                       
        (rev 0)
+++ trunk/phase3/maintenance/tests/DatabaseSqliteTest.php       2010-02-20 
20:00:28 UTC (rev 62752)
@@ -0,0 +1,53 @@
+<?php
+
+class MockDatabaseSqlite extends DatabaseSqliteStandalone {
+       var $lastQuery;
+
+       function __construct( ) {
+               parent::__construct( '' );
+       }
+
+       function query( $sql, $fname = '', $tempIgnore = false ) {
+               $this->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


Property changes on: trunk/phase3/maintenance/tests/DatabaseSqliteTest.php
___________________________________________________________________
Added: svn:eol-style
   + native



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to