From a2449fea8638e0fab49521f4d15a2f9d497bc55a Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Thu, 12 Aug 2010 16:06:47 +0000 Subject: [PATCH] maintenance/sqlite.php: added the ability to check .sql files for SQLite compatibility --- includes/AutoLoader.php | 1 + maintenance/sqlite.inc | 67 ++++++++++++++++++++++++ maintenance/sqlite.php | 20 +++++++ maintenance/tests/DatabaseSqliteTest.php | 26 ++------- 4 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 maintenance/sqlite.inc diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 20fd5477f3..52b6bd57a2 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -670,6 +670,7 @@ $wgAutoloadLocalClasses = array( 'ParserTestStaticParserHook' => 'maintenance/parserTestsStaticParserHook.php', 'RemoteTestRecorder' => 'maintenance/parserTests.inc', 'SevenZipStream' => 'maintenance/7zip.inc', + 'Sqlite' => 'maintenance/sqlite.inc', 'TestFileIterator' => 'maintenance/parserTests.inc', 'TestRecorder' => 'maintenance/parserTests.inc', diff --git a/maintenance/sqlite.inc b/maintenance/sqlite.inc new file mode 100644 index 0000000000..c9201045e6 --- /dev/null +++ b/maintenance/sqlite.inc @@ -0,0 +1,67 @@ +sourceFile( $file ); + if ( $err != true ) { + return $err; + } + } + + $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ ); + foreach ( $tables as $table ) { + if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue; + + $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ ); + foreach ( $columns as $col ) { + if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) { + $db->close(); + return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'"; + } + } + } + } catch ( DBError $e ) { + return $e->getMessage(); + } + $db->close(); + return true; + } + }; \ No newline at end of file diff --git a/maintenance/sqlite.php b/maintenance/sqlite.php index ebaa69d99c..ef70d3222f 100644 --- a/maintenance/sqlite.php +++ b/maintenance/sqlite.php @@ -29,6 +29,7 @@ class SqliteMaintenance extends Maintenance { $this->addOption( 'vacuum', 'Clean up database by removing deleted pages. Decreases database file size' ); $this->addOption( 'integrity', 'Check database for integrity' ); $this->addOption( 'backup-to', 'Backup database to the given file', false, true ); + $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true ); } /** @@ -42,6 +43,11 @@ class SqliteMaintenance extends Maintenance { public function execute() { global $wgDBtype; + // Should work even if we use a non-SQLite database + if ( $this->hasOption( 'check-syntax' ) ) { + $this->checkSyntax(); + } + if ( $wgDBtype != 'sqlite' ) { $this->error( "This maintenance script requires a SQLite database.\n" ); return; @@ -107,6 +113,20 @@ class SqliteMaintenance extends Maintenance { $this->output( " Releasing lock...\n" ); $this->db->query( 'COMMIT TRANSACTION', __METHOD__ ); } + + private function checkSyntax() { + if ( !Sqlite::IsPresent() ) { + $this->error( "Error: SQLite support not found\n" ); + } + $files = array( $this->getOption( 'check-syntax' ) ); + $files += $this->mArgs; + $result = Sqlite::checkSqlSyntax( $files ); + if ( $result === true ) { + $this->output( "SQL syntax check: no errors detected.\n" ); + } else { + $this->error( "Error: $result\n" ); + } + } } $maintClass = "SqliteMaintenance"; diff --git a/maintenance/tests/DatabaseSqliteTest.php b/maintenance/tests/DatabaseSqliteTest.php index 9ba008468c..e70c3ac4ac 100644 --- a/maintenance/tests/DatabaseSqliteTest.php +++ b/maintenance/tests/DatabaseSqliteTest.php @@ -21,7 +21,7 @@ class DatabaseSqliteTest extends PHPUnit_Framework_TestCase { var $db; function setup() { - if ( !extension_loaded( 'pdo_sqlite' ) ) { + if ( !Sqlite::isPresent() ) { $this->markTestIncomplete( 'No SQLite support detected' ); } $this->db = new MockDatabaseSqlite(); @@ -62,27 +62,9 @@ class DatabaseSqliteTest extends PHPUnit_Framework_TestCase { function testEntireSchema() { global $IP; - $allowedTypes = array_flip( array( - 'integer', - 'real', - 'text', - 'blob', // NULL type is omitted intentionally - ) ); - - $db = new DatabaseSqliteStandalone( ':memory:' ); - $db->sourceFile( "$IP/maintenance/tables.sql" ); - - $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ ); - foreach ( $tables as $table ) { - if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue; - - $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ ); - foreach ( $columns as $col ) { - if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) { - $this->fail( "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'" ); - } - } + $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" ); + if ( $result !== true ) { + $this->fail( $result ); } - $db->close(); } } \ No newline at end of file -- 2.20.1