Skip to content

Commit ebfe694

Browse files
authoredNov 17, 2021
Fix drop table if exists for Oracle (#920)
1 parent 64d1d3c commit ebfe694

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed
 

‎src/Persistence/Sql/Exception.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Atk4\Data\Persistence\Sql;
66

7+
use Atk4\Data\Exception as BaseException;
8+
79
/**
810
* All exceptions generated by DSQL use this class.
911
*/
10-
class Exception extends \Atk4\Core\Exception
12+
class Exception extends BaseException
1113
{
1214
}

‎src/Schema/Migration.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Atk4\Data\Schema;
66

7-
use Atk4\Core\Exception;
7+
use Atk4\Data\Exception;
88
use Atk4\Data\Field;
99
use Atk4\Data\FieldSqlExpression;
1010
use Atk4\Data\Model;
@@ -98,7 +98,9 @@ public function create(): self
9898

9999
public function drop(): self
100100
{
101-
$this->getSchemaManager()->dropTable($this->getDatabasePlatform()->quoteSingleIdentifier($this->table->getName()));
101+
$this->getSchemaManager()
102+
->dropTable($this->getDatabasePlatform()->quoteSingleIdentifier($this->table->getName()));
103+
102104
$this->createdTableNames = array_diff($this->createdTableNames, [$this->table->getName()]);
103105

104106
return $this;
@@ -111,6 +113,19 @@ public function dropIfExists(): self
111113
} catch (\Doctrine\DBAL\Exception|\Doctrine\DBAL\DBALException $e) {
112114
}
113115

116+
$this->createdTableNames = array_diff($this->createdTableNames, [$this->table->getName()]);
117+
118+
// OracleSchemaManager::dropTable() called in self::drop() above tries to drop AI,
119+
// but if AI trigger is not present, AI sequence is not dropped
120+
// https://github.com/doctrine/dbal/issues/4997
121+
if ($this->getDatabasePlatform() instanceof OraclePlatform) {
122+
$dropTriggerSql = $this->getDatabasePlatform()->getDropAutoincrementSql($this->table->getName())[1];
123+
try {
124+
$this->connection->expr($dropTriggerSql)->execute();
125+
} catch (Exception $e) {
126+
}
127+
}
128+
114129
return $this;
115130
}
116131

@@ -152,6 +167,7 @@ public function id(string $name = 'id'): self
152167
$options = [
153168
'type' => 'integer',
154169
'ref_type' => self::REF_TYPE_PRIMARY,
170+
'mandatory' => true,
155171
];
156172

157173
$this->field($name, $options);

‎src/Schema/TestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public function setDb(array $dbData, bool $importData = true): void
130130
foreach ($dbData as $tableName => $data) {
131131
$migrator = $this->createMigrator()->table($tableName);
132132

133-
// drop table if exists but only if it was created during this test
133+
// drop table if already created but only if it was created during this test
134134
foreach ($this->createdMigrators as $migr) {
135135
if ($migr->connection === $this->db->connection) {
136136
foreach ($migr->getCreatedTableNames() as $t) {
137137
if ($t === $tableName) {
138-
$migrator->dropIfExists();
138+
$migrator->drop();
139139

140140
break 2;
141141
}

0 commit comments

Comments
 (0)
Please sign in to comment.