Skip to content

Commit

Permalink
Fix CI for PostgreSQL (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored May 20, 2024
1 parent fb52f9c commit a28625e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 deletions.
70 changes: 42 additions & 28 deletions demos/_demo-data/create-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Data\Schema\Migrator;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;

require_once __DIR__ . '/../init-autoloader.php';

Expand Down Expand Up @@ -79,6 +80,11 @@ public function import(array $rowsMulti)
}
}

// TODO impl. typed bind variable in atk4/data
$fixDatetimeTypeForPostgresqlFx = static function (string $type) use ($db): string {
return $db->getDatabasePlatform() instanceof PostgreSQLPlatform ? 'string' : $type;
};

$model = new ImportModelWithPrefixedFields($db, ['table' => 'client']);
$model->addField('name', ['type' => 'string']);
$model->addField('addresses', ['type' => 'text']);
Expand All @@ -96,7 +102,10 @@ public function import(array $rowsMulti)
$model->addField('iso3', ['type' => 'string']); // should be CHAR(3) NOT NULL
$model->addField('numcode', ['type' => 'smallint']);
$model->addField('phonecode', ['type' => 'integer']);
$countryIdField = $model->getField('atk_fp_country__id');
(new Migrator($model))->create();
(new Migrator($model))->createIndex([$model->getField('atk_fp_country__iso')], true);
(new Migrator($model))->createIndex([$model->getField('atk_fp_country__iso3')], true);
$model->import([
['id' => 1, 'iso' => 'AF', 'name' => 'AFGHANISTAN', 'nicename' => 'Afghanistan', 'iso3' => 'AFG', 'numcode' => 4, 'phonecode' => 93],
['id' => 2, 'iso' => 'AL', 'name' => 'ALBANIA', 'nicename' => 'Albania', 'iso3' => 'ALB', 'numcode' => 8, 'phonecode' => 355],
Expand Down Expand Up @@ -337,28 +346,27 @@ public function import(array $rowsMulti)
['id' => 238, 'iso' => 'ZM', 'name' => 'ZAMBIA', 'nicename' => 'Zambia', 'iso3' => 'ZMB', 'numcode' => 894, 'phonecode' => 260],
['id' => 239, 'iso' => 'ZW', 'name' => 'ZIMBABWE', 'nicename' => 'Zimbabwe', 'iso3' => 'ZWE', 'numcode' => 716, 'phonecode' => 263],
['id' => 240, 'iso' => 'RS', 'name' => 'SERBIA', 'nicename' => 'Serbia', 'iso3' => 'SRB', 'numcode' => 688, 'phonecode' => 381],
['id' => 241, 'iso' => 'AP', 'name' => 'ASIA PACIFIC REGION', 'nicename' => 'Asia / Pacific Region', 'iso3' => '---', 'numcode' => -1, 'phonecode' => -1],
['id' => 242, 'iso' => 'ME', 'name' => 'MONTENEGRO', 'nicename' => 'Montenegro', 'iso3' => 'MNE', 'numcode' => 499, 'phonecode' => 382],
['id' => 243, 'iso' => 'AX', 'name' => 'ALAND ISLANDS', 'nicename' => 'Aland Islands', 'iso3' => 'ALA', 'numcode' => 248, 'phonecode' => 358],
['id' => 244, 'iso' => 'BQ', 'name' => 'BONAIRE, SINT EUSTATIUS AND SABA', 'nicename' => 'Bonaire, Sint Eustatius and Saba', 'iso3' => 'BES', 'numcode' => 535, 'phonecode' => 599],
['id' => 245, 'iso' => 'CW', 'name' => 'CURACAO', 'nicename' => 'Curacao', 'iso3' => 'CUW', 'numcode' => 531, 'phonecode' => 599],
['id' => 246, 'iso' => 'GG', 'name' => 'GUERNSEY', 'nicename' => 'Guernsey', 'iso3' => 'GGY', 'numcode' => 831, 'phonecode' => 44],
['id' => 247, 'iso' => 'IM', 'name' => 'ISLE OF MAN', 'nicename' => 'Isle of Man', 'iso3' => 'IMN', 'numcode' => 833, 'phonecode' => 44],
['id' => 248, 'iso' => 'JE', 'name' => 'JERSEY', 'nicename' => 'Jersey', 'iso3' => 'JEY', 'numcode' => 832, 'phonecode' => 44],
['id' => 249, 'iso' => 'XK', 'name' => 'KOSOVO', 'nicename' => 'Kosovo', 'iso3' => '---', 'numcode' => -1, 'phonecode' => 381],
['id' => 250, 'iso' => 'BL', 'name' => 'SAINT BARTHELEMY', 'nicename' => 'Saint Barthelemy', 'iso3' => 'BLM', 'numcode' => 652, 'phonecode' => 590],
['id' => 251, 'iso' => 'MF', 'name' => 'SAINT MARTIN', 'nicename' => 'Saint Martin', 'iso3' => 'MAF', 'numcode' => 663, 'phonecode' => 590],
['id' => 252, 'iso' => 'SX', 'name' => 'SINT MAARTEN', 'nicename' => 'Sint Maarten', 'iso3' => 'SXM', 'numcode' => 534, 'phonecode' => 1],
['id' => 253, 'iso' => 'SS', 'name' => 'SOUTH SUDAN', 'nicename' => 'South Sudan', 'iso3' => 'SSD', 'numcode' => 728, 'phonecode' => 211],
['id' => 241, 'iso' => 'ME', 'name' => 'MONTENEGRO', 'nicename' => 'Montenegro', 'iso3' => 'MNE', 'numcode' => 499, 'phonecode' => 382],
['id' => 242, 'iso' => 'AX', 'name' => 'ALAND ISLANDS', 'nicename' => 'Aland Islands', 'iso3' => 'ALA', 'numcode' => 248, 'phonecode' => 358],
['id' => 243, 'iso' => 'BQ', 'name' => 'BONAIRE, SINT EUSTATIUS AND SABA', 'nicename' => 'Bonaire, Sint Eustatius and Saba', 'iso3' => 'BES', 'numcode' => 535, 'phonecode' => 599],
['id' => 244, 'iso' => 'CW', 'name' => 'CURACAO', 'nicename' => 'Curacao', 'iso3' => 'CUW', 'numcode' => 531, 'phonecode' => 599],
['id' => 245, 'iso' => 'GG', 'name' => 'GUERNSEY', 'nicename' => 'Guernsey', 'iso3' => 'GGY', 'numcode' => 831, 'phonecode' => 44],
['id' => 246, 'iso' => 'IM', 'name' => 'ISLE OF MAN', 'nicename' => 'Isle of Man', 'iso3' => 'IMN', 'numcode' => 833, 'phonecode' => 44],
['id' => 247, 'iso' => 'JE', 'name' => 'JERSEY', 'nicename' => 'Jersey', 'iso3' => 'JEY', 'numcode' => 832, 'phonecode' => 44],
['id' => 248, 'iso' => 'XK', 'name' => 'KOSOVO', 'nicename' => 'Kosovo', 'iso3' => 'XKK', 'numcode' => -1, 'phonecode' => 381],
['id' => 249, 'iso' => 'BL', 'name' => 'SAINT BARTHELEMY', 'nicename' => 'Saint Barthelemy', 'iso3' => 'BLM', 'numcode' => 652, 'phonecode' => 590],
['id' => 250, 'iso' => 'MF', 'name' => 'SAINT MARTIN', 'nicename' => 'Saint Martin', 'iso3' => 'MAF', 'numcode' => 663, 'phonecode' => 590],
['id' => 251, 'iso' => 'SX', 'name' => 'SINT MAARTEN', 'nicename' => 'Sint Maarten', 'iso3' => 'SXM', 'numcode' => 534, 'phonecode' => 1],
['id' => 252, 'iso' => 'SS', 'name' => 'SOUTH SUDAN', 'nicename' => 'South Sudan', 'iso3' => 'SSD', 'numcode' => 728, 'phonecode' => 211],
]);

$model = new ImportModelWithPrefixedFields($db, ['table' => 'file']);
$model->addField('name', ['type' => 'string']);
$model->addField('type', ['type' => 'string']);
$model->addField('is_folder', ['type' => 'boolean']);
$model->addField('parent_folder_id', ['type' => 'bigint']);
// KEY `fk_file_file_idx` (`parent_folder_id`)
$model->addField('parent_folder_id', ['type' => 'integer']);
(new Migrator($model))->create();
(new Migrator($model))->createForeignKey([$model->getField('atk_fp_file__parent_folder_id'), $model->getField('atk_fp_file__id')]);
$model->import([
['id' => 1, 'name' => '.gitattributes', 'type' => 'gitattributes', 'is_folder' => false, 'parent_folder_id' => null],
['id' => 2, 'name' => '.github', 'type' => 'github', 'is_folder' => true, 'parent_folder_id' => null],
Expand Down Expand Up @@ -1097,11 +1105,11 @@ public function import(array $rowsMulti)
$model->addField('project_expenses', ['type' => 'float']);
$model->addField('project_mgmt_cost_pct', ['type' => 'float']);
$model->addField('project_qa_cost_pct', ['type' => 'float']);
$model->addField('start_date', ['type' => 'date']);
$model->addField('finish_date', ['type' => 'date']);
$model->addField('finish_time', ['type' => 'time']);
$model->addField('created', ['type' => 'datetime']);
$model->addField('updated', ['type' => 'datetime']);
$model->addField('start_date', ['type' => $fixDatetimeTypeForPostgresqlFx('date')]);
$model->addField('finish_date', ['type' => $fixDatetimeTypeForPostgresqlFx('date')]);
$model->addField('finish_time', ['type' => $fixDatetimeTypeForPostgresqlFx('time')]);
$model->addField('created', ['type' => $fixDatetimeTypeForPostgresqlFx('datetime')]);
$model->addField('updated', ['type' => $fixDatetimeTypeForPostgresqlFx('datetime')]);
(new Migrator($model))->create();
$data = [
['id' => 1, 'project_name' => 'Agile DSQL', 'project_code' => 'at01', 'description' => 'DSQL is a composable SQL query builder. You can write multi-vendor queries in PHP profiting from better security, clean syntax and avoid human errors.', 'client_name' => 'Agile Toolkit', 'client_address' => 'Some Street,' . "\n" . 'Garden City' . "\n" . 'UK', 'client_country_iso' => 'GB', 'is_commercial' => 0, 'currency' => 'GBP', 'is_completed' => 1, 'project_budget' => 7000, 'project_invoiced' => 0, 'project_paid' => 0, 'project_hour_cost' => 0, 'project_hours_est' => 150, 'project_hours_reported' => 125, 'project_expenses_est' => 50, 'project_expenses' => 0, 'project_mgmt_cost_pct' => 0.1, 'project_qa_cost_pct' => 0.2, 'start_date' => '2016-01-26', 'finish_date' => '2016-06-23', 'finish_time' => '12:50:00', 'created' => '2017-04-06 10:34:34', 'updated' => '2017-04-06 10:35:04'],
Expand All @@ -1113,6 +1121,7 @@ public function import(array $rowsMulti)

$model = new ImportModelWithPrefixedFields($db, ['table' => 'product_category']);
$model->addField('name', ['type' => 'string']);
$productCategoryIdField = $model->getField('atk_fp_product_category__id');
(new Migrator($model))->create();
$model->import([
['id' => 1, 'name' => 'Condiments and Gravies'],
Expand All @@ -1122,8 +1131,10 @@ public function import(array $rowsMulti)

$model = new ImportModelWithPrefixedFields($db, ['table' => 'product_sub_category']);
$model->addField('name', ['type' => 'string']);
$model->addField('product_category_id', ['type' => 'bigint']);
$model->addField('product_category_id', ['type' => 'integer']);
$productSubcategoryIdField = $model->getField('atk_fp_6f3c91cf51e02fd5__id');
(new Migrator($model))->create();
(new Migrator($model))->createForeignKey([$model->getField('atk_fp_6f3c91cf51e02fd5__product_category_id'), $productCategoryIdField]);
$model->import([
['id' => 1, 'name' => 'Gravie', 'product_category_id' => 1],
['id' => 2, 'name' => 'Spread', 'product_category_id' => 1],
Expand All @@ -1139,9 +1150,11 @@ public function import(array $rowsMulti)
$model = new ImportModelWithPrefixedFields($db, ['table' => 'product']);
$model->addField('name', ['type' => 'string']);
$model->addField('brand', ['type' => 'string']);
$model->addField('product_category_id', ['type' => 'bigint']);
$model->addField('product_sub_category_id', ['type' => 'bigint']);
$model->addField('product_category_id', ['type' => 'integer']);
$model->addField('product_sub_category_id', ['type' => 'integer']);
(new Migrator($model))->create();
(new Migrator($model))->createForeignKey([$model->getField('atk_fp_product__product_category_id'), $productCategoryIdField]);
(new Migrator($model))->createForeignKey([$model->getField('atk_fp_product__6f3c91cf51e02fd5_id'), $productSubcategoryIdField]);
$model->import([
['id' => 1, 'name' => 'Mustard', 'brand' => 'Condiment Corp.', 'product_category_id' => 1, 'product_sub_category_id' => 2],
['id' => 2, 'name' => 'Ketchup', 'brand' => 'Condiment Corp.', 'product_category_id' => 1, 'product_sub_category_id' => 2],
Expand All @@ -1154,15 +1167,16 @@ public function import(array $rowsMulti)

$model = new ImportModelWithPrefixedFields($db, ['table' => 'multiline_item']);
$model->addField('item', ['type' => 'string']);
$model->addField('inv_date', ['type' => 'date']);
$model->addField('inv_time', ['type' => 'time']);
$model->addField('country_id', ['type' => 'bigint']);
$model->addField('inv_date', ['type' => $fixDatetimeTypeForPostgresqlFx('date')]);
$model->addField('inv_time', ['type' => $fixDatetimeTypeForPostgresqlFx('time')]);
$model->addField('country_id', ['type' => 'integer']);
$model->addField('qty', ['type' => 'integer']);
$model->addField('box', ['type' => 'integer']);
(new Migrator($model))->create();
(new Migrator($model))->createForeignKey([$model->getField('atk_fp_multiline_item__country_id'), $countryIdField]);
$model->import([
['id' => 1, 'item' => 'Chocolate', 'inv_date' => '2020-02-20', 'inv_time' => '7:20', 'country_id' => 80, 'qty' => 7, 'box' => 5],
['id' => 2, 'item' => 'DAP delivery', 'inv_date' => '2020-02-01', 'inv_time' => '8:33', 'country_id' => 223, 'qty' => 2, 'box' => 100],
['id' => 1, 'item' => 'Chocolate', 'inv_date' => '2020-02-20', 'inv_time' => '7:20:00', 'country_id' => 80, 'qty' => 7, 'box' => 5],
['id' => 2, 'item' => 'DAP delivery', 'inv_date' => '2020-02-01', 'inv_time' => '8:33:00', 'country_id' => 223, 'qty' => 2, 'box' => 100],
]);

$model = new ImportModelWithPrefixedFields($db, ['table' => 'multiline_delivery']);
Expand Down
26 changes: 15 additions & 11 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ class Percent extends Field
* @property string $name @Atk4\Field()
* @property string $type @Atk4\Field()
* @property bool $is_folder @Atk4\Field()
* @property File $subFolder @Atk4\RefMany()
* @property int $count @Atk4\Field()
* @property Folder $parent_folder_id @Atk4\RefOne()
* @property File $subFolder @Atk4\RefMany()
* @property int $subCount @Atk4\Field()
*/
class File extends ModelWithPrefixedFields
{
Expand All @@ -490,16 +490,16 @@ protected function init(): void
$this->addField($this->fieldName()->type, ['caption' => 'MIME Type']);
$this->addField($this->fieldName()->is_folder, ['type' => 'boolean']);

$this->hasMany($this->fieldName()->subFolder, [
'model' => [self::class],
'theirField' => self::hinting()->fieldName()->parent_folder_id,
])
->addField($this->fieldName()->count, ['aggregate' => 'count', 'field' => $this->getPersistence()->expr($this, '*')]);

$this->hasOne($this->fieldName()->parent_folder_id, [
'model' => [Folder::class],
])
->addTitle();

$this->hasMany($this->fieldName()->subFolder, [
'model' => [self::class],
'theirField' => self::hinting()->fieldName()->parent_folder_id,
])
->addField($this->fieldName()->subCount, ['aggregate' => 'count', 'field' => $this->getPersistence()->expr($this, '*')]);
}

public function importFromFilesystem(string $path, ?bool $isSub = null): void
Expand All @@ -512,9 +512,13 @@ public function importFromFilesystem(string $path, ?bool $isSub = null): void
}

$this->atomic(function () use ($path) {
foreach ($this as $entity) {
$entity->delete();
}
do {
$empty = true;
foreach ($this->createIteratorBy($this->fieldName()->subFolder . '/#', 0) as $entity) {
$entity->delete();
$empty = false;
}
} while (!$empty);

$path = __DIR__ . '/../' . $path;

Expand Down
4 changes: 2 additions & 2 deletions tests-behat/card-deck.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Feature: CardDeck
Scenario: add
Then I press button "Add Country"
Then I fill in "atk_fp_country__name" with "Test"
Then I fill in "atk_fp_country__iso" with "TT"
Then I fill in "atk_fp_country__iso3" with "TTT"
Then I fill in "atk_fp_country__iso" with "XT"
Then I fill in "atk_fp_country__iso3" with "XTT"
Then I fill in "atk_fp_country__numcode" with "123"
Then I fill in "atk_fp_country__phonecode" with "1"
Then I press Modal button "Save"
Expand Down
8 changes: 4 additions & 4 deletions tests-behat/crud.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Feature: Crud
Scenario: add
Then I press button "Add Country"
Then I fill in "atk_fp_country__name" with "Test"
Then I fill in "atk_fp_country__iso" with "TT"
Then I fill in "atk_fp_country__iso3" with "TTT"
Then I fill in "atk_fp_country__iso" with "XT"
Then I fill in "atk_fp_country__iso3" with "XTT"
Then I fill in "atk_fp_country__numcode" with "123"
Then I fill in "atk_fp_country__phonecode" with "1"
Then I press Modal button "Save"
Expand All @@ -25,8 +25,8 @@ Feature: Crud
Then I should see "United Kingdom"
Then I press button "Add Country"
Then I fill in "atk_fp_country__name" with "Test 2"
Then I fill in "atk_fp_country__iso" with "TT"
Then I fill in "atk_fp_country__iso3" with "TTT"
Then I fill in "atk_fp_country__iso" with "XT"
Then I fill in "atk_fp_country__iso3" with "XTT"
Then I fill in "atk_fp_country__numcode" with "123"
Then I fill in "atk_fp_country__phonecode" with "1"
Then I press Modal button "Save"
Expand Down

0 comments on commit a28625e

Please sign in to comment.