Change OC_Migrate from MDB2 to Doctrine

This commit is contained in:
Bart Visscher 2013-11-22 15:53:56 +01:00
parent 60cfdae503
commit 9adff4f7f5
3 changed files with 26 additions and 107 deletions

View File

@ -44,7 +44,7 @@ class OC_DB {
/** /**
* @var \OC\DB\Connection $connection * @var \OC\DB\Connection $connection
*/ */
static private $connection; //the prefered connection to use, only Doctrine static private $connection; //the preferred connection to use, only Doctrine
static private $prefix=null; static private $prefix=null;
static private $type=null; static private $type=null;

View File

@ -38,7 +38,7 @@ class OC_Migrate{
// Array of temp files to be deleted after zip creation // Array of temp files to be deleted after zip creation
static private $tmpfiles=array(); static private $tmpfiles=array();
// Holds the db object // Holds the db object
static private $MDB2=false; static private $migration_database=false;
// Schema db object // Schema db object
static private $schema=false; static private $schema=false;
// Path to the sqlite db // Path to the sqlite db
@ -131,7 +131,7 @@ class OC_Migrate{
if( !self::connectDB() ) { if( !self::connectDB() ) {
return json_encode( array( 'success' => false ) ); return json_encode( array( 'success' => false ) );
} }
self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 ); self::$content = new OC_Migration_Content( self::$zip, self::$migration_database );
// Export the app info // Export the app info
$exportdata = self::exportAppData(); $exportdata = self::exportAppData();
// Add the data dir to the zip // Add the data dir to the zip
@ -358,24 +358,6 @@ class OC_Migrate{
return $to; return $to;
} }
/**
* @brief connects to a MDB2 database scheme
* @returns bool
*/
static private function connectScheme() {
// We need a mdb2 database connection
self::$MDB2->loadModule( 'Manager' );
self::$MDB2->loadModule( 'Reverse' );
// Connect if this did not happen before
if( !self::$schema ) {
require_once 'MDB2/Schema.php';
self::$schema=MDB2_Schema::factory( self::$MDB2 );
}
return true;
}
/** /**
* @brief creates a migration.db in the users data dir with their app data in * @brief creates a migration.db in the users data dir with their app data in
* @return bool whether operation was successfull * @return bool whether operation was successfull
@ -463,47 +445,15 @@ class OC_Migrate{
return false; return false;
} }
// Already connected // Already connected
if(!self::$MDB2) { if(!self::$migration_database) {
require_once 'MDB2.php';
$datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
$connectionParams = array(
// DB type 'path' => self::$dbpath,
if( class_exists( 'SQLite3' ) ) { 'driver' => 'pdo_sqlite',
$dbtype = 'sqlite3';
} else if( is_callable( 'sqlite_open' ) ) {
$dbtype = 'sqlite';
} else {
OC_Log::write( 'migration', 'SQLite not found', OC_Log::ERROR );
return false;
}
// Prepare options array
$options = array(
'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE),
'log_line_break' => '<br>',
'idxname_format' => '%s',
'debug' => true,
'quote_identifier' => true
);
$dsn = array(
'phptype' => $dbtype,
'database' => self::$dbpath,
'mode' => '0644'
); );
// Try to establish connection // Try to establish connection
self::$MDB2 = MDB2::factory( $dsn, $options ); self::$migration_database = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
// Die if we could not connect
if( PEAR::isError( self::$MDB2 ) ) {
die( self::$MDB2->getMessage() );
OC_Log::write( 'migration', 'Failed to create/connect to migration.db', OC_Log::FATAL );
OC_Log::write( 'migration', self::$MDB2->getUserInfo(), OC_Log::FATAL );
OC_Log::write( 'migration', self::$MDB2->getMessage(), OC_Log::FATAL );
return false;
}
// We always, really always want associative arrays
self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC);
} }
return true; return true;
@ -515,10 +465,7 @@ class OC_Migrate{
* @return bool whether the operation was successful * @return bool whether the operation was successful
*/ */
static private function createAppTables( $appid ) { static private function createAppTables( $appid ) {
$schema_manager = new OC\DB\MDB2SchemaManager(self::$migration_database);
if( !self::connectScheme() ) {
return false;
}
// There is a database.xml file // There is a database.xml file
$content = file_get_contents(OC_App::getAppPath($appid) . '/appinfo/database.xml' ); $content = file_get_contents(OC_App::getAppPath($appid) . '/appinfo/database.xml' );
@ -538,29 +485,16 @@ class OC_Migrate{
file_put_contents( $file2, $content ); file_put_contents( $file2, $content );
// Try to create tables // Try to create tables
$definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); try {
$schema_manager->createDbFromStructure($file2);
unlink( $file2 ); } catch(Exception $e) {
unlink( $file2 );
// Die in case something went wrong
if( $definition instanceof MDB2_Schema_Error ) {
OC_Log::write( 'migration', 'Failed to parse database.xml for: '.$appid, OC_Log::FATAL );
OC_Log::write( 'migration', $definition->getMessage().': '.$definition->getUserInfo(), OC_Log::FATAL );
return false;
}
$definition['overwrite'] = true;
$ret = self::$schema->createDatabase( $definition );
// Die in case something went wrong
if( $ret instanceof MDB2_Error ) {
OC_Log::write( 'migration', 'Failed to create tables for: '.$appid, OC_Log::FATAL ); OC_Log::write( 'migration', 'Failed to create tables for: '.$appid, OC_Log::FATAL );
OC_Log::write( 'migration', $ret->getMessage().': '.$ret->getUserInfo(), OC_Log::FATAL ); OC_Log::write( 'migration', $e->getMessage(), OC_Log::FATAL );
return false; return false;
} }
return $tables;
return $tables;
} }
/** /**
@ -646,7 +580,7 @@ class OC_Migrate{
if( !self::connectDB( $db ) ) { if( !self::connectDB( $db ) ) {
return false; return false;
} }
$content = new OC_Migration_Content( self::$zip, self::$MDB2 ); $content = new OC_Migration_Content( self::$zip, self::$migration_database );
$provider->setData( self::$uid, $content, $info ); $provider->setData( self::$uid, $content, $info );
// Then do the import // Then do the import
if( !$appsstatus[$id] = $provider->import( $info->apps->$id, $importinfo ) ) { if( !$appsstatus[$id] = $provider->import( $info->apps->$id, $importinfo ) ) {

View File

@ -27,7 +27,7 @@
class OC_Migration_Content{ class OC_Migration_Content{
private $zip=false; private $zip=false;
// Holds the MDB2 object // Holds the database object
private $db=null; private $db=null;
// Holds an array of tmpfiles to delete after zip creation // Holds an array of tmpfiles to delete after zip creation
private $tmpfiles=array(); private $tmpfiles=array();
@ -35,7 +35,7 @@ class OC_Migration_Content{
/** /**
* @brief sets up the * @brief sets up the
* @param $zip ZipArchive object * @param $zip ZipArchive object
* @param optional $db a MDB2 database object (required for exporttype user) * @param optional $db a database object (required for exporttype user)
* @return bool * @return bool
*/ */
public function __construct( $zip, $db=null ) { public function __construct( $zip, $db=null ) {
@ -64,16 +64,7 @@ class OC_Migration_Content{
// Optimize the query // Optimize the query
$query = $this->db->prepare( $query ); $query = $this->db->prepare( $query );
// Die if we have an error (error means: bad query, not 0 results!) return $query;
if( PEAR::isError( $query ) ) {
$entry = 'DB Error: "'.$query->getMessage().'"<br />';
$entry .= 'Offending command was: '.$query.'<br />';
OC_Log::write( 'migration', $entry, OC_Log::FATAL );
return false;
} else {
return $query;
}
} }
/** /**
@ -156,20 +147,14 @@ class OC_Migration_Content{
$sql .= $valuessql . " )"; $sql .= $valuessql . " )";
// Make the query // Make the query
$query = $this->prepare( $sql ); $query = $this->prepare( $sql );
if( !$query ) { $query->execute( $values );
OC_Log::write( 'migration', 'Invalid sql produced: '.$sql, OC_Log::FATAL ); // Do we need to return some values?
return false; if( array_key_exists( 'idcol', $options ) ) {
exit(); // Yes we do
$return[] = $row[$options['idcol']];
} else { } else {
$query->execute( $values ); // Take a guess and return the first field :)
// Do we need to return some values? $return[] = reset($row);
if( array_key_exists( 'idcol', $options ) ) {
// Yes we do
$return[] = $row[$options['idcol']];
} else {
// Take a guess and return the first field :)
$return[] = reset($row);
}
} }
$fields = ''; $fields = '';
$values = ''; $values = '';