Skip to content

Commit b7d6b94

Browse files
authored
Merge pull request #29 from SOHELAHMED7/132-create-migration-for-drop-table-if-a-entire-schema-is-deleted-from-open-api-spec
Resolve - Create migration for drop table if a entire schema is deleted from OpenAPI spec
2 parents cbae5c5 + 2976125 commit b7d6b94

File tree

188 files changed

+1745
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+1745
-341
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,24 @@ Provide custom database table column name in case of relationship column. This w
309309
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
310310
```
311311
312+
313+
### `x-deleted-schemas`
314+
315+
This is root level key used to generate "drop table" migration for the deleted component schema. If a component schema (DB model) is removed from OpenAPI spec then its following entities should be also deleted from the code:
316+
317+
- DB table (migrations)
318+
- model
319+
- faker
320+
321+
So to generate appropriate migration for the removed schema, explicitly setting schema name or schema name + custom table name is required in this key. Only then the migrations will be generated. It should be set as:
322+
323+
```yaml
324+
x-deleted-schemas:
325+
- Fruit # Example: table name is evaluated to `itt_fruits`, if `itt_` is prefix set in DB config
326+
- Mango: the_mango_table_name # custom table name; see `x-table` in README.md
327+
```
328+
329+
312330
## Many-to-Many relation definition
313331
314332
There are two ways for define many-to-many relations:

src/generator/ApiGenerator.php

+24-20
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace cebe\yii2openapi\generator;
99

10-
use yii\db\mysql\Schema as MySqlSchema;
11-
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
12-
use yii\db\pgsql\Schema as PgSqlSchema;
10+
use cebe\openapi\exceptions\IOException;
11+
use cebe\openapi\exceptions\TypeErrorException;
12+
use cebe\openapi\exceptions\UnresolvableReferenceException;
1313
use cebe\openapi\Reader;
1414
use cebe\openapi\spec\OpenApi;
1515
use cebe\yii2openapi\lib\Config;
@@ -22,7 +22,10 @@
2222
use cebe\yii2openapi\lib\generators\UrlRulesGenerator;
2323
use cebe\yii2openapi\lib\PathAutoCompletion;
2424
use cebe\yii2openapi\lib\SchemaToDatabase;
25+
use Exception;
2526
use Yii;
27+
use yii\db\mysql\Schema as MySqlSchema;
28+
use yii\db\pgsql\Schema as PgSqlSchema;
2629
use yii\gii\CodeFile;
2730
use yii\gii\Generator;
2831
use yii\helpers\Html;
@@ -176,7 +179,7 @@ class ApiGenerator extends Generator
176179
private $_openApiWithoutRef;
177180

178181
/**
179-
* @var \cebe\yii2openapi\lib\Config $config
182+
* @var Config $config
180183
**/
181184
private $config;
182185

@@ -283,11 +286,11 @@ public function rules()
283286

284287
/**
285288
* @param $attribute
286-
* @throws \cebe\openapi\exceptions\IOException
287-
* @throws \cebe\openapi\exceptions\TypeErrorException
288-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
289+
* @throws IOException
290+
* @throws TypeErrorException
291+
* @throws UnresolvableReferenceException
289292
*/
290-
public function validateSpec($attribute):void
293+
public function validateSpec($attribute): void
291294
{
292295
if ($this->ignoreSpecErrors) {
293296
return;
@@ -299,7 +302,7 @@ public function validateSpec($attribute):void
299302
}
300303
}
301304

302-
public function validateUrlPrefixes($attribute):void
305+
public function validateUrlPrefixes($attribute): void
303306
{
304307
if (empty($this->urlPrefixes)) {
305308
return;
@@ -427,7 +430,7 @@ public function stickyAttributes()
427430
);
428431
}
429432

430-
public function makeConfig():Config
433+
public function makeConfig(): Config
431434
{
432435
if (!$this->config) {
433436
$props = get_object_vars($this);
@@ -457,11 +460,12 @@ public function makeConfig():Config
457460
* Please refer to [[\yii\gii\generators\controller\Generator::generate()]] as an example
458461
* on how to implement this method.
459462
* @return CodeFile[] a list of code files to be created.
460-
* @throws \Exception
463+
* @throws Exception
461464
*/
462-
public function generate():array
465+
public function generate(): array
463466
{
464467
$config = $this->makeConfig();
468+
465469
$actionsGenerator = $this->useJsonApi
466470
? Yii::createObject(JsonActionGenerator::class, [$config])
467471
: Yii::createObject(RestActionGenerator::class, [$config]);
@@ -489,12 +493,12 @@ public function generate():array
489493
}
490494

491495
/**
492-
* @return \cebe\openapi\spec\OpenApi
493-
* @throws \cebe\openapi\exceptions\IOException
494-
* @throws \cebe\openapi\exceptions\TypeErrorException
495-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
496+
* @return OpenApi
497+
* @throws IOException
498+
* @throws TypeErrorException
499+
* @throws UnresolvableReferenceException
496500
*/
497-
protected function getOpenApiWithoutReferences():OpenApi
501+
protected function getOpenApiWithoutReferences(): OpenApi
498502
{
499503
if ($this->_openApiWithoutRef === null) {
500504
$file = Yii::getAlias($this->openApiPath);
@@ -507,17 +511,17 @@ protected function getOpenApiWithoutReferences():OpenApi
507511
return $this->_openApiWithoutRef;
508512
}
509513

510-
public static function isPostgres():bool
514+
public static function isPostgres(): bool
511515
{
512516
return Yii::$app->db->schema instanceof PgSqlSchema;
513517
}
514518

515-
public static function isMysql():bool
519+
public static function isMysql(): bool
516520
{
517521
return (Yii::$app->db->schema instanceof MySqlSchema && !static::isMariaDb());
518522
}
519523

520-
public static function isMariaDb():bool
524+
public static function isMariaDb(): bool
521525
{
522526
return strpos(Yii::$app->db->schema->getServerVersion(), 'MariaDB') !== false;
523527
}

0 commit comments

Comments
 (0)