Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
7babecd
Fix-xDbType
siggi-k Jul 6, 2022
a934859
Fix-Nullable (#99)
siggi-k Jul 7, 2022
5870b24
Update README.md
cebe Jul 7, 2022
7754878
Fix-Enum on MariaDb
siggi-k Jul 12, 2022
304cf15
Merge branch 'fix-db-schema' of github.com:siggi-k/yii2-openapi into …
siggi-k Jul 12, 2022
6ae03fb
Fix-numeric (mariaDb)
siggi-k Aug 25, 2022
b92a2ab
test
SOHELAHMED7 Sep 30, 2022
2a7e053
Undo test
SOHELAHMED7 Sep 30, 2022
f12b01c
test
SOHELAHMED7 Sep 30, 2022
d177ed9
Undo test
SOHELAHMED7 Sep 30, 2022
314743e
test2
SOHELAHMED7 Oct 3, 2022
3184ae0
Merge branch '100-timestamp-migrations-do-not-work-in-mariadb' of git…
SOHELAHMED7 Oct 3, 2022
2af51fb
Undo test2
SOHELAHMED7 Oct 3, 2022
96b91b1
Simplify condition
SOHELAHMED7 Oct 3, 2022
2697fe8
Update fixture
SOHELAHMED7 Oct 4, 2022
73fd187
Enhancements
SOHELAHMED7 Oct 4, 2022
0fda678
Fix failing tests
SOHELAHMED7 Oct 4, 2022
540e9ff
Fix style
SOHELAHMED7 Oct 4, 2022
c9aeee7
test
SOHELAHMED7 Nov 2, 2022
fdfe048
undo test
SOHELAHMED7 Nov 2, 2022
3740d5b
Add composer package sam-it/yii2-mariadb
SOHELAHMED7 Nov 2, 2022
4a313cc
Remove enum as there is not enum physical type in \yii\db\pgsql\Schema
SOHELAHMED7 Nov 2, 2022
e0b8e00
Fix error in tests: Remove enum as there is not enum physical type in…
SOHELAHMED7 Nov 2, 2022
1b9f568
Fix errors in tests, fix fixtures, fix mariadb docker image; fix mari…
SOHELAHMED7 Nov 2, 2022
8fb7084
test
SOHELAHMED7 Nov 21, 2022
329866e
undo test
SOHELAHMED7 Nov 21, 2022
58e5772
Merge branches 'master' and '100-timestamp-migrations-do-not-work-in-…
SOHELAHMED7 Nov 21, 2022
4654195
Remove 'without UUID' test case
SOHELAHMED7 Nov 21, 2022
da85359
Remove 'without UUID' test case part 2
SOHELAHMED7 Nov 21, 2022
0b94b2a
Add TODO note [skip ci]
SOHELAHMED7 Nov 21, 2022
99433e1
Remove UUID in tests [skip ci]
SOHELAHMED7 Nov 22, 2022
ee5136c
Convert erroneous tests to failed tests
SOHELAHMED7 Nov 22, 2022
b667ac0
Use real app and real DB everywhere
SOHELAHMED7 Nov 22, 2022
930e39e
Edit GA to Run tests in Docker container in Github action
SOHELAHMED7 Nov 22, 2022
5a3395c
Change vendor of install-docker-compose in GA
SOHELAHMED7 Nov 22, 2022
bf0c9a7
Config GA - WIP
SOHELAHMED7 Nov 22, 2022
1924510
Config GA -part 2 - WIP
SOHELAHMED7 Nov 22, 2022
b4ec082
Config GA -part 3 - WIP
SOHELAHMED7 Nov 22, 2022
95dee95
Config GA -part 4 - WIP
SOHELAHMED7 Nov 22, 2022
a705c84
Config GA -part 5 - WIP
SOHELAHMED7 Nov 22, 2022
e9dea60
Config GA -part 6 - WIP
SOHELAHMED7 Nov 22, 2022
bfa65e2
Config GA -part 7 - WIP
SOHELAHMED7 Nov 22, 2022
17781a8
Fix failing tests and add more tests for Pgsql and Mariadb
SOHELAHMED7 Nov 22, 2022
e4e2488
Fix failing tests and bugs
SOHELAHMED7 Nov 23, 2022
a753d91
Fix enum related bug in failing tests - WIP
SOHELAHMED7 Nov 23, 2022
64b6936
WIP
SOHELAHMED7 Nov 23, 2022
6adae6a
Remove all enum related changes
SOHELAHMED7 Nov 25, 2022
015a1fc
Add TODO in Makefile
SOHELAHMED7 Nov 25, 2022
0a79006
Fix failing tests
SOHELAHMED7 Nov 25, 2022
68718e0
Attempt to fix failing tests in GA
SOHELAHMED7 Nov 25, 2022
f2cacfe
Error message betterment
SOHELAHMED7 Nov 25, 2022
4868df0
last commit ('Error message betterment') didn't seen in Github, check…
SOHELAHMED7 Nov 25, 2022
b32ae65
BLOB, TEXT, GEOMETRY, and JSON data types cannot be assigned a defaul…
SOHELAHMED7 Nov 26, 2022
a3929f0
Fix failing tests caused by x-db-type implementation and running test…
SOHELAHMED7 Nov 26, 2022
54da89a
Cleanup
SOHELAHMED7 Nov 26, 2022
1516dd6
Cleanup 2
SOHELAHMED7 Nov 27, 2022
e8ae746
Fix bug in x-db-type feature + Add tests WIP
SOHELAHMED7 Nov 28, 2022
dbe2ba5
Fix failing test
SOHELAHMED7 Nov 28, 2022
22e3ace
Restructure base DbTestCase and organize tests
SOHELAHMED7 Nov 28, 2022
2f010bf
Add tests for x-db-type for MySQL, MariaDb and PgSQL for cases: fresh…
SOHELAHMED7 Nov 29, 2022
df929e0
Fix failing tests + add change column data type by x-db-type - WIP
SOHELAHMED7 Nov 30, 2022
15b1b68
Trigger Github Action
SOHELAHMED7 Nov 30, 2022
8feea61
Trigger Github Action 2
SOHELAHMED7 Nov 30, 2022
3085986
Trigger Github Action 3
SOHELAHMED7 Nov 30, 2022
9953ede
Trigger Github Action 4
SOHELAHMED7 Nov 30, 2022
632dcb8
Enh Github Action
SOHELAHMED7 Dec 1, 2022
44af514
Fix issue and failing test in implementation of x-db-type
SOHELAHMED7 Dec 1, 2022
109e46d
Enhance tests + organize code
SOHELAHMED7 Dec 2, 2022
5cc77ea
Change PropertySchema
SOHELAHMED7 Dec 2, 2022
fec8a0b
Consider phpType in comparison
SOHELAHMED7 Dec 2, 2022
c3c5367
Strengthen the way tmp column is stored for compareColumn part + fix …
SOHELAHMED7 Dec 3, 2022
7928ef0
Fix style + Implement Pgsql compare column change by new way
SOHELAHMED7 Dec 3, 2022
ac42564
Fix bugs in Decimal related x-db-type changes
SOHELAHMED7 Dec 5, 2022
6e2275d
Refactor migration generation code and add more tests for x-db-type f…
SOHELAHMED7 Dec 6, 2022
fb869ba
Add more tests for x-db-type for MySQL
SOHELAHMED7 Dec 6, 2022
dd0ce9b
Add more tests
SOHELAHMED7 Dec 7, 2022
2180f7a
Add more tests for edit column scenario
SOHELAHMED7 Dec 8, 2022
6a3e1da
Pass the CI
SOHELAHMED7 Dec 8, 2022
30671aa
Add tests for Mariadb + add tooling support to run migrations for mul…
SOHELAHMED7 Dec 8, 2022
d5d0f6f
Fix bug related to DB type with more than one word for MySQL and PgSQ…
SOHELAHMED7 Dec 12, 2022
02d06fa
Tests refactoring and add more x-db-type tests for PgSQL
SOHELAHMED7 Dec 13, 2022
4fb7b28
WIP
SOHELAHMED7 Dec 13, 2022
8449d44
Implement string base add column code - WIP ('double precision precis…
SOHELAHMED7 Dec 13, 2022
2baf6c5
Fix 'double precision precision' issue for x-db-type - WIP + Fix few …
SOHELAHMED7 Dec 14, 2022
3ac7837
Fix failing tests
SOHELAHMED7 Dec 14, 2022
7df1b85
Delete un-necessary file
SOHELAHMED7 Dec 14, 2022
b1cf636
Change addColumn feature in migration for x-db-type
SOHELAHMED7 Dec 17, 2022
8d19fe9
Fix issue for alter column for x-db-type in Pgsql
SOHELAHMED7 Dec 19, 2022
7f64da0
Fix style
SOHELAHMED7 Dec 19, 2022
477c38b
Fix tests for fresh tables scenario
SOHELAHMED7 Dec 19, 2022
0070b63
Enhance PR
SOHELAHMED7 Dec 20, 2022
595f25f
Add more tests
SOHELAHMED7 Dec 20, 2022
cbd1dc1
Enhance PR
SOHELAHMED7 Dec 20, 2022
a49eee1
Fix bugs + add more tests
SOHELAHMED7 Dec 20, 2022
984382f
Add docs
SOHELAHMED7 Dec 20, 2022
a00a8da
Fix failing test
SOHELAHMED7 Dec 20, 2022
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
Fix bug in x-db-type feature + Add tests WIP
  • Loading branch information
SOHELAHMED7 committed Nov 28, 2022
commit e8ae746c3074ab5a6d47d70c9d8c362031887497
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ On console you can run the generator with `./yii gii/api --openApiPath=@app/open

Run `./yii gii/api --help` for all options.

See [Petstore example](https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml) for example OpenAPI spec.


## OpenAPI extensions

Expand Down Expand Up @@ -115,9 +117,26 @@ Explicitly specify primary key name for table, if it is different from "id"

### `x-db-type`

Explicitly specify the database type for a column. (MUST contains only db type! (json, jsonb, uuid, varchar etc))
If x-db-type sets as false, property will be processed as virtual;
It will be added in model as public property, but skipped for migrations generation
Explicitly specify the database type for a column. (MUST contains only real DB type! (`json`, `jsonb`, `uuid`, `varchar` etc.)).
If x-db-type sets as `false`, property will be processed as virtual;
It will be added in model as public property, but skipped for migrations generation.

Example values of `x-db-type` are:

- `false` (boolean false)
- as string and its value can be like:
- text
- text[]
- INTEGER PRIMARY KEY AUTO_INCREMENT
- decimal(12,4)
- decimal(12)
- decimal
- json
- varchar
- VARCHAR

Such values are not allowed:
- `int null default null after low_price` (null and default will be handled by `nullable` and `default` keys respectively)

### `x-indexes`
Specify table indexes
Expand Down
90 changes: 62 additions & 28 deletions src/lib/items/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,58 +303,54 @@ public function toColumnSchema():ColumnSchema
// todo docs - it throw new NotSupportedException
private function yiiAbstractTypeForDbSpecificType(string $type): string
{
if (is_string($this->xDbType) && !empty($this->xDbType)) {
if (is_string($this->xDbType) && !empty($this->xDbType) && trim($this->xDbType)) {
$xDbType = strtolower($this->xDbType);

list($isXDbTypeWithArray, $modifiedXDbType) = $this->isTypeWithArray($xDbType);
list(, $justYiiAbstractType) = $this->isTypeWithMoreInfo($xDbType);

if ($this->isMysql()) {
$mysqlSchema = new MySqlSchema;

if (!array_key_exists($modifiedXDbType, $mysqlSchema->typeMap)) {
throw new InvalidDefinitionException('"x-db-type: '.$modifiedXDbType.'" is incorrect for MySQL. "'.$modifiedXDbType.'" is not a real data type in MySQL.');
if (!array_key_exists($justYiiAbstractType, $mysqlSchema->typeMap)) {
throw new InvalidDefinitionException('"x-db-type: '.$justYiiAbstractType.'" is incorrect for MySQL. "'.$justYiiAbstractType.'" is not a real data type in MySQL.');
}

return $mysqlSchema->typeMap[$modifiedXDbType] .
($isXDbTypeWithArray ? '[]' : '');
return $mysqlSchema->typeMap[$justYiiAbstractType];
} elseif ($this->isMariaDb()) {
$mariadbSchema = new MariaDbSchema;

if (!array_key_exists($modifiedXDbType, $mariadbSchema->typeMap)) {
throw new InvalidDefinitionException('"x-db-type: '.$modifiedXDbType.'" is incorrect for MariaDB. "'.$modifiedXDbType.'" is not a real data type in MariaDb.');
if (!array_key_exists($justYiiAbstractType, $mariadbSchema->typeMap)) {
throw new InvalidDefinitionException('"x-db-type: '.$justYiiAbstractType.'" is incorrect for MariaDB. "'.$justYiiAbstractType.'" is not a real data type in MariaDb.');
}
return $mariadbSchema->typeMap[$modifiedXDbType] .
($isXDbTypeWithArray ? '[]' : '');
return $mariadbSchema->typeMap[$justYiiAbstractType];
} elseif ($this->isPostgres()) {
$pgsqlSchema = new PgSqlSchema;
if (!array_key_exists($modifiedXDbType, $pgsqlSchema->typeMap)) {
throw new InvalidDefinitionException('"x-db-type: '.$modifiedXDbType.'" is incorrect for PostgreSQL. "'.$modifiedXDbType.'" is not a real data type in PostgreSQL.');
if (!array_key_exists($justYiiAbstractType, $pgsqlSchema->typeMap)) {
throw new InvalidDefinitionException('"x-db-type: '.$justYiiAbstractType.'" is incorrect for PostgreSQL. "'.$justYiiAbstractType.'" is not a real data type in PostgreSQL.');
}
return $pgsqlSchema->typeMap[$modifiedXDbType] .
($isXDbTypeWithArray ? '[]' : '');
return $pgsqlSchema->typeMap[$justYiiAbstractType];
} else {
throw new NotSupportedException('"x-db-type" for database '.get_class(Yii::$app->db->schema).' is not implemented. It is only implemented for PostgreSQL, MySQL and MariaDB.');
}
} else {
list($isTypeWithArray, $modifiedType) = $this->isTypeWithArray($type);
if (stripos($type, 'int') === 0) {
return $isTypeWithArray ? 'integer[]' : 'integer';
return 'integer';
}
if (stripos($type, 'string') === 0) {
return $isTypeWithArray ? 'string[]' : 'string';
return 'string';
}
if (stripos($type, 'varchar') === 0) {
return $isTypeWithArray ? 'string[]' : 'string';
return 'string';
}
if (stripos($type, 'tsvector') === 0) {
return $isTypeWithArray ? 'string[]' : 'string';
return 'string';
}
if (stripos($type, 'json') === 0) {
return $isTypeWithArray ? 'json[]' : 'json';
return 'json';
}
// TODO? behaviour in Pgsql should remain same but timestamp/datetime bug which is only reproduced in Mysql and Mariadb should be fixed
if (stripos($type, 'datetime') === 0) {
return $isTypeWithArray ? 'timestamp[]' : 'timestamp';
return 'timestamp';
}
}

Expand All @@ -369,16 +365,54 @@ private function allowNull()
return !$this->isRequired();
}

private function isTypeWithArray(string $type): array
/**
* TODO docs + unit tests
* Value of `x-db-type` can be:
* - `false` (boolean false)
* - as string and its value can be like:
* - text
* - text[]
* - INTEGER PRIMARY KEY AUTO_INCREMENT
* - decimal(12,4)
* - decimal(12)
* - decimal
* - json
* - varchar
* - VARCHAR
*
*/
public function isTypeWithMoreInfo(string $type): array
{
$modifiedType = $type;
$isDataTypeWithArray = false;
$justYiiAbstractType = $type;
$isTypeWithMoreInfo = false;

// 'text' => match `text`
// 'text[]' => match `text`
// 'INTEGER PRIMARY KEY AUTO_INCREMENT' => match `INTEGER`
// 'decimal(12,4)' => match `decimal`
// 'decimal(12)' => match `decimal`
preg_match('/(\w+)/', $type, $matches);

if (StringHelper::endsWith($type, '[]')) {
$isDataTypeWithArray = true;
$modifiedType = str_replace('[]', '', $type); // remove `[]` (array) e.g. 'text[]' -> 'text'
if (isset($matches[0])) {
$justYiiAbstractType = $matches[0];
$justYiiAbstractType = strtolower($justYiiAbstractType);
}
return [$isDataTypeWithArray, $modifiedType];

// $isTypeWithMoreInfo = true for following:
// - text[]
// - INTEGER PRIMARY KEY AUTO_INCREMENT
// - decimal(12,4)
// - decimal(12)

// $isTypeWithMoreInfo = false for following:
// json
// text
// VARCHAR
if (strlen($justYiiAbstractType) < strlen($type)) {
$isTypeWithMoreInfo = true;
}

return [$isTypeWithMoreInfo, $justYiiAbstractType];
}

// TODO avoid duplication. also present in lib/ColumnToCode
Expand Down
13 changes: 13 additions & 0 deletions tests/specs/petstore_x_db_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@specs/petstore_x_db_type.yaml',
'generateUrls' => true,
'generateModels' => true,
'excludeModels' => [
'Error',
],
'generateControllers' => true,
'generateMigrations' => true,
'generateModelFaker' => true,
];
143 changes: 143 additions & 0 deletions tests/specs/petstore_x_db_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
maximum: 100
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Checktypes:
type: object
required:
- id
properties:
id:
type: integer
format: int64
string_col:
type: string
x-db-type: string
varchar_col:
type: string
x-db-type: varchar
text_col:
type: string
x-db-type: text
varchar_4_col:
type: string
x-db-type: VARCHAR(4)
char_4_col:
type: string
x-db-type: CHAR(4)
# int_col:
# type: integer
# x-db-type: INT NULL DEFAULT NULL AFTER `Low_price`
decimal_col:
type: float
x-db-type: decimal(12,3)

Pets:
type: array
maxItems: 100
items:
$ref: "#/components/schemas/Pet"
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string