Skip to content

Commit 9f49ce7

Browse files
committed
feat(db): Configuration item versioning for optimistic locking
1 parent 4465096 commit 9f49ce7

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ migrate: replenish
4444
migrate-to:
4545
@docker compose exec -u www-data web sh -c '. ./scripts/migrate-version.sh && ./orm migrations:migrate $$migrations --object-manager doctrine.entitymanager.$$alias'
4646

47-
migration-list: replenish
48-
@docker compose exec -u www-data -T web ./orm migrations:list --object-manager doctrine.entitymanager.orm_default
49-
@docker compose exec -u www-data -T web ./orm migrations:list --object-manager doctrine.entitymanager.orm_report
50-
5147
migration-diff: replenish
5248
@docker compose exec -u root web chown www-data:www-data /code/module/Database/migrations/
5349
@docker compose exec -u www-data -T web ./orm migrations:diff --object-manager doctrine.entitymanager.orm_default
@@ -58,10 +54,10 @@ migration-diff: replenish
5854
@docker cp "$(shell docker compose ps -q web)":/code/module/Report/migrations ./module/Report
5955
@docker compose exec -u root web chown -R root:root /code/module/Report/migrations/
6056

61-
migration-up: replenish migration-list
57+
migration-up: replenish
6258
@docker compose exec -u www-data web sh -c '. ./scripts/migrate-version.sh && ./orm migrations:execute --up $$migrations --object-manager doctrine.entitymanager.$$alias'
6359

64-
migration-down: replenish migration-list
60+
migration-down: replenish
6561
@docker compose exec -u www-data web sh -c '. ./scripts/migrate-version.sh && ./orm migrations:execute --down $$migrations --object-manager doctrine.entitymanager.$$alias'
6662

6763
seed: replenish

module/Application/src/Model/ConfigItem.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Application\Model\Enums\ConfigNamespaces;
88
use Database\Model\Trait\TimestampableTrait;
9+
use Database\Model\Trait\VersionTrait;
910
use DateTime;
1011
use Doctrine\ORM\Mapping\Column;
1112
use Doctrine\ORM\Mapping\Entity;
@@ -34,6 +35,11 @@
3435
class ConfigItem
3536
{
3637
use TimestampableTrait;
38+
// We implement locking by using version numbers (optimistic locking)
39+
// rather than by banning other processes from locking the same row.
40+
// This is more versatile and is possible because we do not care which
41+
// process in the end changes the config, as long as it is only one.
42+
use VersionTrait;
3743

3844
/**
3945
* Primary key item ID (to avoid reference issues).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Database\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
final class Version20251207150548 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Implement versioning of config items for optimistic locking';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql('ALTER TABLE configitem ADD version INT DEFAULT 1000 NOT NULL');
20+
}
21+
22+
public function down(Schema $schema): void
23+
{
24+
$this->addSql('ALTER TABLE ConfigItem DROP version');
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Database\Model\Trait;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Version;
9+
10+
trait VersionTrait
11+
{
12+
/**
13+
* integer version
14+
* From the docs:
15+
* "Version numbers [should] be preferred as they can not potentially conflict in a highly concurrent environment"
16+
*/
17+
#[Version()]
18+
#[Column(type: 'integer')]
19+
private int $version;
20+
}

scripts/migrate-list.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#/bin/sh
2+
3+
# This script prints the available migrations for a specific alias
4+
set -e
5+
6+
. /code/scripts/migrate-alias.sh
7+
8+
./orm migrations:list --no-interaction --object-manager doctrine.entitymanager.$alias
9+
10+
export alias=$alias
11+
export migrations=$migrations

scripts/migrate-version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# If you put this directly in the makefile, replace $ with $$
55
set -e
66

7-
. /code/scripts/migrate-alias.sh
7+
. /code/scripts/migrate-list.sh
88

99
read -rp "Give (partial, unique) version name (e.g. Database\Migrations\Version20241020224949 or 20241020)): " version
1010

0 commit comments

Comments
 (0)