Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,43 @@ jobs:
pnpm exec tsc --noEmit
pnpm run build
git diff --exit-code -- dist

# The suite again on PostgreSQL, which rejects SQL that SQLite accepts (0.2.0
# shipped a FOR UPDATE on an aggregate that no PostgreSQL install could run).
test-postgres:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:18
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: testing
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 5s
--health-timeout 5s
--health-retries 10
env:
DB_CONNECTION: pgsql
DB_HOST: 127.0.0.1
DB_PORT: 5432
DB_DATABASE: testing
DB_USERNAME: postgres
DB_PASSWORD: postgres
steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
extensions: sodium, pdo_pgsql
coverage: none

- name: Install PHP dependencies
run: composer install --no-interaction --prefer-dist

- name: Test on PostgreSQL
run: composer run test

8 changes: 5 additions & 3 deletions app/Application/TaskNumberSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
/**
* Per-company task numbering.
*
* The number is `max(number) + 1` inside a transaction, which is portable
* across MySQL, PostgreSQL and SQLite. Two concurrent writers can still agree
* The number is the highest one plus one, read inside a transaction. The row
* holding it is read and locked rather than `max()` taken, because PostgreSQL
* refuses FOR UPDATE on an aggregate. Two concurrent writers can still agree
* on the same number; the unique index on `(company_id, number)` catches that
* and TaskService retries once.
*/
Expand All @@ -22,8 +23,9 @@ public function next(int $companyId): int
return DB::transaction(static function () use ($companyId): int {
$highest = Task::query()
->forCompany($companyId)
->orderByDesc('number')
->lockForUpdate()
->max('number');
->value('number');

return (int) $highest + 1;
});
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"requires": {},
"schema_version": 2,
"slug": "tasks-projects",
"version": "0.2.0",
"version": "0.2.1",
"license": "AGPL-3.0-only",
"compatibility": {
"invoiceshelf": ">=3.0.0-alpha.2 <4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invoiceshelf/module-tasks-projects",
"version": "0.2.0",
"version": "0.2.1",
"private": true,
"type": "module",
"packageManager": "pnpm@11.6.0",
Expand Down
27 changes: 21 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,27 @@ protected function getEnvironmentSetUp($app): void
{
$app['config']->set('app.key', 'base64:'.base64_encode(str_repeat('a', 32)));
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => true,
]);
// SQLite in memory by default; CI also runs the suite on PostgreSQL,
// which refuses SQL that SQLite lets through (DB_CONNECTION=pgsql).
$app['config']->set('database.connections.testing', getenv('DB_CONNECTION') === 'pgsql'
? [
'driver' => 'pgsql',
'host' => getenv('DB_HOST') ?: '127.0.0.1',
'port' => (int) (getenv('DB_PORT') ?: 5432),
'database' => getenv('DB_DATABASE') ?: 'testing',
'username' => getenv('DB_USERNAME') ?: 'postgres',
'password' => getenv('DB_PASSWORD') ?: '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
]
: [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => true,
]);
}

/**
Expand Down
Loading