From ad8d55c3274e81dd4fd92b41ac5d6ef5c39febf3 Mon Sep 17 00:00:00 2001 From: tbelau666 Date: Sun, 30 Nov 2014 23:17:09 +0100 Subject: Use Doctrines filter by table name Doctrine's SchemaManager can filter table names by regular expression. On this way it picks up only ownClouds's tables in a database. by tbelau666 --- lib/private/db/mdb2schemamanager.php | 4 +--- lib/private/db/mdb2schemawriter.php | 8 ++++++-- lib/private/db/migrator.php | 6 +++++- lib/private/db/pgsqltools.php | 3 +++ 4 files changed, 15 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/private/db/mdb2schemamanager.php b/lib/private/db/mdb2schemamanager.php index 78267094d0e..d90c8525a7b 100644 --- a/lib/private/db/mdb2schemamanager.php +++ b/lib/private/db/mdb2schemamanager.php @@ -36,9 +36,7 @@ class MDB2SchemaManager { * TODO: write more documentation */ public function getDbStructure($file, $mode = MDB2_SCHEMA_DUMP_STRUCTURE) { - $sm = $this->conn->getSchemaManager(); - - return \OC_DB_MDB2SchemaWriter::saveSchemaToFile($file, $sm); + return \OC_DB_MDB2SchemaWriter::saveSchemaToFile($file, $this->conn); } /** diff --git a/lib/private/db/mdb2schemawriter.php b/lib/private/db/mdb2schemawriter.php index a2a62a81475..3c91f3c784a 100644 --- a/lib/private/db/mdb2schemawriter.php +++ b/lib/private/db/mdb2schemawriter.php @@ -13,13 +13,17 @@ class OC_DB_MDB2SchemaWriter { * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $sm * @return bool */ - static public function saveSchemaToFile($file, $sm) { + static public function saveSchemaToFile($file, $conn) { $xml = new SimpleXMLElement(''); $xml->addChild('name', OC_Config::getValue( "dbname", "owncloud" )); $xml->addChild('create', 'true'); $xml->addChild('overwrite', 'false'); $xml->addChild('charset', 'utf8'); - foreach ($sm->listTables() as $table) { + + $conn->getConfiguration()-> + setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix'.'/')); + + foreach ($conn->getSchemaManager()->listTables() as $table) { self::saveTable($table, $xml->addChild('table')); } file_put_contents($file, $xml->asXML()); diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 31c648a9b65..903e9b8a716 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -69,7 +69,9 @@ class Migrator { * @var \Doctrine\DBAL\Schema\Table[] $tables */ $tables = $targetSchema->getTables(); - + + $this->connection->getConfiguration()-> + setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix').'/'); $existingTables = $this->connection->getSchemaManager()->listTableNames(); foreach ($tables as $table) { @@ -153,6 +155,8 @@ class Migrator { } protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { + $connection->getConfiguration()-> + setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix').'/'); $sourceSchema = $connection->getSchemaManager()->createSchema(); // remove tables we don't know about diff --git a/lib/private/db/pgsqltools.php b/lib/private/db/pgsqltools.php index c3ac140594d..6d17f8f40f8 100644 --- a/lib/private/db/pgsqltools.php +++ b/lib/private/db/pgsqltools.php @@ -21,6 +21,9 @@ class PgSqlTools { */ public function resynchronizeDatabaseSequences(Connection $conn) { $databaseName = $conn->getDatabase(); + $conn->getConfiguration()-> + setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix').'/'); + foreach ($conn->getSchemaManager()->listSequences() as $sequence) { $sequenceName = $sequence->getName(); $sqlInfo = 'SELECT table_schema, table_name, column_name -- cgit v1.2.3 From de25084defa361e6d85af1d469b4b59e8a69c694 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 22 Dec 2014 10:43:56 +0100 Subject: inject \OCP\IConfig instance in migrator --- lib/private/db/mdb2schemamanager.php | 12 ++++++------ lib/private/db/migrator.php | 8 +++++++- lib/private/db/sqlitemigrator.php | 16 ---------------- 3 files changed, 13 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/private/db/mdb2schemamanager.php b/lib/private/db/mdb2schemamanager.php index d90c8525a7b..358360d0b46 100644 --- a/lib/private/db/mdb2schemamanager.php +++ b/lib/private/db/mdb2schemamanager.php @@ -58,19 +58,19 @@ class MDB2SchemaManager { public function getMigrator() { $random = \OC::$server->getSecureRandom()->getMediumStrengthGenerator(); $platform = $this->conn->getDatabasePlatform(); + $config = \OC::$server->getConfig(); if ($platform instanceof SqlitePlatform) { - $config = \OC::$server->getConfig(); return new SQLiteMigrator($this->conn, $random, $config); } else if ($platform instanceof OraclePlatform) { - return new OracleMigrator($this->conn, $random); + return new OracleMigrator($this->conn, $random, $config); } else if ($platform instanceof MySqlPlatform) { - return new MySQLMigrator($this->conn, $random); + return new MySQLMigrator($this->conn, $random, $config); } else if ($platform instanceof SQLServerPlatform) { - return new MsSqlMigrator($this->conn, $random); + return new MsSqlMigrator($this->conn, $random, $config); } else if ($platform instanceof PostgreSqlPlatform) { - return new Migrator($this->conn, $random); + return new Migrator($this->conn, $random, $config); } else { - return new NoCheckMigrator($this->conn, $random); + return new NoCheckMigrator($this->conn, $random, $config); } } diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 903e9b8a716..3a178f387ed 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -14,6 +14,7 @@ use \Doctrine\DBAL\Schema\Table; use \Doctrine\DBAL\Schema\Schema; use \Doctrine\DBAL\Schema\SchemaConfig; use \Doctrine\DBAL\Schema\Comparator; +use OCP\IConfig; use OCP\Security\ISecureRandom; class Migrator { @@ -28,13 +29,18 @@ class Migrator { */ private $random; + /** @var IConfig */ + protected $config; + /** * @param \Doctrine\DBAL\Connection $connection * @param ISecureRandom $random + * @param IConfig $config */ - public function __construct(\Doctrine\DBAL\Connection $connection, ISecureRandom $random) { + public function __construct(\Doctrine\DBAL\Connection $connection, ISecureRandom $random, IConfig $config) { $this->connection = $connection; $this->random = $random; + $this->config = $config; } /** diff --git a/lib/private/db/sqlitemigrator.php b/lib/private/db/sqlitemigrator.php index 848e4986571..42b65634645 100644 --- a/lib/private/db/sqlitemigrator.php +++ b/lib/private/db/sqlitemigrator.php @@ -10,25 +10,9 @@ namespace OC\DB; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Schema\Schema; -use OCP\Security\ISecureRandom; class SQLiteMigrator extends Migrator { - /** - * @var \OCP\IConfig - */ - private $config; - - /** - * @param \Doctrine\DBAL\Connection $connection - * @param ISecureRandom $random - * @param \OCP\IConfig $config - */ - public function __construct(\Doctrine\DBAL\Connection $connection, ISecureRandom $random, \OCP\IConfig $config) { - parent::__construct($connection, $random); - $this->config = $config; - } - /** * @param \Doctrine\DBAL\Schema\Schema $targetSchema * @throws \OC\DB\MigrationException -- cgit v1.2.3 From dbc465de973660ec0530967cd3ae54aeacab70ee Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 22 Dec 2014 10:44:30 +0100 Subject: use injected config object and fix typos --- core/command/db/converttype.php | 2 +- lib/private/db/mdb2schemawriter.php | 14 ++++++++------ lib/private/db/migrator.php | 6 +++--- lib/private/db/pgsqltools.php | 14 +++++++++++++- 4 files changed, 25 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/core/command/db/converttype.php b/core/command/db/converttype.php index 92ca9e41ebe..b4d1f0e2a1a 100644 --- a/core/command/db/converttype.php +++ b/core/command/db/converttype.php @@ -266,7 +266,7 @@ class ConvertType extends Command { $this->copyTable($fromDB, $toDB, $table, $input, $output); } if ($input->getArgument('type') === 'pgsql') { - $tools = new \OC\DB\PgSqlTools; + $tools = new \OC\DB\PgSqlTools($this->config); $tools->resynchronizeDatabaseSequences($toDB); } // save new database config diff --git a/lib/private/db/mdb2schemawriter.php b/lib/private/db/mdb2schemawriter.php index 3c91f3c784a..aa6d578e9b5 100644 --- a/lib/private/db/mdb2schemawriter.php +++ b/lib/private/db/mdb2schemawriter.php @@ -10,19 +10,21 @@ class OC_DB_MDB2SchemaWriter { /** * @param string $file - * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $sm + * @param \OC\DB\Connection $conn * @return bool */ - static public function saveSchemaToFile($file, $conn) { + static public function saveSchemaToFile($file, \OC\DB\Connection $conn) { + $config = \OC::$server->getConfig(); + $xml = new SimpleXMLElement(''); - $xml->addChild('name', OC_Config::getValue( "dbname", "owncloud" )); + $xml->addChild('name', $config->getSystemValue('dbname', 'owncloud')); $xml->addChild('create', 'true'); $xml->addChild('overwrite', 'false'); $xml->addChild('charset', 'utf8'); - + $conn->getConfiguration()-> - setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix'.'/')); - + setFilterSchemaAssetsExpression('/^' . $config->getSystemValue('dbtableprefix') . '/'); + foreach ($conn->getSchemaManager()->listTables() as $table) { self::saveTable($table, $xml->addChild('table')); } diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 3a178f387ed..aecbcf418d3 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -75,9 +75,9 @@ class Migrator { * @var \Doctrine\DBAL\Schema\Table[] $tables */ $tables = $targetSchema->getTables(); - + $this->connection->getConfiguration()-> - setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix').'/'); + setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix') . '/'); $existingTables = $this->connection->getSchemaManager()->listTableNames(); foreach ($tables as $table) { @@ -162,7 +162,7 @@ class Migrator { protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { $connection->getConfiguration()-> - setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix').'/'); + setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix') . '/'); $sourceSchema = $connection->getSchemaManager()->createSchema(); // remove tables we don't know about diff --git a/lib/private/db/pgsqltools.php b/lib/private/db/pgsqltools.php index 6d17f8f40f8..3896009add4 100644 --- a/lib/private/db/pgsqltools.php +++ b/lib/private/db/pgsqltools.php @@ -8,11 +8,23 @@ */ namespace OC\DB; +use OCP\IConfig; /** * Various PostgreSQL specific helper functions. */ class PgSqlTools { + + /** @var \OCP\IConfig */ + private $config; + + /** + * @param \OCP\IConfig $config + */ + public function __construct(IConfig $config) { + $this->config = $config; + } + /** * @brief Resynchronizes all sequences of a database after using INSERTs * without leaving out the auto-incremented column. @@ -22,7 +34,7 @@ class PgSqlTools { public function resynchronizeDatabaseSequences(Connection $conn) { $databaseName = $conn->getDatabase(); $conn->getConfiguration()-> - setFilterSchemaAssetsExpression('/^'.\OCP\Config::getSystemValue('dbtableprefix').'/'); + setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix') . '/'); foreach ($conn->getSchemaManager()->listSequences() as $sequence) { $sequenceName = $sequence->getName(); -- cgit v1.2.3 From 95374e1404e6342dcd34d40bcb4d8acb197bf831 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 23 Dec 2014 13:32:25 +0100 Subject: add default for dbtableprefix --- core/command/db/converttype.php | 2 +- lib/private/db/mdb2schemawriter.php | 2 +- lib/private/db/migrator.php | 4 ++-- lib/private/db/pgsqltools.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/core/command/db/converttype.php b/core/command/db/converttype.php index b4d1f0e2a1a..8d1560b0511 100644 --- a/core/command/db/converttype.php +++ b/core/command/db/converttype.php @@ -229,7 +229,7 @@ class ConvertType extends Command { protected function getTables(Connection $db) { $db->getConfiguration()-> - setFilterSchemaAssetsExpression('/^'.$this->config->getSystemValue('dbtableprefix').'/'); + setFilterSchemaAssetsExpression('/^'.$this->config->getSystemValue('dbtableprefix', 'oc_').'/'); return $db->getSchemaManager()->listTableNames(); } diff --git a/lib/private/db/mdb2schemawriter.php b/lib/private/db/mdb2schemawriter.php index aa6d578e9b5..a42cd86ba54 100644 --- a/lib/private/db/mdb2schemawriter.php +++ b/lib/private/db/mdb2schemawriter.php @@ -23,7 +23,7 @@ class OC_DB_MDB2SchemaWriter { $xml->addChild('charset', 'utf8'); $conn->getConfiguration()-> - setFilterSchemaAssetsExpression('/^' . $config->getSystemValue('dbtableprefix') . '/'); + setFilterSchemaAssetsExpression('/^' . $config->getSystemValue('dbtableprefix', 'oc_') . '/'); foreach ($conn->getSchemaManager()->listTables() as $table) { self::saveTable($table, $xml->addChild('table')); diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index aecbcf418d3..8ccc02e36a5 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -77,7 +77,7 @@ class Migrator { $tables = $targetSchema->getTables(); $this->connection->getConfiguration()-> - setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix') . '/'); + setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix', 'oc_') . '/'); $existingTables = $this->connection->getSchemaManager()->listTableNames(); foreach ($tables as $table) { @@ -162,7 +162,7 @@ class Migrator { protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { $connection->getConfiguration()-> - setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix') . '/'); + setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix', 'oc_') . '/'); $sourceSchema = $connection->getSchemaManager()->createSchema(); // remove tables we don't know about diff --git a/lib/private/db/pgsqltools.php b/lib/private/db/pgsqltools.php index 3896009add4..f3204d4c7b6 100644 --- a/lib/private/db/pgsqltools.php +++ b/lib/private/db/pgsqltools.php @@ -34,7 +34,7 @@ class PgSqlTools { public function resynchronizeDatabaseSequences(Connection $conn) { $databaseName = $conn->getDatabase(); $conn->getConfiguration()-> - setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix') . '/'); + setFilterSchemaAssetsExpression('/^' . $this->config->getSystemValue('dbtableprefix', 'oc_') . '/'); foreach ($conn->getSchemaManager()->listSequences() as $sequence) { $sequenceName = $sequence->getName(); -- cgit v1.2.3