Unverified Commit 491a7b1e authored by Thomas Müller's avatar Thomas Müller
Browse files

Adding schema methods to IDbConnection

parent 4ad984a5
......@@ -26,6 +26,7 @@
namespace OC\AppFramework\Db;
use Doctrine\DBAL\Schema\Schema;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDb;
use OCP\IDBConnection;
......@@ -299,4 +300,23 @@ class Db implements IDb {
public function escapeLikeParameter($param) {
return $this->connection->escapeLikeParameter($param);
}
/**
* Create the schema of the connected database
*
* @return Schema
* @since 10.0.0
*/
public function createSchema() {
return $this->connection->createSchema();
}
/**
* Migrate the database to the given schema
*
* @param Schema $toSchema
*/
public function migrateToSchema(Schema $toSchema) {
return $this->connection->migrateToSchema($toSchema);
}
}
......@@ -33,10 +33,12 @@ use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Exception\ConstraintViolationException;
use Doctrine\DBAL\Schema\Schema;
use OC\DB\QueryBuilder\QueryBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\PreConditionNotMetException;
use OCP\Util;
class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
/**
......@@ -162,7 +164,7 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
$statement = $this->adapter->fixupStatement($statement);
if(\OC::$server->getSystemConfig()->getValue( 'log_query', false)) {
\OCP\Util::writeLog('core', 'DB prepare : '.$statement, \OCP\Util::DEBUG);
Util::writeLog('core', 'DB prepare : '.$statement, Util::DEBUG);
}
return parent::prepare($statement);
}
......@@ -402,4 +404,27 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
public function escapeLikeParameter($param) {
return addcslashes($param, '\\_%');
}
/**
* Create the schema of the connected database
*
* @return Schema
*/
public function createSchema() {
$schemaManager = new MDB2SchemaManager($this);
$migrator = $schemaManager->getMigrator();
return $migrator->createSchema();
}
/**
* Migrate the database to the given schema
*
* @param Schema $toSchema
*/
public function migrateToSchema(Schema $toSchema) {
$schemaManager = new MDB2SchemaManager($this);
$migrator = $schemaManager->getMigrator();
$migrator->migrate($toSchema);
}
}
......@@ -83,9 +83,6 @@ class ConnectionFactory {
];
}
// set default table creation options
$result['defaultTableOptions'] = ['collate' => 'utf8_bin'];
return $result;
}
......@@ -187,6 +184,12 @@ class ConnectionFactory {
$connectionParams['driverOptions'] = $driverOptions;
}
// set default table creation options
$connectionParams['defaultTableOptions'] = [
'collate' => 'utf8_bin',
'tablePrefix' => $connectionParams['tablePrefix']
];
return $connectionParams;
}
}
<?php
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2016, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\DB;
use Doctrine\DBAL\Migrations\Finder\MigrationFinderInterface;
use Doctrine\DBAL\Migrations\OutputWriter;
use Doctrine\DBAL\Platforms\OraclePlatform;
class MigrationConfiguration extends \Doctrine\DBAL\Migrations\Configuration\Configuration {
function __construct(Connection $connection, OutputWriter $outputWriter = null, MigrationFinderInterface $finder = null) {
parent::__construct($connection, $outputWriter, $finder);
$this->setMigrationsTableName($this->getMigrationsTableName());
$this->setMigrationsColumnName($this->getMigrationsColumnName());
}
public function setMigrationsColumnName($columnName) {
$platform = $this->getConnection()->getDatabasePlatform();
if ($platform instanceof OraclePlatform) {
$columnName = $platform->quoteIdentifier($columnName);
}
parent::setMigrationsColumnName($columnName);
}
public function setMigrationsTableName($tableName) {
$platform = $this->getConnection()->getDatabasePlatform();
if ($platform instanceof OraclePlatform) {
$tableName = $platform->quoteIdentifier($tableName);
}
parent::setMigrationsTableName($tableName);
}
}
......@@ -42,14 +42,10 @@ use Symfony\Component\EventDispatcher\GenericEvent;
class Migrator {
/**
* @var \Doctrine\DBAL\Connection $connection
*/
/** @var \Doctrine\DBAL\Connection $connection */
protected $connection;
/**
* @var ISecureRandom
*/
/** @var ISecureRandom */
private $random;
/** @var IConfig */
......@@ -196,6 +192,12 @@ class Migrator {
return new Table($newName, $table->getColumns(), $newIndexes, [], 0, $table->getOptions());
}
public function createSchema() {
$filterExpression = $this->getFilterExpression();
$this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
return $this->connection->getSchemaManager()->createSchema();
}
/**
* @param Schema $targetSchema
* @param \Doctrine\DBAL\Connection $connection
......@@ -216,8 +218,7 @@ class Migrator {
}
$filterExpression = $this->getFilterExpression();
$this->connection->getConfiguration()->
setFilterSchemaAssetsExpression($filterExpression);
$this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
$sourceSchema = $connection->getSchemaManager()->createSchema();
// remove tables we don't know about
......
......@@ -33,6 +33,7 @@
// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP;
use Doctrine\DBAL\Schema\Schema;
use OCP\DB\QueryBuilder\IQueryBuilder;
/**
......@@ -250,4 +251,20 @@ interface IDBConnection {
* @since 9.0.0
*/
public function escapeLikeParameter($param);
/**
* Create the schema of the connected database
*
* @return Schema
* @since 10.0
*/
public function createSchema();
/**
* Migrate the database to the given schema
*
* @param Schema $toSchema
* @since 10.0
*/
public function migrateToSchema(Schema $toSchema);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment