Skip to content

Commit 6920654

Browse files
committed
feat: Add migration files for tree and multiple_tree tables with support for various database drivers.
1 parent 6d79343 commit 6920654

File tree

4 files changed

+134
-41
lines changed

4 files changed

+134
-41
lines changed

migrations/m250707_103609_tree.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m250707_103609_tree extends Migration
6+
{
7+
public function safeUp()
8+
{
9+
$primaryKey = $this->db->driverName === 'oci'
10+
? 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
11+
: $this->primaryKey();
12+
13+
$nameColumn = $this->db->driverName === 'oci'
14+
? $this->string(4000)->notNull()
15+
: $this->text()->notNull();
16+
17+
$this->createTable('{{%tree}}', [
18+
'id' => $primaryKey,
19+
'name' => $nameColumn,
20+
'lft' => $this->integer()->notNull(),
21+
'rgt' => $this->integer()->notNull(),
22+
'depth' => $this->integer()->notNull(),
23+
]);
24+
25+
$this->createIndex('idx_tree_lft', '{{%tree}}', 'lft');
26+
$this->createIndex('idx_tree_rgt', '{{%tree}}', 'rgt');
27+
$this->createIndex('idx_tree_depth', '{{%tree}}', 'depth');
28+
$this->createIndex('idx_tree_lft_rgt', '{{%tree}}', ['lft', 'rgt']);
29+
30+
if ($this->db->driverName !== 'sqlite') {
31+
$this->addCommentOnTable('{{%tree}}', 'Nested sets tree structure for hierarchical data');
32+
$this->addCommentOnColumn('{{%tree}}', 'id', 'Primary key of the tree node');
33+
$this->addCommentOnColumn('{{%tree}}', 'name', 'Name of the tree node');
34+
$this->addCommentOnColumn('{{%tree}}', 'lft', 'Left boundary of nested set');
35+
$this->addCommentOnColumn('{{%tree}}', 'rgt', 'Right boundary of nested set');
36+
$this->addCommentOnColumn('{{%tree}}', 'depth', 'Node depth in the tree hierarchy');
37+
}
38+
}
39+
40+
public function safeDown()
41+
{
42+
$this->dropTable('{{%tree}}');
43+
}
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m250707_104009_multiple_tree extends Migration
6+
{
7+
public function safeUp()
8+
{
9+
$primaryKey = $this->db->driverName === 'oci'
10+
? 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
11+
: $this->primaryKey();
12+
13+
$nameColumn = $this->db->driverName === 'oci'
14+
? $this->string(4000)->notNull()
15+
: $this->text()->notNull();
16+
17+
$this->createTable('{{%multiple_tree}}', [
18+
'id' => $primaryKey,
19+
'tree' => $this->integer()->null(),
20+
'name' => $nameColumn,
21+
'lft' => $this->integer()->notNull(),
22+
'rgt' => $this->integer()->notNull(),
23+
'depth' => $this->integer()->notNull(),
24+
]);
25+
26+
$this->createIndex('idx_multiple_tree_tree', '{{%multiple_tree}}', 'tree');
27+
$this->createIndex('idx_multiple_tree_lft', '{{%multiple_tree}}', 'lft');
28+
$this->createIndex('idx_multiple_tree_rgt', '{{%multiple_tree}}', 'rgt');
29+
$this->createIndex('idx_multiple_tree_depth', '{{%multiple_tree}}', 'depth');
30+
$this->createIndex('idx_multiple_tree_tree_lft_rgt', '{{%multiple_tree}}', ['tree', 'lft', 'rgt']);
31+
32+
if ($this->db->driverName !== 'sqlite') {
33+
$this->addCommentOnTable('{{%multiple_tree}}', 'Multiple nested sets tree structure for hierarchical data');
34+
$this->addCommentOnColumn('{{%multiple_tree}}', 'tree', 'Tree identifier for multiple trees support');
35+
$this->addCommentOnColumn('{{%multiple_tree}}', 'lft', 'Left boundary of nested set');
36+
$this->addCommentOnColumn('{{%multiple_tree}}', 'rgt', 'Right boundary of nested set');
37+
$this->addCommentOnColumn('{{%multiple_tree}}', 'depth', 'Node depth in the tree hierarchy');
38+
}
39+
}
40+
41+
public function safeDown()
42+
{
43+
$this->dropTable('{{%multiple_tree}}');
44+
}
45+
}

tests/TestCase.php

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use Yii;
1010
use yii\base\InvalidArgumentException;
1111
use yii\console\Application;
12-
use yii\db\{ActiveQuery, ActiveRecord, Connection, SchemaBuilderTrait};
12+
use yii\db\{ActiveQuery, ActiveRecord, Connection, Query, SchemaBuilderTrait};
1313
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree};
14+
use yii2\extensions\nestedsets\tests\support\stub\EchoMigrateController;
1415

1516
use function array_merge;
1617
use function array_values;
@@ -168,45 +169,8 @@ protected function buildFlatXMLDataSet(array $dataSet): string
168169

169170
protected function createDatabase(): void
170171
{
171-
$command = $this->getDb()->createCommand();
172-
173-
if ($this->getDb()->getTableSchema('tree', true) !== null) {
174-
$command->dropTable('tree')->execute();
175-
}
176-
177-
if ($this->getDb()->getTableSchema('multiple_tree', true) !== null) {
178-
$command->dropTable('multiple_tree')->execute();
179-
}
180-
181-
$primaryKey = $this->driverName === 'oci'
182-
? 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
183-
: $this->primaryKey()->notNull();
184-
$name = $this->driverName === 'oci'
185-
? $this->string()->notNull()
186-
: $this->text()->notNull();
187-
188-
$command->createTable(
189-
'tree',
190-
[
191-
'id' => $primaryKey,
192-
'name' => $name,
193-
'lft' => $this->integer()->notNull(),
194-
'rgt' => $this->integer()->notNull(),
195-
'depth' => $this->integer()->notNull(),
196-
],
197-
)->execute();
198-
199-
$command->createTable(
200-
'multiple_tree',
201-
[
202-
'id' => $primaryKey,
203-
'tree' => $this->integer(),
204-
'name' => $name,
205-
'lft' => $this->integer()->notNull(),
206-
'rgt' => $this->integer()->notNull(),
207-
'depth' => $this->integer()->notNull(),
208-
],
209-
)->execute();
172+
$this->runMigrate('down');
173+
$this->runMigrate('up');
210174
}
211175

212176
/**
@@ -261,7 +225,7 @@ protected function createTreeStructure(
261225

262226
protected function generateFixtureTree(): void
263227
{
264-
$this->createDatabase();
228+
$this->runMigrate('up');
265229

266230
$command = $this->getDb()->createCommand();
267231

@@ -334,6 +298,30 @@ protected function getDataSetMultipleTree(): array
334298
return array_values($dataSetMultipleTree);
335299
}
336300

301+
/**
302+
* @phpstan-param array<string, mixed> $params
303+
*/
304+
protected function runMigrate(string $action, array $params = []): mixed
305+
{
306+
$migrate = new EchoMigrateController(
307+
'migrate',
308+
Yii::$app,
309+
[
310+
'migrationPath' => dirname(__DIR__) . '/migrations',
311+
'interactive' => false,
312+
],
313+
);
314+
315+
ob_start();
316+
ob_implicit_flush(false);
317+
318+
$result = $migrate->run($action, $params);
319+
320+
ob_get_clean();
321+
322+
return $result;
323+
}
324+
337325
protected function loadFixtureXML(string $fileName): SimpleXMLElement
338326
{
339327
$filePath = "{$this->fixtureDirectory}/{$fileName}";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace yii2\extensions\nestedsets\tests\support\stub;
4+
5+
use yii\console\controllers\MigrateController;
6+
7+
final class EchoMigrateController extends MigrateController
8+
{
9+
public function stdout($string)
10+
{
11+
echo $string;
12+
13+
return true;
14+
}
15+
}

0 commit comments

Comments
 (0)