3 class MockDatabaseSqlite
extends DatabaseSqliteStandalone
{
6 function __construct( ) {
7 parent
::__construct( ':memory:' );
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery
= $sql;
15 function replaceVars( $s ) {
16 return parent
::replaceVars( $s );
23 class DatabaseSqliteTest
extends MediaWikiTestCase
{
26 public function setUp() {
27 if ( !Sqlite
::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
30 $this->db
= new MockDatabaseSqlite();
33 private function replaceVars( $sql ) {
34 // normalize spacing to hide implementation details
35 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
38 public function testReplaceVars() {
39 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
41 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
42 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
43 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
44 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
47 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
48 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
51 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
52 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
55 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
56 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
60 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
61 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
63 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
64 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
67 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
68 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
71 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
72 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
76 public function testTableName() {
78 $db = new DatabaseSqliteStandalone( ':memory:' );
79 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
80 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
81 $db->tablePrefix( 'foo' );
82 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
83 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
86 public function testDuplicateTableStructure() {
87 $db = new DatabaseSqliteStandalone( ':memory:' );
88 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
90 $db->duplicateTableStructure( 'foo', 'bar' );
91 $this->assertEquals( 'CREATE TABLE bar(foo, barfoo)',
92 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
93 'Normal table duplication'
96 $db->duplicateTableStructure( 'foo', 'baz', true );
97 $this->assertEquals( 'CREATE TABLE baz(foo, barfoo)',
98 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
99 'Creation of temporary duplicate'
101 $this->assertEquals( 0,
102 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
103 'Create a temporary duplicate only'
107 public function testDuplicateTableStructureVirtual() {
108 $db = new DatabaseSqliteStandalone( ':memory:' );
109 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
110 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
112 $db->query( 'CREATE VIRTUAL TABLE foo USING FTS3(foobar)' );
114 $db->duplicateTableStructure( 'foo', 'bar' );
115 $this->assertEquals( 'CREATE VIRTUAL TABLE bar USING FTS3(foobar)',
116 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
117 'Duplication of virtual tables'
120 $db->duplicateTableStructure( 'foo', 'baz', true );
121 $this->assertEquals( 'CREATE VIRTUAL TABLE baz USING FTS3(foobar)',
122 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
123 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
127 function testEntireSchema() {
130 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
131 if ( $result !== true ) {
132 $this->fail( $result );