Index: trunk/phase3/tests/phpunit/includes/db/DatabaseSqliteTest.php |
— | — | @@ -75,7 +75,48 @@ |
76 | 76 | $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) ); |
77 | 77 | $this->assertEquals( 'foobar', $db->tableName( 'bar' ) ); |
78 | 78 | } |
| 79 | + |
| 80 | + public function testDuplicateTableStructure() { |
| 81 | + $db = new DatabaseSqliteStandalone( ':memory:' ); |
| 82 | + $db->query( 'CREATE TABLE foo(foo, barfoo)' ); |
79 | 83 | |
| 84 | + $db->duplicateTableStructure( 'foo', 'bar' ); |
| 85 | + $this->assertEquals( 'CREATE TABLE bar(foo, barfoo)', |
| 86 | + $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ), |
| 87 | + 'Normal table duplication' |
| 88 | + ); |
| 89 | + |
| 90 | + $db->duplicateTableStructure( 'foo', 'baz', true ); |
| 91 | + $this->assertEquals( 'CREATE TABLE baz(foo, barfoo)', |
| 92 | + $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ), |
| 93 | + 'Creation of temporary duplicate' |
| 94 | + ); |
| 95 | + $this->assertEquals( 0, |
| 96 | + $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ), |
| 97 | + 'Create a temporary duplicate only' |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + public function testDuplicateTableStructureVirtual() { |
| 102 | + $db = new DatabaseSqliteStandalone( ':memory:' ); |
| 103 | + if ( $db->getFulltextSearchModule() != 'FTS3' ) { |
| 104 | + $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' ); |
| 105 | + } |
| 106 | + $db->query( 'CREATE VIRTUAL TABLE foo USING FTS3(foobar)' ); |
| 107 | + |
| 108 | + $db->duplicateTableStructure( 'foo', 'bar' ); |
| 109 | + $this->assertEquals( 'CREATE VIRTUAL TABLE bar USING FTS3(foobar)', |
| 110 | + $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ), |
| 111 | + 'Duplication of virtual tables' |
| 112 | + ); |
| 113 | + |
| 114 | + $db->duplicateTableStructure( 'foo', 'baz', true ); |
| 115 | + $this->assertEquals( 'CREATE VIRTUAL TABLE baz USING FTS3(foobar)', |
| 116 | + $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ), |
| 117 | + "Can't create temporary virtual tables, should fall back to non-temporary duplication" |
| 118 | + ); |
| 119 | + } |
| 120 | + |
80 | 121 | function testEntireSchema() { |
81 | 122 | global $IP; |
82 | 123 | |
Index: trunk/phase3/includes/db/DatabaseSqlite.php |
— | — | @@ -601,11 +601,12 @@ |
602 | 602 | } |
603 | 603 | $sql = $obj->sql; |
604 | 604 | $sql = preg_replace( '/\b' . preg_quote( $oldName ) . '\b/', $newName, $sql, 1 ); |
605 | | - if ( $temporary && strpos( $oldName, 'searchindex' ) === false ) { |
606 | | - # For some reason TEMPORARY TABLE doesn't work with searchindex |
607 | | - # We need to explicitly look for searchindex rather than VIRTUAL |
608 | | - # because we don't want to clone the FTS subtables either |
609 | | - $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql ); |
| 605 | + if ( $temporary ) { |
| 606 | + if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/', $sql ) ) { |
| 607 | + wfDebug( "Table $oldName is virtual, can't create a temporary duplicate.\n" ); |
| 608 | + } else { |
| 609 | + $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql ); |
| 610 | + } |
610 | 611 | } |
611 | 612 | return $this->query( $sql, $fname ); |
612 | 613 | } |