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 1 commit
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
Prev Previous commit
Next Next commit
Create PK using Yii's in-built method - more concrete
  • Loading branch information
SOHELAHMED7 committed Jun 20, 2024
commit c205866cbd20833ca463d28a26e4b89cbe30335e
70 changes: 42 additions & 28 deletions src/lib/SchemaToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

namespace cebe\yii2openapi\lib;

use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
use cebe\yii2openapi\lib\items\Attribute;
use cebe\yii2openapi\lib\items\DbModel;
use cebe\yii2openapi\lib\items\JunctionSchemas;
use cebe\yii2openapi\lib\items\MigrationModel;
use cebe\yii2openapi\lib\migrations\MigrationRecordBuilder;
use cebe\yii2openapi\lib\openapi\ComponentSchema;
use Yii;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\db\ColumnSchema;
use yii\db\Schema;
use yii\helpers\StringHelper;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use function count;

/**
Expand Down Expand Up @@ -63,7 +67,7 @@
class SchemaToDatabase
{
/**
* @var \cebe\yii2openapi\lib\Config
* @var Config
*/
protected $config;

Expand All @@ -73,15 +77,15 @@ public function __construct(Config $config)
}

/**
* @return array|\cebe\yii2openapi\lib\items\DbModel[]
* @throws \cebe\openapi\exceptions\IOException
* @throws \cebe\openapi\exceptions\TypeErrorException
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
* @return array|DbModel[]
* @throws IOException
* @throws TypeErrorException
* @throws UnresolvableReferenceException
* @throws InvalidDefinitionException
* @throws Exception
* @throws InvalidConfigException
*/
public function prepareModels():array
public function prepareModels(): array
{
$models = [];
$openApi = $this->config->getOpenApi();
Expand All @@ -95,12 +99,12 @@ public function prepareModels():array
if ($junctions->isJunctionSchema($schemaName)) {
$schemaName = $junctions->trimPrefix($schemaName);
}
/** @var \cebe\yii2openapi\lib\AttributeResolver $resolver */
/** @var AttributeResolver $resolver */
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);
$models[$schemaName] = $resolver->resolve();
}

foreach ($models as $model) {
foreach ($models as $model) {
foreach ($model->many2many as $relation) {
if (isset($models[$relation->viaModelName])) {
$relation->hasViaModel = true;
Expand All @@ -123,14 +127,14 @@ public function prepareModels():array
}

/**
* @return \cebe\yii2openapi\lib\items\JunctionSchemas
* @throws \cebe\openapi\exceptions\IOException
* @throws \cebe\openapi\exceptions\TypeErrorException
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
* @return JunctionSchemas
* @throws IOException
* @throws TypeErrorException
* @throws UnresolvableReferenceException
* @throws Exception
* @throws InvalidConfigException
*/
public function findJunctionSchemas():JunctionSchemas
public function findJunctionSchemas(): JunctionSchemas
{
$junctions = [];
$openApi = $this->config->getOpenApi();
Expand Down Expand Up @@ -210,7 +214,7 @@ public function findJunctionSchemas():JunctionSchemas
return Yii::createObject(JunctionSchemas::class, [$junctions]);
}

private function canGenerateModel(string $schemaName, ComponentSchema $schema):bool
private function canGenerateModel(string $schemaName, ComponentSchema $schema): bool
{
// only generate tables for schemas of type object and those who have defined properties
if ($schema->isObjectSchema() && !$schema->hasProperties()) {
Expand Down Expand Up @@ -240,7 +244,7 @@ private function canGenerateModel(string $schemaName, ComponentSchema $schema):b
}

/**
* @param array $schemasToDrop. Example:
* @param array $schemasToDrop . Example:
* ```
* array(2) {
* [0]=>
Expand Down Expand Up @@ -278,20 +282,21 @@ public static function f7(array $schemasToDrop): array // TODO rename
'drop' => true
]);
$dbModelsToDrop[$key] = $dbModelHere;
// TODO
// $mm = new MigrationModel($dbModelHere);
// $builder = new MigrationRecordBuilder($this->db->getSchema());
// $mm->addUpCode($builder->dropTable($tableName))
// ->addDownCode($builder->dropTable($tableName))
// ;
// if ($mm->notEmpty()) {
// $this->migrations[$tableName] = $mm;
// $this->migrations[$tableName] = $mm;
// }
}
}
return $dbModelsToDrop;
}

public static function resolveTableNameHere(string $schemaName):string // TODO rename
public static function resolveTableNameHere(string $schemaName): string // TODO rename
{
return Inflector::camel2id(StringHelper::basename(Inflector::pluralize($schemaName)), '_');
}
Expand All @@ -304,7 +309,7 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
$attributes = [];
foreach ($columnSchemas as $columnName => $columnSchema) {
/** @var $columnName string */
/** @var $columnSchema \yii\db\ColumnSchema */
/** @var $columnSchema ColumnSchema */
unset($attribute);
$attribute = new Attribute($columnSchema->name, [
'phpType' => $columnSchema->phpType, // pk
Expand All @@ -317,8 +322,16 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
]);

// generate PK using `->primaryKeys()` or similar methods instead of separate SQL statement which sets only PK to a column of table
if ($columnSchema->phpType === 'integer' && $columnSchema->isPrimaryKey === true && $columnSchema->autoIncrement) {
if ($columnSchema->dbType === 'BIGINT' || $columnSchema->dbType === 'bigserial') {
// https://github.com/cebe/yii2-openapi/issues/132
if (in_array($columnSchema->phpType, [
'integer',
'string' # https://github.com/yiisoft/yii2/issues/14663
])
&& $columnSchema->isPrimaryKey === true && $columnSchema->autoIncrement
) {
if (stripos($columnSchema->dbType, 'BIGINT') !== false # MySQL, MariaDB
|| stripos($columnSchema->dbType, 'bigserial') !== false # PgSQL
) {
if ($columnSchema->unsigned) {
$attribute->dbType = Schema::TYPE_UBIGPK;
} else {
Expand All @@ -332,6 +345,7 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
}
}
}

$attributes[] = $attribute;
}
return $attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
title: 132_create_migration_for_drop_table \#132

x-deleted-schemas: # don't use x-keep-table, use x-deleted-schemas
# - Pristine
- Pristine
- Fruit # table name evaluated to `itt_fruits`. `itt_` is prefix
# - Mango: the_mango7 # custom table name; `x-table`

Expand Down
22 changes: 11 additions & 11 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,38 +277,38 @@ public function testNullableFalseInRequired()

// Create migration for drop table if a entire schema is deleted from OpenAPI spec #132
// https://github.com/cebe/yii2-openapi/issues/132
public function testCreateMigrationForDropTable132()
{
public function testCreateMigrationForDropTable132()
{
// $this->changeDbToMariadb();
$testFile = Yii::getAlias("@specs/issue_fix/132_create_migration_for_drop_table/132_create_migration_for_drop_table.php");
$testFile = Yii::getAlias("@specs/issue_fix/132_create_migration_for_drop_table/132_create_migration_for_drop_table.php");
// $this->deleteTablesForCreateMigrationForDropTable132();
$this->createTablesForCreateMigrationForDropTable132();
$this->createTablesForCreateMigrationForDropTable132();
// sleep(3000);
$this->runGenerator($testFile);
$this->runGenerator($testFile);
// $this->runActualMigrations('mysql', 3);
// ... TODO
$this->deleteTablesForCreateMigrationForDropTable132();
// ... TODO
$this->deleteTablesForCreateMigrationForDropTable132();
// $this->deleteTables();
}
}

private function createTablesForCreateMigrationForDropTable132()
{
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
'id' => 'pk',
'id' => 'ubigpk',
'name' => 'string(150)',
])->execute();
Yii::$app->db->createCommand()->createTable('{{%pristines}}', [
'id' => 'pk',
'name' => 'string(151)',
'fruit_id' => 'integer(11)', // FK
'fruit_id' => 'bigint unsigned', // FK
// 'fruit_id' => $this->integer()->unsigned(), // FK
])->execute();
Yii::$app->db->createCommand()->addForeignKey('name', '{{%pristines}}', 'fruit_id', '{{%fruits}}', 'id')->execute();
}

private function deleteTablesForCreateMigrationForDropTable132()
{
Yii::$app->db->createCommand()->dropForeignKey('name', '{{%pristines}}')->execute();
// Yii::$app->db->createCommand('')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
}
Expand Down