Skip to content

Commit e5342b7

Browse files
committed
feat(ISchemaWrapper): Add method to remove an autoincrement and cleanup
Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
1 parent 6766d0a commit e5342b7

File tree

2 files changed

+78
-85
lines changed

2 files changed

+78
-85
lines changed

lib/private/DB/SchemaWrapper.php

Lines changed: 60 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,131 +4,118 @@
44
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: AGPL-3.0-or-later
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace OC\DB;
811

912
use Doctrine\DBAL\Exception;
1013
use Doctrine\DBAL\Platforms\AbstractPlatform;
1114
use Doctrine\DBAL\Schema\Schema;
15+
use Doctrine\DBAL\Schema\Table;
1216
use OCP\DB\ISchemaWrapper;
17+
use Override;
1318

1419
class SchemaWrapper implements ISchemaWrapper {
15-
/** @var Connection */
16-
protected $connection;
17-
18-
/** @var Schema */
19-
protected $schema;
20-
21-
/** @var array */
22-
protected $tablesToDelete = [];
23-
24-
public function __construct(Connection $connection, ?Schema $schema = null) {
25-
$this->connection = $connection;
20+
protected Schema $schema;
21+
protected array $tablesToDelete = [];
22+
/** @var list<string> */
23+
protected $autoincrementsToDelete = [];
24+
25+
public function __construct(
26+
protected readonly Connection $connection,
27+
?Schema $schema = null,
28+
) {
2629
if ($schema) {
2730
$this->schema = $schema;
2831
} else {
2932
$this->schema = $this->connection->createSchema();
3033
}
3134
}
3235

33-
public function getWrappedSchema() {
36+
public function getWrappedSchema(): Schema {
3437
return $this->schema;
3538
}
3639

37-
public function performDropTableCalls() {
40+
public function performDropTableCalls(): void {
3841
foreach ($this->tablesToDelete as $tableName => $true) {
3942
$this->connection->dropTable($tableName);
4043
foreach ($this->connection->getShardConnections() as $shardConnection) {
4144
$shardConnection->dropTable($tableName);
4245
}
4346
unset($this->tablesToDelete[$tableName]);
4447
}
48+
49+
// Also delete auto-increments
50+
$platform = $this->getDatabasePlatform();
51+
if ($platform instanceof OraclePlatform) {
52+
foreach ($this->autoincrementsToDelete as $tableName) {
53+
/** @psalm-suppress InternalMethod */
54+
$platform->getDropAutoincrementSql($tableName);
55+
}
56+
$this->autoincrementsToDelete = [];
57+
}
4558
}
4659

47-
/**
48-
* Gets all table names
49-
*
50-
* @return array
51-
*/
52-
public function getTableNamesWithoutPrefix() {
53-
$tableNames = $this->schema->getTableNames();
54-
return array_map(function ($tableName) {
60+
#[Override]
61+
public function getTableNamesWithoutPrefix(): array {
62+
$tableNames = $this->schema->getTables();
63+
return array_values(array_map(function (Table $table): string {
64+
$tableName = $table->getName();
5565
if (str_starts_with($tableName, $this->connection->getPrefix())) {
5666
return substr($tableName, strlen($this->connection->getPrefix()));
5767
}
5868

5969
return $tableName;
60-
}, $tableNames);
70+
}, $tableNames));
6171
}
6272

63-
// Overwritten methods
64-
65-
/**
66-
* @return array
67-
*/
68-
public function getTableNames() {
69-
return $this->schema->getTableNames();
73+
#[Override]
74+
public function getTableNames(): array {
75+
$tableNames = $this->schema->getTables();
76+
return array_values(array_map(fn (Table $table): string => $table->getName(), $tableNames));
7077
}
7178

72-
/**
73-
* @param string $tableName
74-
*
75-
* @return \Doctrine\DBAL\Schema\Table
76-
* @throws \Doctrine\DBAL\Schema\SchemaException
77-
*/
78-
public function getTable($tableName) {
79+
#[Override]
80+
public function getTable(string $tableName) {
7981
return $this->schema->getTable($this->connection->getPrefix() . $tableName);
8082
}
8183

82-
/**
83-
* Does this schema have a table with the given name?
84-
*
85-
* @param string $tableName
86-
*
87-
* @return boolean
88-
*/
89-
public function hasTable($tableName) {
84+
#[Override]
85+
public function hasTable(string $tableName): bool {
9086
return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
9187
}
9288

93-
/**
94-
* Creates a new table.
95-
*
96-
* @param string $tableName
97-
* @return \Doctrine\DBAL\Schema\Table
98-
*/
99-
public function createTable($tableName) {
89+
#[Override]
90+
public function createTable(string $tableName) {
10091
unset($this->tablesToDelete[$tableName]);
10192
return $this->schema->createTable($this->connection->getPrefix() . $tableName);
10293
}
10394

104-
/**
105-
* Drops a table from the schema.
106-
*
107-
* @param string $tableName
108-
* @return \Doctrine\DBAL\Schema\Schema
109-
*/
110-
public function dropTable($tableName) {
95+
#[Override]
96+
public function dropTable(string $tableName): self {
11197
$this->tablesToDelete[$tableName] = true;
112-
return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
98+
$this->schema->dropTable($this->connection->getPrefix() . $tableName);
99+
return $this;
113100
}
114101

115-
/**
116-
* Gets all tables of this schema.
117-
*
118-
* @return \Doctrine\DBAL\Schema\Table[]
119-
*/
120-
public function getTables() {
102+
#[Override]
103+
public function getTables(): array {
121104
return $this->schema->getTables();
122105
}
123106

124-
/**
125-
* Gets the DatabasePlatform for the database.
126-
*
127-
* @return AbstractPlatform
128-
*
129-
* @throws Exception
130-
*/
131-
public function getDatabasePlatform() {
107+
#[Override]
108+
public function getDatabasePlatform(): AbstractPlatform {
132109
return $this->connection->getDatabasePlatform();
133110
}
111+
112+
#[Override]
113+
public function dropAutoincrement(string $tableName, string $columnName): void {
114+
if ($this->schema->hasTable($tableName)) {
115+
$table = $this->schema->getTable($tableName);
116+
$table->modifyColumn($columnName, ['autoincrement' => false]);
117+
}
118+
119+
$this->autoincrementsToDelete[] = $tableName;
120+
}
134121
}

lib/public/DB/ISchemaWrapper.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Doctrine\DBAL\Exception;
1010
use Doctrine\DBAL\Platforms\AbstractPlatform;
11+
use OCP\AppFramework\Attribute\Consumable;
1112

1213
/**
1314
* This interface allows to get information about the database schema.
@@ -19,6 +20,7 @@
1920
*
2021
* @since 13.0.0
2122
*/
23+
#[Consumable(since: '13.0.0')]
2224
interface ISchemaWrapper {
2325
/**
2426
* @param string $tableName
@@ -27,17 +29,16 @@ interface ISchemaWrapper {
2729
* @throws \Doctrine\DBAL\Schema\SchemaException
2830
* @since 13.0.0
2931
*/
30-
public function getTable($tableName);
32+
public function getTable(string $tableName);
3133

3234
/**
3335
* Does this schema have a table with the given name?
3436
*
3537
* @param string $tableName Prefix is automatically prepended
3638
*
37-
* @return boolean
3839
* @since 13.0.0
3940
*/
40-
public function hasTable($tableName);
41+
public function hasTable(string $tableName): bool;
4142

4243
/**
4344
* Creates a new table.
@@ -46,40 +47,39 @@ public function hasTable($tableName);
4647
* @return \Doctrine\DBAL\Schema\Table
4748
* @since 13.0.0
4849
*/
49-
public function createTable($tableName);
50+
public function createTable(string $tableName);
5051

5152
/**
5253
* Drops a table from the schema.
5354
*
5455
* @param string $tableName Prefix is automatically prepended
55-
* @return \Doctrine\DBAL\Schema\Schema
5656
* @since 13.0.0
5757
*/
58-
public function dropTable($tableName);
58+
public function dropTable(string $tableName): self;
5959

6060
/**
6161
* Gets all tables of this schema.
6262
*
6363
* @return \Doctrine\DBAL\Schema\Table[]
6464
* @since 13.0.0
6565
*/
66-
public function getTables();
66+
public function getTables(): array;
6767

6868
/**
6969
* Gets all table names, prefixed with table prefix
7070
*
71-
* @return array
71+
* @return list<string>
7272
* @since 13.0.0
7373
*/
74-
public function getTableNames();
74+
public function getTableNames(): array;
7575

7676
/**
7777
* Gets all table names
7878
*
79-
* @return array
79+
* @return list<string>
8080
* @since 13.0.0
8181
*/
82-
public function getTableNamesWithoutPrefix();
82+
public function getTableNamesWithoutPrefix(): array;
8383

8484
/**
8585
* Gets the DatabasePlatform for the database.
@@ -89,5 +89,11 @@ public function getTableNamesWithoutPrefix();
8989
* @throws Exception
9090
* @since 23.0.0
9191
*/
92-
public function getDatabasePlatform();
92+
public function getDatabasePlatform(): AbstractPlatform;
93+
94+
/**
95+
* Drop auto-increment on a table to port it to snowflake-ids.
96+
* @since 33.0.0
97+
*/
98+
public function dropAutoincrement(string $tableName, string $columnName): void;
9399
}

0 commit comments

Comments
 (0)