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 - Create migration for drop table if a entire schema is deleted from OpenAPI spec #29

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
40e7c8f
Initial commit of this PR
SOHELAHMED7 Jul 7, 2023
186045d
Add failing test
SOHELAHMED7 Jul 7, 2023
242ecde
WIP
SOHELAHMED7 Jul 10, 2023
1a6a516
Merge branches 'master' and '132-create-migration-for-drop-table-if-a…
SOHELAHMED7 May 30, 2024
c489f22
Design: WIP
SOHELAHMED7 May 30, 2024
d4e0d88
WIP
SOHELAHMED7 May 31, 2024
be52231
WIP
SOHELAHMED7 May 31, 2024
6c7e792
WIP
SOHELAHMED7 Jun 4, 2024
8d047c5
Create attributes from column schemas and more - WIP
SOHELAHMED7 Jun 12, 2024
9d901c9
Create delete file (migration) containing important DB statements for…
SOHELAHMED7 Jun 13, 2024
6427a61
Cleanup
SOHELAHMED7 Jun 13, 2024
aa9ca71
Fix style
SOHELAHMED7 Jun 13, 2024
46ef387
Generate foreign keys in migrations - WIP
SOHELAHMED7 Jun 13, 2024
67cc5a0
Create PK using Yii's in-built method
SOHELAHMED7 Jun 19, 2024
c205866
Create PK using Yii's in-built method - more concrete
SOHELAHMED7 Jun 20, 2024
2d10ff4
Fix https://github.com/php-openapi/yii2-openapi/issues/2
SOHELAHMED7 Jun 21, 2024
6644d15
Cleanup
SOHELAHMED7 Jun 21, 2024
d45f911
Test cleanup
SOHELAHMED7 Jun 21, 2024
7cf9aa3
Fix issue of migration ordering in drop + fix failing tests
SOHELAHMED7 Jun 21, 2024
fbb2e8c
Fix another failing tests
SOHELAHMED7 Jun 21, 2024
7d7dba7
Fix another failing tests - 2
SOHELAHMED7 Jun 21, 2024
51ebfd7
Fix another failing tests - 3
SOHELAHMED7 Jun 21, 2024
0dbad7c
Fix another failing tests - 4
SOHELAHMED7 Jun 21, 2024
72b927b
Fix another failing tests - EnumTest::testAddNewColumn
SOHELAHMED7 Jun 21, 2024
ad7b022
Fix another failing tests - EnumTest::testChangeToAndFromEnum
SOHELAHMED7 Jun 21, 2024
07b00b1
Fix another failing tests - ForeignKeyColumnNameTest::testIndex
SOHELAHMED7 Jun 21, 2024
0c552ce
Fix another failing tests - ForeignKeyColumnNameTest::testIndexForCol…
SOHELAHMED7 Jun 21, 2024
ba2ef56
Fix another failing tests - GeneratorTest::testGenerate
SOHELAHMED7 Jun 21, 2024
3a1499b
Fix another failing tests - NewColumnPositionTest::testAddOneNewColum…
SOHELAHMED7 Jun 21, 2024
a4dc0a4
Fix another failing tests - MultiDbSecondaryMigrationTest
SOHELAHMED7 Jun 21, 2024
cc82918
Fix issue in test
SOHELAHMED7 Jun 21, 2024
7a42619
Add more tests for different PKs
SOHELAHMED7 Jun 21, 2024
11a7d5e
Handle case of custom column name
SOHELAHMED7 Jun 22, 2024
e0b96a3
Separate tests for PgSQL and more
SOHELAHMED7 Jun 22, 2024
f06d6dc
Apply work-around for https://github.com/yiisoft/yii2/issues/20209
SOHELAHMED7 Jun 22, 2024
37cfd68
Fix style
SOHELAHMED7 Jun 22, 2024
feba5f1
Add support for PgSQL array data type
SOHELAHMED7 Jun 24, 2024
990ae26
Fix lot of issues and failing test + proper handling of text[] (array…
SOHELAHMED7 Jun 26, 2024
ea8f499
Fix drop migration
SOHELAHMED7 Jun 26, 2024
193b66d
Fix style
SOHELAHMED7 Jun 26, 2024
83e2ef8
Fix failing AttributeResolverTest
SOHELAHMED7 Jun 26, 2024
e5737fc
Compare files in test
SOHELAHMED7 Jun 26, 2024
e8dd4af
Add docs + enhancements + refactoring
SOHELAHMED7 Jun 26, 2024
d9bad2e
Polish
SOHELAHMED7 Jun 26, 2024
2976125
Fix style
SOHELAHMED7 Jun 26, 2024
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ Provide custom database table column name in case of relationship column. This w
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
```


### `x-deleted-schemas`

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:

- DB table (migrations)
- model
- faker

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:

```yaml
x-deleted-schemas:
- Fruit # Example: table name is evaluated to `itt_fruits`, if `itt_` is prefix set in DB config
- Mango: the_mango_table_name # custom table name; see `x-table` in README.md
```


## Many-to-Many relation definition

There are two ways for define many-to-many relations:
Expand Down
44 changes: 24 additions & 20 deletions src/generator/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace cebe\yii2openapi\generator;

use yii\db\mysql\Schema as MySqlSchema;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\openapi\Reader;
use cebe\openapi\spec\OpenApi;
use cebe\yii2openapi\lib\Config;
Expand All @@ -22,7 +22,10 @@
use cebe\yii2openapi\lib\generators\UrlRulesGenerator;
use cebe\yii2openapi\lib\PathAutoCompletion;
use cebe\yii2openapi\lib\SchemaToDatabase;
use Exception;
use Yii;
use yii\db\mysql\Schema as MySqlSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use yii\gii\CodeFile;
use yii\gii\Generator;
use yii\helpers\Html;
Expand Down Expand Up @@ -176,7 +179,7 @@ class ApiGenerator extends Generator
private $_openApiWithoutRef;

/**
* @var \cebe\yii2openapi\lib\Config $config
* @var Config $config
**/
private $config;

Expand Down Expand Up @@ -283,11 +286,11 @@ public function rules()

/**
* @param $attribute
* @throws \cebe\openapi\exceptions\IOException
* @throws \cebe\openapi\exceptions\TypeErrorException
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
* @throws IOException
* @throws TypeErrorException
* @throws UnresolvableReferenceException
*/
public function validateSpec($attribute):void
public function validateSpec($attribute): void
{
if ($this->ignoreSpecErrors) {
return;
Expand All @@ -299,7 +302,7 @@ public function validateSpec($attribute):void
}
}

public function validateUrlPrefixes($attribute):void
public function validateUrlPrefixes($attribute): void
{
if (empty($this->urlPrefixes)) {
return;
Expand Down Expand Up @@ -427,7 +430,7 @@ public function stickyAttributes()
);
}

public function makeConfig():Config
public function makeConfig(): Config
{
if (!$this->config) {
$props = get_object_vars($this);
Expand Down Expand Up @@ -457,11 +460,12 @@ public function makeConfig():Config
* Please refer to [[\yii\gii\generators\controller\Generator::generate()]] as an example
* on how to implement this method.
* @return CodeFile[] a list of code files to be created.
* @throws \Exception
* @throws Exception
*/
public function generate():array
public function generate(): array
{
$config = $this->makeConfig();

$actionsGenerator = $this->useJsonApi
? Yii::createObject(JsonActionGenerator::class, [$config])
: Yii::createObject(RestActionGenerator::class, [$config]);
Expand Down Expand Up @@ -489,12 +493,12 @@ public function generate():array
}

/**
* @return \cebe\openapi\spec\OpenApi
* @throws \cebe\openapi\exceptions\IOException
* @throws \cebe\openapi\exceptions\TypeErrorException
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
* @return OpenApi
* @throws IOException
* @throws TypeErrorException
* @throws UnresolvableReferenceException
*/
protected function getOpenApiWithoutReferences():OpenApi
protected function getOpenApiWithoutReferences(): OpenApi
{
if ($this->_openApiWithoutRef === null) {
$file = Yii::getAlias($this->openApiPath);
Expand All @@ -507,17 +511,17 @@ protected function getOpenApiWithoutReferences():OpenApi
return $this->_openApiWithoutRef;
}

public static function isPostgres():bool
public static function isPostgres(): bool
{
return Yii::$app->db->schema instanceof PgSqlSchema;
}

public static function isMysql():bool
public static function isMysql(): bool
{
return (Yii::$app->db->schema instanceof MySqlSchema && !static::isMariaDb());
}

public static function isMariaDb():bool
public static function isMariaDb(): bool
{
return strpos(Yii::$app->db->schema->getServerVersion(), 'MariaDB') !== false;
}
Expand Down
Loading
Loading