f182208d098c6a49b6da36351c788b92c0a608e5
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();
31 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
32 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
36 private function replaceVars( $sql ) {
37 // normalize spacing to hide implementation details
38 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
41 public function testReplaceVars() {
42 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
44 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
45 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
46 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
47 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
50 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
51 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
54 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
55 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
58 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
59 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
63 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
64 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
66 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
67 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
70 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
71 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
74 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
75 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
79 public function testTableName() {
81 $db = new DatabaseSqliteStandalone( ':memory:' );
82 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
83 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
84 $db->tablePrefix( 'foo' );
85 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
86 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
89 public function testDuplicateTableStructure() {
90 $db = new DatabaseSqliteStandalone( ':memory:' );
91 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
93 $db->duplicateTableStructure( 'foo', 'bar' );
94 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
95 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
96 'Normal table duplication'
99 $db->duplicateTableStructure( 'foo', 'baz', true );
100 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
101 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
102 'Creation of temporary duplicate'
104 $this->assertEquals( 0,
105 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
106 'Create a temporary duplicate only'
110 public function testDuplicateTableStructureVirtual() {
111 $db = new DatabaseSqliteStandalone( ':memory:' );
112 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
113 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
115 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
117 $db->duplicateTableStructure( 'foo', 'bar' );
118 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
119 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
120 'Duplication of virtual tables'
123 $db->duplicateTableStructure( 'foo', 'baz', true );
124 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
125 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
126 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
130 public function testEntireSchema() {
133 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
134 if ( $result !== true ) {
135 $this->fail( $result );
140 * Runs upgrades of older databases and compares results with current schema
141 * @todo: currently only checks list of tables
143 public function testUpgrades() {
146 $versions = array( '1.13', '1.15', '1.16', '1.17' ); // SQLite wasn't included in 1.14
147 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
148 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
149 $currentTables = $this->getTables( $currentDB );
150 sort( $currentTables );
152 foreach ( $versions as $version ) {
153 $db = $this->prepareDB( $version );
154 $tables = $this->getTables( $db );
155 $this->assertEquals( $currentTables, $tables );
160 private function prepareDB( $version ) {
161 static $maint = null;
162 if ( $maint === null ) {
163 $maint = new FakeMaintenance();
164 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
167 $db = new DatabaseSqliteStandalone( ':memory:' );
168 $db->sourceFile( dirname( __FILE__
) . "/sqlite/tables-$version.sql" );
169 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
170 $updater->doUpdates( array( 'core' ) );
174 protected function getTables( $db ) {
175 $list = array_flip( $db->listTables() );
177 'math', // moved out of core in 1.18
179 'searchindex_content',
180 'searchindex_segments',
181 'searchindex_segdir',
183 'searchindex_docsize',
186 foreach ( $excluded as $t ) {
189 $list = array_flip( $list );