|
4 | 4 | * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors |
5 | 5 | * SPDX-License-Identifier: AGPL-3.0-or-later |
6 | 6 | */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
7 | 10 | namespace OC\DB; |
8 | 11 |
|
9 | 12 | use Doctrine\DBAL\Exception; |
10 | 13 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
11 | 14 | use Doctrine\DBAL\Schema\Schema; |
| 15 | +use Doctrine\DBAL\Schema\Table; |
12 | 16 | use OCP\DB\ISchemaWrapper; |
| 17 | +use Override; |
13 | 18 |
|
14 | 19 | 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 | + ) { |
26 | 29 | if ($schema) { |
27 | 30 | $this->schema = $schema; |
28 | 31 | } else { |
29 | 32 | $this->schema = $this->connection->createSchema(); |
30 | 33 | } |
31 | 34 | } |
32 | 35 |
|
33 | | - public function getWrappedSchema() { |
| 36 | + public function getWrappedSchema(): Schema { |
34 | 37 | return $this->schema; |
35 | 38 | } |
36 | 39 |
|
37 | | - public function performDropTableCalls() { |
| 40 | + public function performDropTableCalls(): void { |
38 | 41 | foreach ($this->tablesToDelete as $tableName => $true) { |
39 | 42 | $this->connection->dropTable($tableName); |
40 | 43 | foreach ($this->connection->getShardConnections() as $shardConnection) { |
41 | 44 | $shardConnection->dropTable($tableName); |
42 | 45 | } |
43 | 46 | unset($this->tablesToDelete[$tableName]); |
44 | 47 | } |
| 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 | + } |
45 | 58 | } |
46 | 59 |
|
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(); |
55 | 65 | if (str_starts_with($tableName, $this->connection->getPrefix())) { |
56 | 66 | return substr($tableName, strlen($this->connection->getPrefix())); |
57 | 67 | } |
58 | 68 |
|
59 | 69 | return $tableName; |
60 | | - }, $tableNames); |
| 70 | + }, $tableNames)); |
61 | 71 | } |
62 | 72 |
|
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)); |
70 | 77 | } |
71 | 78 |
|
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) { |
79 | 81 | return $this->schema->getTable($this->connection->getPrefix() . $tableName); |
80 | 82 | } |
81 | 83 |
|
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 { |
90 | 86 | return $this->schema->hasTable($this->connection->getPrefix() . $tableName); |
91 | 87 | } |
92 | 88 |
|
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) { |
100 | 91 | unset($this->tablesToDelete[$tableName]); |
101 | 92 | return $this->schema->createTable($this->connection->getPrefix() . $tableName); |
102 | 93 | } |
103 | 94 |
|
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 { |
111 | 97 | $this->tablesToDelete[$tableName] = true; |
112 | | - return $this->schema->dropTable($this->connection->getPrefix() . $tableName); |
| 98 | + $this->schema->dropTable($this->connection->getPrefix() . $tableName); |
| 99 | + return $this; |
113 | 100 | } |
114 | 101 |
|
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 { |
121 | 104 | return $this->schema->getTables(); |
122 | 105 | } |
123 | 106 |
|
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 { |
132 | 109 | return $this->connection->getDatabasePlatform(); |
133 | 110 | } |
| 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 | + } |
134 | 121 | } |
0 commit comments