| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2016, ownCloud, Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Bart Visscher <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Christoph Wurst <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Jörn Friedrich Dreyer <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Lukas Reschke <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author Morris Jobke <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author Robin Appelman <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Roeland Jago Douma <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author Thomas Müller <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author Victor Dubiniuk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Vincent Petry <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @license AGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * along with this program. If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | namespace OC\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use Doctrine\DBAL\Platforms\MySQLPlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use Doctrine\DBAL\Platforms\OraclePlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use Doctrine\DBAL\Platforms\PostgreSQL94Platform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use Doctrine\DBAL\Platforms\SqlitePlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use Doctrine\DBAL\Schema\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | class MDB2SchemaManager { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** @var Connection $conn */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	protected $conn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @param Connection $conn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function __construct($conn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->conn = $conn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * Creates tables from XML file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @param string $file file to read structure from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * TODO: write more documentation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	public function createDbFromStructure($file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		return $this->executeSchemaChange($toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @return \OC\DB\Migrator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	public function getMigrator() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$random = \OC::$server->getSecureRandom(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$platform = $this->conn->getDatabasePlatform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$config = \OC::$server->getConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$dispatcher = \OC::$server->getEventDispatcher(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		if ($platform instanceof SqlitePlatform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			return new SQLiteMigrator($this->conn, $random, $config, $dispatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		} elseif ($platform instanceof OraclePlatform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			return new OracleMigrator($this->conn, $random, $config, $dispatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} elseif ($platform instanceof MySQLPlatform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			return new MySQLMigrator($this->conn, $random, $config, $dispatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		} elseif ($platform instanceof PostgreSQL94Platform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			return new Migrator($this->conn, $random, $config, $dispatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * Reads database schema from file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @param string $file file to read from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @return \Doctrine\DBAL\Schema\Schema | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 	private function readSchemaFromFile($file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 		$platform = $this->conn->getDatabasePlatform(); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 		$schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 		$toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 		return $schemaReader->loadSchemaFromFile($file, $toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * update the database scheme | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * @param string $file file to read structure from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * @param bool $generateSql only return the sql needed for the upgrade | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * @return string|boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	public function updateDbFromStructure($file, $generateSql = false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$toSchema = $this->readSchemaFromFile($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$migrator = $this->getMigrator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		if ($generateSql) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			return $migrator->generateChangeScript($toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$migrator->migrate($toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @param \Doctrine\DBAL\Schema\Schema $schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	public function generateChangeScript($schema) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$migrator = $this->getMigrator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		return $migrator->generateChangeScript($schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * remove all tables defined in a database structure xml file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @param string $file the xml file describing the tables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	public function removeDBStructure($file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		$schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		$fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$toSchema = clone $fromSchema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		foreach ($toSchema->getTables() as $table) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			$toSchema->dropTable($table->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		$comparator = new \Doctrine\DBAL\Schema\Comparator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$schemaDiff = $comparator->compare($fromSchema, $toSchema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$this->executeSchemaChange($schemaDiff); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	private function executeSchemaChange($schema) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$this->conn->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			$this->conn->query($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		$this->conn->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			$this->conn->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 			$this->conn->connect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  |