1<?php
2/**
3 * @author Morris Jobke <hey@morrisjobke.de>
4 * @author Robin Appelman <icewind@owncloud.com>
5 * @author Thomas Müller <thomas.mueller@tmit.eu>
6 * @author Victor Dubiniuk <dubiniuk@owncloud.com>
7 *
8 * @copyright Copyright (c) 2018, ownCloud GmbH
9 * @license AGPL-3.0
10 *
11 * This code is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License, version 3,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License, version 3,
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22 *
23 */
24
25namespace OC\DB;
26
27use Doctrine\DBAL\Schema\Schema;
28use Doctrine\DBAL\Schema\Table;
29
30class MySQLMigrator extends Migrator {
31	/**
32	 * @return Schema
33	 */
34	public function createSchema() {
35		$this->registerAdditionalMappings($this->connection);
36		return parent::createSchema();
37	}
38
39	/**
40	 * @param Schema $targetSchema
41	 * @param \Doctrine\DBAL\Connection $connection
42	 * @return \Doctrine\DBAL\Schema\SchemaDiff
43	 */
44	protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
45		$this->registerAdditionalMappings($connection);
46
47		$schemaDiff = parent::getDiff($targetSchema, $connection);
48
49		// identifiers need to be quoted for mysql
50		foreach ($schemaDiff->changedTables as $tableDiff) {
51			$tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name);
52			foreach ($tableDiff->changedColumns as $column) {
53				$column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName);
54			}
55		}
56
57		return $schemaDiff;
58	}
59
60	/**
61	 * @param \Doctrine\DBAL\Connection $connection
62	 */
63	private function registerAdditionalMappings(\Doctrine\DBAL\Connection $connection) {
64		$platform = $connection->getDatabasePlatform();
65		$platform->registerDoctrineTypeMapping('enum', 'string');
66		$platform->registerDoctrineTypeMapping('bit', 'string');
67	}
68}
69