Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Fix for #100 - timestamp migrations do not work in MariaDB #112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 18 additions & 24 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,32 @@ jobs:
fail-fast: true
matrix:
php-versions: ['7.1', '7.2', '7.3', '7.4']

# TODO use cache
steps:
- uses: actions/checkout@v2

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
# Run every tests inside Docker container
- name: Docker Compose Setup
uses: ndeloof/install-compose-action@v0.0.1
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, intl, gd, zip, dom, pgsql
tools: php-cs-fixer

- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
# version: v3.5 # defaults to 'latest'
legacy: true # will also install in PATH as `docker-compose`

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php-versions }}
- name: Clean
run: make clean_all

- name: Install deps
run: composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
- name: docker-compose up
run: make up

- name: Prepare permissions
run: mkdir -p tests/tmp && chmod -R 0777 tests/tmp
- name: Install Docker and composer dependencies
run: make installdocker

- name: Unit tests
run: php vendor/bin/phpunit
- name: Migrate
run: make migrate

- name: Check style
run: make check-style
run: make check-style-from-host

- name: Run tests
run: make testdocker
29 changes: 26 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ all:
check-style:
vendor/bin/php-cs-fixer fix --diff --dry-run

check-style-from-host:
docker-compose run --rm php sh -c 'vendor/bin/php-cs-fixer fix --diff --dry-run'

fix-style:
vendor/bin/indent --tabs composer.json
vendor/bin/indent --spaces .php_cs.dist
Expand All @@ -28,20 +31,40 @@ clean:

up:
docker-compose up -d
echo "Waiting for mariadb to start up..."
docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h maria --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1)

cli:
docker-compose exec php bash

migrate:
mkdir -p "tests/tmp/app"
mkdir -p "tests/tmp/docker_app"
docker-compose run --rm php sh -c 'mkdir -p "tests/tmp/app"'
docker-compose run --rm php sh -c 'mkdir -p "tests/tmp/docker_app"'
docker-compose run --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0'

installdocker:
docker-compose run --rm php composer install && chmod +x tests/yii

testdocker:
docker-compose run --rm php sh -c 'vendor/bin/phpunit tests/unit'
docker-compose run --rm php sh -c 'vendor/bin/phpunit'

.PHONY: all check-style fix-style install test clean clean_all up cli installdocker migrate testdocker


# Docs:

# outside docker
# clean_all
# clean (in both)
# up
# cli
# migrate
# installdocker
# testdocker

# inside docker
# check-style
# fix-style
# install
# test
# clean (in both)
144 changes: 141 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 @@ -114,9 +116,27 @@ 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)
- json
- varchar
- VARCHAR
- SMALLINT UNSIGNED ZEROFILL
- MEDIUMINT(10) UNSIGNED ZEROFILL COMMENT "comment" (note the double quotes here)

Such values are not allowed:
- `int null default null after low_price` (null and default will be handled by `nullable` and `default` keys respectively)
- MEDIUMINT(10) UNSIGNED ZEROFILL NULL DEFAULT '7' COMMENT 'comment' AFTER `seti`, ADD INDEX `t` (`w`)

### `x-indexes`
Specify table indexes
Expand Down Expand Up @@ -248,6 +268,97 @@ $this->update('{{%company}}', ['name' => 'No name']);
$this->alterColumn('{{%company}}', 'name', $this->string(128)->notNull());
```

### Handling of `NOT NULL` constraints

`NOT NULL` in DB migrations is determined by `nullable` and `required` properties of the OpenAPI schema.
e.g. attribute = 'my_property'.

- If you define attribute neither "required" nor via "nullable", then it is by default `NULL`:

```yaml
ExampleSchema:
properties:
my_property:
type: string
```

- If you define attribute in "required", then it is `NOT NULL`

```yaml
ExampleSchema:
required:
- my_property
properties:
my_property:
type: string
```

- If you define attribute via "nullable", then it overrides "required", e.g. allow `NULL` in this case:

```yaml
ExampleSchema:
required:
- my_property
properties:
my_property:
type: string
nullable: true
```

- If you define attribute via "nullable", then it overrides "required", e.g. `NOT NULL` in this case:

```yaml
test_table:
required:
properties:
my_property:
type: string
nullable: false
```

### Handling of `enum` (#enum, #MariaDb)
It work on MariaDb.

```yaml
test_table:
properties:
my_property:
enum:
- one
- two
- three
```

### Handling of `numeric` (#numeric, #MariaDb)
precision-default = 10
scale-default = 2

- You can define attribute like "numeric(precision,scale)":
```yaml
test_table:
properties:
my_property:
x-db-type: decimal(12,4)
```
DB-Result = decimal(12,4)

- You can define attribute like "numeric(precision)" with default scale-default = 2:
```yaml
test_table:
properties:
my_property:
x-db-type: decimal(12)
```
DB-Result = decimal(12,2)

- You can define attribute like "numeric" with precision-default = 10 and scale-default = 2:
```yaml
test_table:
properties:
my_property:
x-db-type: decimal
```
DB-Result = decimal(10,2)

## Screenshots

Expand All @@ -259,6 +370,32 @@ Generated files:

![Gii Generated Files](doc/screenshot-files.png)

# Development

There commands are available to develop and check the tests. It can be used inside the Docker container. To enter into bash of container run `make cli` .

```bash
./yii migrate-mysql/up
./yii migrate-mysql/down 4

./yii migrate-maria/up
./yii migrate-maria/down 4

./yii migrate-pgsql/up
./yii migrate-pgsql/down 4
```

To apply multiple migration with one command:

```bash
./yii migrate-mysql/up --interactive=0 && \
./yii migrate-mysql/down --interactive=0 4 && \
./yii migrate-maria/up --interactive=0 && \
./yii migrate-maria/down --interactive=0 4 && \
./yii migrate-pgsql/up --interactive=0 && \
./yii migrate-pgsql/down --interactive=0 4
```

# Support

**Need help with your API project?**
Expand All @@ -268,3 +405,4 @@ Professional support, consulting as well as software development services are av
https://www.cebe.cc/en/contact

Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud).

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0",
"laminas/laminas-code": "^3.4",
"insolita/yii2-fractal": "^1.0.0",
"fakerphp/faker": "^1.9"
"fakerphp/faker": "^1.9",
"sam-it/yii2-mariadb": "^2.0"
},
"require-dev": {
"cebe/indent": "*",
Expand Down
16 changes: 9 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ services:
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
MYSQL_DATABASE: testdb

maria:
image: mariadb
image: mariadb:10.8
ports:
- '23306:3306'
volumes:
- ./tests/tmp/maria:/var/lib/mysql:rw
environment:
TZ: UTC
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
MYSQL_DATABASE: testdb
MYSQL_INITDB_SKIP_TZINFO: 1
# TZ: UTC
# MARIADB_ALLOW_EMPTY_PASSWORD: 1
MARIADB_ROOT_PASSWORD: verysecret
MARIADB_USER: dbuser
MARIADB_PASSWORD: dbpass
MARIADB_DATABASE: testdb
# MYSQL_INITDB_SKIP_TZINFO: 1
postgres:
image: postgres:12
ports:
Expand Down
28 changes: 28 additions & 0 deletions src/db/ColumnSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
*/

namespace cebe\yii2openapi\db;

class ColumnSchema extends \yii\db\ColumnSchema
{
/**
* @var string|null|false
* Custom DB type which contains real DB type
* Contains x-db-type string if present in OpenAPI YAML/json file
* @see \cebe\yii2openapi\lib\items\Attribute::$xDbType and `x-db-type` docs in README.md
* Used to detect what kind of migration code for column is to be generated
* e.g. `double_p double precision NULL DEFAULT NULL`
* instead of
* ```php
* $this->createTable('{{%alldbdatatypes}}', [
* ...
* 'double_p' => 'double precision NULL DEFAULT NULL',
* ...
* ```
*/
public $xDbType;
}
18 changes: 18 additions & 0 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 yii\db\mysql\Schema as MySqlSchema;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use cebe\openapi\Reader;
use cebe\openapi\spec\OpenApi;
use cebe\yii2openapi\lib\Config;
Expand Down Expand Up @@ -502,4 +505,19 @@ protected function getOpenApiWithoutReferences():OpenApi
}
return $this->_openApiWithoutRef;
}

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

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

public static function isMariaDb():bool
{
return strpos(Yii::$app->db->schema->getServerVersion(), 'MariaDB') !== false;
}
}
2 changes: 2 additions & 0 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ protected function resolveProperty(PropertySchema $property, bool $isRequired):v
->setDescription($property->getAttr('description', ''))
->setReadOnly($property->isReadonly())
->setDefault($property->guessDefault())
->setXDbType($property->getAttr('x-db-type'))
->setNullable($property->getProperty()->getSerializableData()->nullable ?? null)
->setIsPrimary($property->isPrimaryKey());
if ($property->isReference()) {
if ($property->isVirtual()) {
Expand Down
Loading