Skip to content

Commit

Permalink
Merge pull request #48 from NoResponseMate/maintenance/fix-postgres-m…
Browse files Browse the repository at this point in the history
…igration

[Maintenance] Fix migration to allow running on postgres
  • Loading branch information
lchrusciel authored Jun 24, 2024
2 parents b7f81e5 + 606060f commit e3a587f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 7 deletions.
95 changes: 91 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
workflow_dispatch: ~

jobs:
tests:
mysql-tests:
runs-on: ubuntu-latest

name: "Sylius ${{ matrix.sylius }}, PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}, MySQL ${{ matrix.mysql }}"
Expand All @@ -24,15 +24,15 @@ jobs:
symfony: ["5.4.*", "^6.0"]
sylius: ["~1.12.0", "~1.13.0"]
node: ["18.x"]
mysql: ["5.7"]
mysql: ["8.0"]

env:
APP_ENV: test
DATABASE_URL: "mysql://root:root@127.0.0.1/sylius?serverVersion=${{ matrix.mysql }}"

steps:
-
uses: actions/checkout@v3
uses: actions/checkout@v4

-
name: Restrict Sylius version
Expand All @@ -41,7 +41,7 @@ jobs:
-
name: Build application
uses: SyliusLabs/BuildTestAppAction@v2.1
uses: SyliusLabs/BuildTestAppAction@v2.2
with:
build_type: "plugin"
cache_key: "${{ github.run_id }}-${{ runner.os }}-${{ hashFiles('composer.json') }}-sylius-${{ matrix.sylius }}-symfony-${{ matrix.symfony }}"
Expand Down Expand Up @@ -88,3 +88,90 @@ jobs:
name: Behat logs
path: etc/build/
if-no-files-found: ignore

postgres-tests:
runs-on: ubuntu-latest

name: "Sylius ${{ matrix.sylius }}, PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}, PostgreSQL ${{ matrix.postgres }}"

strategy:
fail-fast: false
matrix:
php: ["8.1"]
symfony: ["5.4.*", "^6.0"]
sylius: ["~1.12.0", "~1.13.0"]
node: ["18.x"]
postgres: ["14.6"]

env:
APP_ENV: test
DATABASE_URL: "pgsql://postgres:postgres@127.0.0.1/sylius?charset=utf8&serverVersion=${{ matrix.postgres }}"

steps:
- name: Set postgres env
shell: bash
run: |
if [ "${{ matrix.sylius }}" == "~1.12.0" ]; then
echo "USE_LEGACY_POSTGRES_SETUP=yes" >> $GITHUB_ENV
else
echo "USE_LEGACY_POSTGRES_SETUP=no" >> $GITHUB_ENV
fi
-
uses: actions/checkout@v4

-
name: Restrict Sylius version
run: |
composer require --no-update --no-scripts sylius/sylius:${{ matrix.sylius }}
-
name: Build application
uses: SyliusLabs/BuildTestAppAction@v2.2
with:
build_type: "plugin"
cache_key: "${{ github.run_id }}-${{ runner.os }}-${{ hashFiles('composer.json') }}-sylius-${{ matrix.sylius }}-symfony-${{ matrix.symfony }}"
cache_restore_key: "${{ github.run_id }}-${{ runner.os }}-${{ hashFiles('composer.json') }}-sylius-${{ matrix.sylius }}-symfony-${{ matrix.symfony }}"
e2e: "yes"
e2e_js: "yes"
database: "postgresql"
database_version: ${{ matrix.postgres }}
legacy_postgresql_setup: ${{ env.USE_LEGACY_POSTGRES_SETUP }}
php_version: ${{ matrix.php }}
symfony_version: ${{ matrix.symfony }}

-
name: Validate composer.json
run: composer validate --ansi --strict

-
name: Validate database schema
run: (cd tests/Application && bin/console doctrine:schema:validate)

-
name: Run PHPStan
run: vendor/bin/phpstan analyse -c phpstan.neon -l max src/

-
name: Run Psalm
run: vendor/bin/psalm

-
name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction

-
name: Run PHPUnit
run: vendor/bin/phpunit --colors=always

-
name: Run Behat
run: vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun

-
name: Upload Behat logs
uses: actions/upload-artifact@v3
if: failure()
with:
name: Behat logs
path: etc/build/
if-no-files-found: ignore
37 changes: 34 additions & 3 deletions migrations/Version20230630102308.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,57 @@

namespace CommerceWeaversSyliusAlsoBoughtMigrations;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Sylius\Bundle\CoreBundle\Doctrine\Migrations\AbstractMigration;

final class Version20230630102308 extends AbstractMigration
{
public function preUp(Schema $schema): void
{
// Intentionally left empty to override the parent method
}

public function preDown(Schema $schema): void
{
// Intentionally left empty to override the parent method
}

public function getDescription(): string
{
return 'Setup the database for the plugin';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE sylius_also_bought_product_synchronization (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', start_date DATETIME NOT NULL, end_date DATETIME DEFAULT NULL, number_of_orders INT NOT NULL, affected_products JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->abortIf(
!$this->isMySql() && !$this->isPostgreSQL(),
'This migration can only be executed on \'MySQL\' or \'PostgreSQL\'.',
);

if ($this->isMySql()) {
$this->addSql('CREATE TABLE sylius_also_bought_product_synchronization (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', start_date DATETIME NOT NULL, end_date DATETIME DEFAULT NULL, number_of_orders INT NOT NULL, affected_products JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE sylius_product ADD bought_together_products JSON NOT NULL');
$this->addSql('UPDATE sylius_product SET bought_together_products = JSON_ARRAY()');

return;
}

$this->addSql('CREATE TABLE sylius_also_bought_product_synchronization (id UUID NOT NULL, start_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, end_date TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, number_of_orders INT NOT NULL, affected_products JSON NOT NULL, PRIMARY KEY(id))');
$this->addSql('COMMENT ON COLUMN sylius_also_bought_product_synchronization.id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE sylius_product ADD bought_together_products JSON NOT NULL');
$this->addSql('UPDATE sylius_product SET bought_together_products = JSON_ARRAY()');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE sylius_also_bought_product_synchronization');
$this->addSql('ALTER TABLE sylius_product DROP bought_together_products');
}

protected function isPostgreSQL(): bool
{
return
class_exists(PostgreSQLPlatform::class) &&
is_a($this->connection->getDatabasePlatform(), PostgreSQLPlatform::class, true);
}
}

0 comments on commit e3a587f

Please sign in to comment.