From d860595ce1f972a580606fe8185a31c7f922ba02 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Mon, 5 Oct 2026 15:46:19 +0200 Subject: [PATCH] cleverage/ui-process-bundle#99 - Allow running the demo on PostgreSQL instead of MySQL - .docker/compose.yaml: postgres service (commented out), using the same process_bundle_demo_data volume as mysql; php-fpm depends_on to switch; .env: its DATABASE_URL, commented out - .docker/postgres/Dockerfile: postgres image with the host UID / GID, as the mysql one - .docker/php/Dockerfile: pdo_pgsql extension - migrations/Version20241210154810.php: PostgreSQL branch for the author / book tables - docs/postgresql.md: how to configure it when installing the project; README.md, docs/index.md: link to it Co-Authored-By: Claude Opus 5.5 (1M context) --- .docker/compose.yaml | 30 ++++++++++++++++++++ .docker/php/Dockerfile | 2 ++ .docker/postgres/Dockerfile | 14 ++++++++++ .env | 3 +- README.md | 3 ++ docs/index.md | 5 ++++ docs/postgresql.md | 41 ++++++++++++++++++++++++++++ migrations/Version20241210154810.php | 16 +++++++++-- 8 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 .docker/postgres/Dockerfile create mode 100644 docs/postgresql.md diff --git a/.docker/compose.yaml b/.docker/compose.yaml index b48962b..ff27072 100644 --- a/.docker/compose.yaml +++ b/.docker/compose.yaml @@ -46,6 +46,22 @@ x-base-mysql: &base-mysql MYSQL_USER: app MYSQL_PASSWORD: app +# PostgreSQL, instead of MySQL (see docs/postgresql.md) +#x-base-postgres: &base-postgres +# build: +# context: postgres +# args: +# UID: ${UID:-1000} +# GID: ${GID:-1000} +# POSTGRES_VERSION: ${POSTGRES_VERSION:-16} +# env_file: .env +# volumes: +# - process_bundle_demo_data:/var/lib/postgresql/data +# environment: +# POSTGRES_DB: app +# POSTGRES_USER: app +# POSTGRES_PASSWORD: app + x-base-sftp: &base-sftp image: atmoz/sftp volumes: @@ -62,8 +78,11 @@ services: <<: *base-php tty: true depends_on: + # PostgreSQL instead of MySQL (see docs/postgresql.md): comment mysql, uncomment postgres mysql: condition: service_healthy + #postgres: + # condition: service_healthy apache2: <<: *base-apache2 @@ -81,6 +100,17 @@ services: ports: - "${MYSQL_PORT:-3306}:3306" + # PostgreSQL instead of MySQL (see docs/postgresql.md): comment the mysql service above, uncomment this one + #postgres: + # <<: *base-postgres + # tty: true + # healthcheck: + # test: [ "CMD", "pg_isready", "-U", "app", "-d", "app" ] + # timeout: 5s + # retries: 100 + # ports: + # - "${POSTGRES_PORT:-5432}:5432" + sftp: <<: *base-sftp tty: true diff --git a/.docker/php/Dockerfile b/.docker/php/Dockerfile index d462b24..3134dad 100644 --- a/.docker/php/Dockerfile +++ b/.docker/php/Dockerfile @@ -24,10 +24,12 @@ RUN set -eux; \ libicu-dev \ libzip-dev \ libxml2-dev \ + libpq-dev \ && docker-php-ext-configure intl \ && docker-php-ext-install intl \ pdo \ pdo_mysql \ + pdo_pgsql \ zip \ soap \ && docker-php-ext-enable soap diff --git a/.docker/postgres/Dockerfile b/.docker/postgres/Dockerfile new file mode 100644 index 0000000..fb447f0 --- /dev/null +++ b/.docker/postgres/Dockerfile @@ -0,0 +1,14 @@ +ARG POSTGRES_VERSION=16 +FROM postgres:${POSTGRES_VERSION} + +ARG UNAME=postgres +ARG UID +ARG GID + +ENV TZ=Europe/Paris + +RUN usermod -u $UID $UNAME \ + && groupmod -g $GID $UNAME \ + && chown -R $UNAME:$UNAME /var/lib/postgresql /var/run/postgresql + +USER $UNAME:$UNAME diff --git a/.env b/.env index 84a94ae..5590f5a 100644 --- a/.env +++ b/.env @@ -34,7 +34,8 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 # DATABASE_URL="sqlite:///%kernel.project_dir%/var/data_%kernel.environment%.db" DATABASE_URL="mysql://app:app@mysql:3306/app?serverVersion=9.1.0&charset=utf8mb4" # DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4" -# DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8" +# PostgreSQL service of .docker/compose.yaml, instead of MySQL (see docs/postgresql.md) +# DATABASE_URL="postgresql://app:app@postgres:5432/app?serverVersion=16&charset=utf8" ###< doctrine/doctrine-bundle ### ###> docker/atmoz-sftp ### diff --git a/README.md b/README.md index f4a9c2b..cfb72e0 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ CleverAge/ProcessBundleDemo A demo project using cleverage/process-bundle & cleverage/*-process-bundle bridges +The demo runs on MySQL by default. To run it on PostgreSQL, configure it before installing the project (first +`make start`): see [running the demo on PostgreSQL](docs/postgresql.md). + ## Documentation For usage documentation, see: diff --git a/docs/index.md b/docs/index.md index b47580f..88b1cd9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,6 +6,11 @@ make start Then access to UI via http://process-bundle-demo.localhost/process (username: admin@clever-age.com / password: admin@clever-age.com) +## PostgreSQL + +The demo runs on MySQL by default. To run it on PostgreSQL, configure it before the first `make start`: see +[running the demo on PostgreSQL](postgresql.md). + ## Mocking APIs See [Mocking APIs with WireMock](wiremock.md): the `wiremock` service and the `demo.wiremock.*` processes. diff --git a/docs/postgresql.md b/docs/postgresql.md new file mode 100644 index 0000000..870d054 --- /dev/null +++ b/docs/postgresql.md @@ -0,0 +1,41 @@ +Running the demo on PostgreSQL +============================== + +The demo runs on MySQL by default. It can run on PostgreSQL instead: the bundle migrations +(`cleverage/ui-process-bundle`), the demo migration (`author` / `book` tables) and the fixtures support both databases. + +The database is chosen **when installing the project**, before the first `make start`. + +## Configuration + +1. In `.docker/compose.yaml`: + - comment out the `mysql` service and uncomment the `postgres` service (and the `x-base-postgres` block at the top + of the file); + - in the `depends_on` of the `php-fpm` service, comment out `mysql` and uncomment `postgres`. +2. In `.env.local`, point Doctrine to PostgreSQL (the URL is also in `.env`, commented out): + + ```dotenv + DATABASE_URL="postgresql://app:app@postgres:5432/app?serverVersion=16&charset=utf8" + ``` + +3. Install and start the demo as usual: + + ```bash + make start + ``` + + It runs the migrations and loads the fixtures on the PostgreSQL database. The UI is at + http://process-bundle-demo.localhost/process (login `admin@clever-age.com` / `admin@clever-age.com`). + +Both databases use the same `process_bundle_demo_data` volume. + +## Variables + +| Variable | Default | Description | +|--------------------|---------|-------------------------------------------------| +| `POSTGRES_VERSION` | `16` | Tag of the `postgres:` image. | +| `POSTGRES_PORT` | `5432` | Port published on the host. | + +If you change `POSTGRES_VERSION`, also change `serverVersion` in `DATABASE_URL`. + +Connect to the database: `docker compose -f .docker/compose.yaml exec postgres psql -U app app`. diff --git a/migrations/Version20241210154810.php b/migrations/Version20241210154810.php index a05a0a4..5319ff4 100644 --- a/migrations/Version20241210154810.php +++ b/migrations/Version20241210154810.php @@ -4,6 +4,7 @@ namespace DoctrineMigrations; +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; @@ -19,7 +20,15 @@ public function getDescription(): string public function up(Schema $schema): void { - // this up() migration is auto-generated, please modify it to your needs + if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { + $this->addSql('CREATE TABLE author (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, firstname VARCHAR(255) NOT NULL, lastname VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE TABLE book (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, author_id INT NOT NULL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_CBE5A331F675F31B ON book (author_id)'); + $this->addSql('ALTER TABLE book ADD CONSTRAINT FK_CBE5A331F675F31B FOREIGN KEY (author_id) REFERENCES author (id) NOT DEFERRABLE'); + + return; + } + $this->addSql('CREATE TABLE author (id INT AUTO_INCREMENT NOT NULL, firstname VARCHAR(255) NOT NULL, lastname VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, author_id INT NOT NULL, title VARCHAR(255) NOT NULL, INDEX IDX_CBE5A331F675F31B (author_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('ALTER TABLE book ADD CONSTRAINT FK_CBE5A331F675F31B FOREIGN KEY (author_id) REFERENCES author (id)'); @@ -27,8 +36,9 @@ public function up(Schema $schema): void public function down(Schema $schema): void { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('ALTER TABLE book DROP FOREIGN KEY FK_CBE5A331F675F31B'); + $this->addSql($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform + ? 'ALTER TABLE book DROP CONSTRAINT FK_CBE5A331F675F31B' + : 'ALTER TABLE book DROP FOREIGN KEY FK_CBE5A331F675F31B'); $this->addSql('DROP TABLE author'); $this->addSql('DROP TABLE book'); }