Skip to content

Add cross-driver testsuite for MySQL and Postgres#119

Merged
alganet merged 1 commit intoRespect:masterfrom
alganet:mysql-postgres
Apr 27, 2026
Merged

Add cross-driver testsuite for MySQL and Postgres#119
alganet merged 1 commit intoRespect:masterfrom
alganet:mysql-postgres

Conversation

@alganet
Copy link
Copy Markdown
Member

@alganet alganet commented Apr 27, 2026

Refactor DbTest and MapperTest onto a shared DatabaseTestCase so the existing tests run against SQLite, MySQL, and Postgres. The driver, DSN, and credentials are selected from environment variables; default to in-memory SQLite to preserve the current zero-config experience. A docker-compose.yml provides MySQL and Postgres locally on non-default ports, and CI fans out into a three-driver matrix using GitHub Actions services.

Fix Mapper::checkNewIdentity to wrap lastInsertId() in a savepoint on Postgres. After an INSERT with an explicit id, Postgres errors with "lastval is not yet defined in this session" and marks the surrounding transaction as aborted; the catch swallowed the exception but the subsequent commit then discarded the row. The savepoint contains the abort. MySQL is excluded because issuing SAVEPOINT between the INSERT and LAST_INSERT_ID() resets the latter to 0.

Also fix four raw-SQL portability bugs in MapperTest that SQLite and MySQL had hidden: double-quoted string literals (Postgres reads them as identifiers) and a VARCHAR/integer comparison.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.72%. Comparing base (a3c9be3) to head (3855e48).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master     #119   +/-   ##
=========================================
  Coverage     99.71%   99.72%           
- Complexity      145      149    +4     
=========================================
  Files             3        3           
  Lines           353      362    +9     
=========================================
+ Hits            352      361    +9     
  Misses            1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alganet alganet requested a review from Copilot April 27, 2026 18:25
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the test harness so the existing DB/Mapper integration tests can run against multiple PDO backends (SQLite/MySQL/Postgres) selected via environment variables, and it adjusts Mapper::checkNewIdentity() to avoid Postgres transaction aborts when lastInsertId() is undefined.

Changes:

  • Introduce a shared DatabaseTestCase + ConnectionFactory to run tests across SQLite/MySQL/Postgres (defaulting to in-memory SQLite).
  • Add driver-aware DDL helpers (Schema::pkAuto(), Schema::dateTime()) and fix raw SQL portability issues in tests.
  • Wrap Postgres lastInsertId() inside a savepoint to contain transaction aborts, with targeted tests for the behavior.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/MapperTest.php Migrates to shared DB test case, uses driver-aware schema, adds savepoint behavior tests, and fixes SQL portability issues.
tests/DbTest.php Migrates to shared DB test case and makes one SQLite-specific assertion explicitly skipped on other drivers.
tests/Database/Schema.php Adds driver-specific DDL fragments for auto PK and datetime types.
tests/Database/DriverUnavailable.php Adds a typed exception for missing PDO drivers to cleanly skip tests.
tests/Database/DatabaseTestCase.php Adds shared connection setup, table reset helper, and Postgres sequence resync helper.
tests/Database/ConnectionFactory.php Adds env-driven PDO connection creation with default DSNs for local docker ports.
src/Mapper.php Adds savepoint wrapping around Postgres lastInsertId() to prevent transaction abort from discarding inserts.
phpunit.xml.dist Splits tests into unit vs database testsuites by explicit file list.
docker-compose.yml Adds local MySQL/Postgres services on non-default host ports with healthchecks.
composer.json Adds convenience scripts to run PHPUnit against sqlite/mysql/pgsql (and all in sequence).
CONTRIBUTING.md Documents how to run the multi-driver tests locally with docker compose and composer scripts.
.github/workflows/ci.yml Fans CI tests out into separate sqlite/mysql/pgsql jobs using GitHub Actions services.
.env.example Documents supported DB environment variables for the integration tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/Database/DatabaseTestCase.php Outdated
Comment thread tests/Database/ConnectionFactory.php
Comment thread src/Mapper.php
@alganet alganet marked this pull request as ready for review April 27, 2026 18:40
Refactor DbTest and MapperTest onto a shared DatabaseTestCase so the
existing tests run against SQLite, MySQL, and Postgres. The driver,
DSN, and credentials are selected from environment variables; default
to in-memory SQLite to preserve the current zero-config experience.
A docker-compose.yml provides MySQL and Postgres locally on non-default
ports, and CI fans out into a three-driver matrix using GitHub Actions
services.

Fix Mapper::checkNewIdentity to wrap lastInsertId() in a savepoint on
Postgres. After an INSERT with an explicit id, Postgres errors with
"lastval is not yet defined in this session" and marks the surrounding
transaction as aborted; the catch swallowed the exception but the
subsequent commit then discarded the row. The savepoint contains the
abort. MySQL is excluded because issuing SAVEPOINT between the INSERT
and LAST_INSERT_ID() resets the latter to 0.

Also fix four raw-SQL portability bugs in MapperTest that SQLite and
MySQL had hidden: double-quoted string literals (Postgres reads them
as identifiers) and a VARCHAR/integer comparison.
@alganet alganet merged commit 4b030d4 into Respect:master Apr 27, 2026
5 checks passed
@alganet alganet deleted the mysql-postgres branch April 27, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants