Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve: Bug: Inversed reference require CASCADE #53 #54

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function buildJunction(ManyToManyRelation $relation):MigrationModel
continue;
}
$this->migration
->addUpCode($this->recordBuilder->addFk($fkName, $tableAlias, $fkCol, $refTable, $refCol))
->addUpCode($this->recordBuilder->addFk($fkName, $tableAlias, $fkCol, $refTable, $refCol, 'CASCADE'))
->addDownCode($this->recordBuilder->dropFk($fkName, $tableAlias));
$this->migration->dependencies[] = $refTable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function up()
'tag_id' => $this->bigInteger()->notNull(),
]);
$this->addPrimaryKey('pk_post_id_tag_id', '{{%posts2tags}}', 'post_id,tag_id');
$this->addForeignKey('fk_posts2tags_post_id_v2_posts_id', '{{%posts2tags}}', 'post_id', '{{%v2_posts}}', 'id');
$this->addForeignKey('fk_posts2tags_tag_id_v2_tags_id', '{{%posts2tags}}', 'tag_id', '{{%v2_tags}}', 'id');
$this->addForeignKey('fk_posts2tags_post_id_v2_posts_id', '{{%posts2tags}}', 'post_id', '{{%v2_posts}}', 'id', 'CASCADE');
$this->addForeignKey('fk_posts2tags_tag_id_v2_tags_id', '{{%posts2tags}}', 'tag_id', '{{%v2_tags}}', 'id', 'CASCADE');
}

public function down()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function up()
'tag_id' => $this->bigInteger()->notNull(),
]);
$this->addPrimaryKey('pk_post_id_tag_id', '{{%posts2tags}}', 'post_id,tag_id');
$this->addForeignKey('fk_posts2tags_post_id_v2_posts_id', '{{%posts2tags}}', 'post_id', '{{%v2_posts}}', 'id');
$this->addForeignKey('fk_posts2tags_tag_id_v2_tags_id', '{{%posts2tags}}', 'tag_id', '{{%v2_tags}}', 'id');
$this->addForeignKey('fk_posts2tags_post_id_v2_posts_id', '{{%posts2tags}}', 'post_id', '{{%v2_posts}}', 'id', 'CASCADE');
$this->addForeignKey('fk_posts2tags_tag_id_v2_tags_id', '{{%posts2tags}}', 'tag_id', '{{%v2_tags}}', 'id', 'CASCADE');
}

public function down()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function safeUp()
'tag_id' => $this->bigInteger()->notNull(),
]);
$this->addPrimaryKey('pk_post_id_tag_id', '{{%posts2tags}}', 'post_id,tag_id');
$this->addForeignKey('fk_posts2tags_post_id_v2_posts_id', '{{%posts2tags}}', 'post_id', '{{%v2_posts}}', 'id');
$this->addForeignKey('fk_posts2tags_tag_id_v2_tags_id', '{{%posts2tags}}', 'tag_id', '{{%v2_tags}}', 'id');
$this->addForeignKey('fk_posts2tags_post_id_v2_posts_id', '{{%posts2tags}}', 'post_id', '{{%v2_posts}}', 'id', 'CASCADE');
$this->addForeignKey('fk_posts2tags_tag_id_v2_tags_id', '{{%posts2tags}}', 'tag_id', '{{%v2_tags}}', 'id', 'CASCADE');
}

public function safeDown()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/53_bug_inversed_reference_require_cascade/index.yml',
'generateUrls' => false,
'generateModels' => false,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => true,
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
];

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
openapi: 3.0.3

info:
title: '53_bug_inversed_reference_require_cascade'
version: 1.0.0

components:
schemas:
Document:
title: Document
properties:
id:
type: integer
labels:
type: array
readOnly: true
description: Inversed reference for detect junction table documents2labels
items:
$ref: '#/components/schemas/Label'

Label:
title: Label
properties:
id:
type: integer
documents:
type: array
readOnly: true
description: Inversed reference for detect junction table documents2labels
items:
$ref: '#/components/schemas/Document'

paths:
'/':
get:
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Table for Document
*/
class m200000_000000_create_table_documents extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%documents}}', [
'id' => $this->primaryKey(),
]);
}

public function down()
{
$this->dropTable('{{%documents}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Table for Label
*/
class m200000_000001_create_table_labels extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%labels}}', [
'id' => $this->primaryKey(),
]);
}

public function down()
{
$this->dropTable('{{%labels}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Table for Documents2Labels
*/
class m200000_000002_create_table_documents2labels extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%documents2labels}}', [
'document_id' => $this->integer()->notNull(),
'label_id' => $this->integer()->notNull(),
]);
$this->addPrimaryKey('pk_document_id_label_id', '{{%documents2labels}}', 'document_id,label_id');
$this->addForeignKey('fk_documents2labels_document_id_documents_id', '{{%documents2labels}}', 'document_id', '{{%documents}}', 'id', 'CASCADE');
$this->addForeignKey('fk_documents2labels_label_id_labels_id', '{{%documents2labels}}', 'label_id', '{{%labels}}', 'id', 'CASCADE');
}

public function down()
{
$this->dropForeignKey('fk_documents2labels_label_id_labels_id', '{{%documents2labels}}');
$this->dropForeignKey('fk_documents2labels_document_id_documents_id', '{{%documents2labels}}');
$this->dropPrimaryKey('pk_document_id_label_id', '{{%documents2labels}}');
$this->dropTable('{{%documents2labels}}');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function up()
'tag_id' => $this->bigInteger()->notNull(),
]);
$this->addPrimaryKey('pk_post_id_tag_id', '{{%posts2tags}}', 'post_id,tag_id');
$this->addForeignKey('fk_posts2tags_post_id_posts_id', '{{%posts2tags}}', 'post_id', '{{%posts}}', 'id');
$this->addForeignKey('fk_posts2tags_tag_id_tags_id', '{{%posts2tags}}', 'tag_id', '{{%tags}}', 'id');
$this->addForeignKey('fk_posts2tags_post_id_posts_id', '{{%posts2tags}}', 'post_id', '{{%posts}}', 'id', 'CASCADE');
$this->addForeignKey('fk_posts2tags_tag_id_tags_id', '{{%posts2tags}}', 'tag_id', '{{%tags}}', 'id', 'CASCADE');
}

public function down()
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,18 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim()
]);
$this->checkFiles($actualFiles, $expectedFiles);
}

// https://github.com/php-openapi/yii2-openapi/issues/53
public function test53BugInversedReferenceRequireCascade()
{
$testFile = Yii::getAlias("@specs/issue_fix/53_bug_inversed_reference_require_cascade/index.php");
$this->runGenerator($testFile);
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
'recursive' => true,
]);
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/53_bug_inversed_reference_require_cascade/mysql"), [
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
}
}
Loading