Skip to content

Resolve: Create migration for drop table if a entire schema is deleted from OpenAPI spec #4

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

Merged
merged 75 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 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
56d4bb5
Update README.md
cebe 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
b7d6b94
Merge pull request #29 from SOHELAHMED7/132-create-migration-for-drop…
SOHELAHMED7 Jun 26, 2024
08998f6
Merge branch 'cebe:master' into master
SOHELAHMED7 Jun 26, 2024
5ffbb33
Update README.md
SOHELAHMED7 Jun 26, 2024
48e07b2
Undo disable sorting for non dependent models
SOHELAHMED7 Jul 25, 2024
cf12d6f
Add test for independent tables drop sort
SOHELAHMED7 Jul 25, 2024
74ca15a
Revert "Fix another failing tests - MultiDbSecondaryMigrationTest"
SOHELAHMED7 Jul 25, 2024
9932d26
Revert "Fix another failing tests - NewColumnPositionTest::testAddOne…
SOHELAHMED7 Jul 25, 2024
bed7fbb
Revert "Fix another failing tests - GeneratorTest::testGenerate"
SOHELAHMED7 Jul 25, 2024
b08a7dc
Revert "Fix another failing tests - ForeignKeyColumnNameTest::testInd…
SOHELAHMED7 Jul 25, 2024
73d36d3
Revert "Fix another failing tests - ForeignKeyColumnNameTest::testIndex"
SOHELAHMED7 Jul 25, 2024
8d835c0
Revert "Fix another failing tests - EnumTest::testChangeToAndFromEnum"
SOHELAHMED7 Jul 25, 2024
1c0f3b6
Revert "Fix another failing tests - EnumTest::testAddNewColumn"
SOHELAHMED7 Jul 25, 2024
9fdc293
Revert "Fix another failing tests - 4"
SOHELAHMED7 Jul 25, 2024
38682f1
Revert "Fix another failing tests - 3"
SOHELAHMED7 Jul 25, 2024
e19fffb
Revert "Fix another failing tests - 2"
SOHELAHMED7 Jul 25, 2024
60df77f
Revert "Fix another failing tests"
SOHELAHMED7 Jul 25, 2024
8b1876f
Revert "Fix issue of migration ordering in drop + fix failing tests"
SOHELAHMED7 Jul 25, 2024
1c86e8e
Fix failing tests
SOHELAHMED7 Jul 25, 2024
592b309
Merge branch 'master' of https://github.com/php-openapi/yii2-openapi
SOHELAHMED7 Nov 13, 2024
25063ca
Fix errors and failing tests
SOHELAHMED7 Nov 13, 2024
353f61e
Fix failing test: IssueFixTest::testCreateMigrationForDropTable132
SOHELAHMED7 Nov 13, 2024
4faa937
Refactor test
SOHELAHMED7 Nov 13, 2024
c47deb8
Fix failing test: IssueFixTest::testCreateMigrationForDropTable132For…
SOHELAHMED7 Nov 13, 2024
3715ff2
Fix failing test: testCreateMigrationForDropTable132IndependentTables…
SOHELAHMED7 Nov 13, 2024
165ea0c
Fix more tests
SOHELAHMED7 Nov 13, 2024
ae229f4
Fix more failing tests
SOHELAHMED7 Nov 13, 2024
6100d21
Refactor
SOHELAHMED7 Nov 13, 2024
db08939
Attempt to fix failing tests in Github Action
SOHELAHMED7 Nov 14, 2024
e4bcd76
Attempt to fix failing tests: memory exhaust (in `phpunit --repeat 3`…
SOHELAHMED7 Nov 14, 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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ 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
```

### `x-no-relation`

To differentiate a component schema property from one-to-many or many-to-many relation in favour of array(json) of
Expand Down Expand Up @@ -443,7 +460,7 @@ paths:
Generated URL rules config for above is (in `urls.rest.php` or pertinent file):
```php
'GET a1/b1' => 'abc/xyz',
'POST a1/b1' => 'abc/xyz',
'POST a1/b1' => 'abc/xyz',
'a1/b1' => 'abc/options',
```
`x-route` does not support [Yii Modules](https://www.yiiframework.com/doc/guide/2.0/en/structure-modules).
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"yiisoft/yii2": "~2.0.48",
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0",
"laminas/laminas-code": ">=3.4 <=4.13",
"php-openapi/yii2-fractal": "^1.0.0",
"php-openapi/yii2-fractal": "^1.4",
"fakerphp/faker": "^1.9",
"sam-it/yii2-mariadb": "^2.0",
"symfony/var-exporter": "^5.4",
"symfony/polyfill-php80": "^1.30"
"symfony/polyfill-php80": "^1.31"
},
"require-dev": {
"cebe/indent": "*",
Expand Down
39 changes: 22 additions & 17 deletions src/generator/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace cebe\yii2openapi\generator;

use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\yii2openapi\lib\items\DbModel;
use cebe\openapi\Reader;
use cebe\openapi\spec\OpenApi;
Expand All @@ -22,6 +25,7 @@
use cebe\yii2openapi\lib\items\RestAction;
use cebe\yii2openapi\lib\PathAutoCompletion;
use cebe\yii2openapi\lib\SchemaToDatabase;
use Exception;
use Yii;
use yii\db\mysql\Schema as MySqlSchema;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
Expand Down Expand Up @@ -190,7 +194,7 @@ class ApiGenerator extends Generator
private $_openApiWithoutRef;

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

Expand Down Expand Up @@ -297,11 +301,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 @@ -313,7 +317,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 @@ -441,7 +445,7 @@ public function stickyAttributes()
);
}

public function makeConfig():Config
public function makeConfig(): Config
{
if (!$this->config) {
$props = get_object_vars($this);
Expand Down Expand Up @@ -471,11 +475,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 @@ -504,12 +509,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 @@ -522,17 +527,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
14 changes: 8 additions & 6 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
->asReference($junkAttribute['relatedClassName'])
->setPhpType($junkAttribute['phpType'])
->setDbType($junkAttribute['dbType'])
->setForeignKeyColumnName($property->fkColName);
->setForeignKeyColumnName($property->fkColName)
->setTableName($this->componentSchema->resolveTableName($this->schemaName));
$relation = Yii::createObject(AttributeRelation::class, [
$property->getName(),
$junkAttribute['relatedTableName'],
Expand Down Expand Up @@ -205,7 +206,7 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
*/
protected function resolveProperty(
PropertySchema $property,
bool $isRequired,
bool $isRequired,
$nullableValue = 'ARG_ABSENT'
): void {
if ($nullableValue === 'ARG_ABSENT') {
Expand All @@ -227,7 +228,8 @@ protected function resolveProperty(
->setNullable($nullableValue)
->setIsPrimary($property->isPrimaryKey())
->setForeignKeyColumnName($property->fkColName)
->setFakerStub($this->guessFakerStub($attribute, $property));
->setFakerStub($this->guessFakerStub($attribute, $property))
->setTableName($this->componentSchema->resolveTableName($this->schemaName));
if ($property->isReference()) {
if ($property->isVirtual()) {
throw new InvalidDefinitionException('References not supported for virtual attributes');
Expand Down Expand Up @@ -374,9 +376,9 @@ protected function resolveProperty(
* @throws InvalidConfigException|InvalidDefinitionException
*/
protected function catchManyToMany(
string $propertyName,
string $relatedSchemaName,
string $relatedTableName,
string $propertyName,
string $relatedSchemaName,
string $relatedTableName,
ComponentSchema $refSchema
): bool {
if (strtolower(Inflector::id2camel($propertyName, '_'))
Expand Down
5 changes: 3 additions & 2 deletions src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ private function getIsBuiltinType($type, $dbType)
private function resolveEnumType():void
{
if (ApiGenerator::isPostgres()) {
$rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
$this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
// $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
// $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
$this->rawParts['type'] = '"'.$this->column->dbType.'"';
return;
}
$this->rawParts['type'] = 'enum(' . self::mysqlEnumToString($this->column->enumValues) . ')';
Expand Down
6 changes: 6 additions & 0 deletions src/lib/CustomSpecAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class CustomSpecAttr
*/
public const FK_COLUMN_NAME = 'x-fk-column-name';

/**
* Drop table Migrations to be generated from removed component schemas
* See README for docs
*/
public const DELETED_SCHEMAS = 'x-deleted-schemas';

/**
* Foreign key column name. See README for usage docs
*/
Expand Down
Loading
Loading