Skip to content

Commit bd6672a

Browse files
committed
add action and schema source hash to add only a commit in case the source has changed
1 parent 8199039 commit bd6672a

File tree

13 files changed

+154
-26
lines changed

13 files changed

+154
-26
lines changed

src/Migrations/Version20230508210151.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function up(Schema $schema) : void
4747
$actionCommitTable->addColumn('user_id', 'integer');
4848
$actionCommitTable->addColumn('prev_hash', 'string', ['length' => 40]);
4949
$actionCommitTable->addColumn('commit_hash', 'string', ['length' => 40]);
50+
$actionCommitTable->addColumn('config_hash', 'string', ['length' => 40]);
5051
$actionCommitTable->addColumn('config', 'text');
5152
$actionCommitTable->addColumn('insert_date', 'datetime');
5253
$actionCommitTable->setPrimaryKey(['id']);
@@ -499,6 +500,7 @@ public function up(Schema $schema) : void
499500
$schemaCommitTable->addColumn('user_id', 'integer');
500501
$schemaCommitTable->addColumn('prev_hash', 'string', ['length' => 40]);
501502
$schemaCommitTable->addColumn('commit_hash', 'string', ['length' => 40]);
503+
$schemaCommitTable->addColumn('source_hash', 'string', ['length' => 40]);
502504
$schemaCommitTable->addColumn('source', 'text');
503505
$schemaCommitTable->addColumn('insert_date', 'datetime');
504506
$schemaCommitTable->setPrimaryKey(['id']);

src/Migrations/Version20260204195815.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function up(Schema $schema): void
2626
$actionCommitTable->addColumn('user_id', 'integer');
2727
$actionCommitTable->addColumn('prev_hash', 'string', ['length' => 40]);
2828
$actionCommitTable->addColumn('commit_hash', 'string', ['length' => 40]);
29+
$actionCommitTable->addColumn('config_hash', 'string', ['length' => 40]);
2930
$actionCommitTable->addColumn('config', 'text');
3031
$actionCommitTable->addColumn('insert_date', 'datetime');
3132
$actionCommitTable->setPrimaryKey(['id']);
@@ -42,6 +43,7 @@ public function up(Schema $schema): void
4243
$schemaCommitTable->addColumn('user_id', 'integer');
4344
$schemaCommitTable->addColumn('prev_hash', 'string', ['length' => 40]);
4445
$schemaCommitTable->addColumn('commit_hash', 'string', ['length' => 40]);
46+
$schemaCommitTable->addColumn('source_hash', 'string', ['length' => 40]);
4547
$schemaCommitTable->addColumn('source', 'text');
4648
$schemaCommitTable->addColumn('insert_date', 'datetime');
4749
$schemaCommitTable->setPrimaryKey(['id']);

src/Service/Action/Committer.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,30 @@ public function commit(int $actionId, ?string $config, UserContext $context): ?s
4444
return null;
4545
}
4646

47-
$previousHash = $this->actionCommitTable->findCurrentHash($actionId);
47+
[$previousCommitHash, $previousConfigHash] = $this->actionCommitTable->findCurrentHash($actionId);
4848

49-
$hash = sha1($context->getTenantId() . $context->getUserId() . $actionId . $previousHash . $config);
49+
$configHash = sha1($config);
50+
if ($configHash === $previousConfigHash) {
51+
return null;
52+
}
53+
54+
$commitHash = sha1($context->getTenantId() . $context->getUserId() . $actionId . $previousCommitHash . $config);
5055

51-
$existing = $this->actionCommitTable->findOneByCommitHash($hash);
56+
$existing = $this->actionCommitTable->findOneByCommitHash($commitHash);
5257
if ($existing instanceof Table\Generated\ActionCommitRow) {
5358
return null;
5459
}
5560

5661
$row = new Table\Generated\ActionCommitRow();
5762
$row->setActionId($actionId);
5863
$row->setUserId($context->getUserId());
59-
$row->setPrevHash($previousHash ?? '');
60-
$row->setCommitHash($hash);
64+
$row->setPrevHash($previousCommitHash ?? '');
65+
$row->setCommitHash($commitHash);
66+
$row->setConfigHash($configHash);
6167
$row->setConfig($config);
6268
$row->setInsertDate(LocalDateTime::now());
6369
$this->actionCommitTable->create($row);
6470

65-
return $hash;
71+
return $commitHash;
6672
}
6773
}

src/Service/Operation.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ private function fixSchemaToCurrentCommitHash(?string $schema, UserContext $cont
282282
return $schema;
283283
}
284284

285-
$schemaHash = $this->schemaCommitTable->findCurrentHash($row->getId());
286-
if (empty($schemaHash)) {
285+
[$commitHash] = $this->schemaCommitTable->findCurrentHash($row->getId());
286+
if (empty($commitHash)) {
287287
return $schema;
288288
}
289289

290-
return 'schema://' . $name . '@' . $schemaHash;
290+
return 'schema://' . $name . '@' . $commitHash;
291291
}
292292

293293
private function fixActionToCurrentCommitHash(?string $action, UserContext $context): ?string
@@ -312,12 +312,12 @@ private function fixActionToCurrentCommitHash(?string $action, UserContext $cont
312312
return $action;
313313
}
314314

315-
$actionHash = $this->actionCommitTable->findCurrentHash($row->getId());
316-
if (empty($actionHash)) {
315+
[$commitHash] = $this->actionCommitTable->findCurrentHash($row->getId());
316+
if (empty($commitHash)) {
317317
return $action;
318318
}
319319

320-
return 'action://' . $name . '@' . $actionHash;
320+
return 'action://' . $name . '@' . $commitHash;
321321
}
322322

323323
private function shouldNotChange(?int $stability): bool

src/Service/Schema/Committer.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,34 @@ public function __construct(
4040

4141
public function commit(int $schemaId, string $source, UserContext $context): ?string
4242
{
43-
$previousHash = $this->schemaCommitTable->findCurrentHash($schemaId);
43+
if (empty($source)) {
44+
return null;
45+
}
46+
47+
[$previousCommitHash, $previousSourceHash] = $this->schemaCommitTable->findCurrentHash($schemaId);
48+
49+
$sourceHash = sha1($source);
50+
if ($sourceHash === $previousSourceHash) {
51+
return null;
52+
}
4453

45-
$hash = sha1($context->getTenantId() . $context->getUserId() . $schemaId . $previousHash . $source);
54+
$commitHash = sha1($context->getTenantId() . $context->getUserId() . $schemaId . $previousCommitHash . $source);
4655

47-
$existing = $this->schemaCommitTable->findOneByCommitHash($hash);
56+
$existing = $this->schemaCommitTable->findOneByCommitHash($commitHash);
4857
if ($existing instanceof Table\Generated\SchemaCommitRow) {
4958
return null;
5059
}
5160

5261
$row = new Table\Generated\SchemaCommitRow();
5362
$row->setSchemaId($schemaId);
5463
$row->setUserId($context->getUserId());
55-
$row->setPrevHash($previousHash ?? '');
56-
$row->setCommitHash($hash);
64+
$row->setPrevHash($previousCommitHash ?? '');
65+
$row->setCommitHash($commitHash);
66+
$row->setSourceHash($sourceHash);
5767
$row->setSource($source);
5868
$row->setInsertDate(LocalDateTime::now());
5969
$this->schemaCommitTable->create($row);
6070

61-
return $hash;
71+
return $commitHash;
6272
}
6373
}

src/Table/Action/Commit.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@
3131
*/
3232
class Commit extends Generated\ActionCommitTable
3333
{
34-
public function findCurrentHash(int $actionId): ?string
34+
public function findCurrentHash(int $actionId): array
3535
{
36-
$hash = $this->connection->fetchOne('SELECT commit_hash FROM fusio_action_commit WHERE action_id = :action_id ORDER BY id DESC', [
36+
$row = $this->connection->fetchAssociative('SELECT commit_hash, config_hash FROM fusio_action_commit WHERE action_id = :action_id ORDER BY id DESC', [
3737
'action_id' => $actionId,
3838
]);
3939

40-
return !empty($hash) ? $hash : null;
40+
if (empty($row)) {
41+
return [null, null];
42+
}
43+
44+
return [$row['commit_hash'], $row['config_hash']];
4145
}
4246
}

src/Table/Generated/ActionCommitColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ enum ActionCommitColumn : string implements \PSX\Sql\ColumnInterface
99
case USER_ID = \Fusio\Impl\Table\Generated\ActionCommitTable::COLUMN_USER_ID;
1010
case PREV_HASH = \Fusio\Impl\Table\Generated\ActionCommitTable::COLUMN_PREV_HASH;
1111
case COMMIT_HASH = \Fusio\Impl\Table\Generated\ActionCommitTable::COLUMN_COMMIT_HASH;
12+
case CONFIG_HASH = \Fusio\Impl\Table\Generated\ActionCommitTable::COLUMN_CONFIG_HASH;
1213
case CONFIG = \Fusio\Impl\Table\Generated\ActionCommitTable::COLUMN_CONFIG;
1314
case INSERT_DATE = \Fusio\Impl\Table\Generated\ActionCommitTable::COLUMN_INSERT_DATE;
1415
}

src/Table/Generated/ActionCommitRow.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ActionCommitRow implements \JsonSerializable, \PSX\Record\RecordableInterf
99
private ?int $userId = null;
1010
private ?string $prevHash = null;
1111
private ?string $commitHash = null;
12+
private ?string $configHash = null;
1213
private ?string $config = null;
1314
private ?\PSX\DateTime\LocalDateTime $insertDate = null;
1415
public function setId(int $id): void
@@ -51,6 +52,14 @@ public function getCommitHash(): string
5152
{
5253
return $this->commitHash ?? throw new \PSX\Sql\Exception\NoValueAvailable('No value for required column "commit_hash" was provided');
5354
}
55+
public function setConfigHash(string $configHash): void
56+
{
57+
$this->configHash = $configHash;
58+
}
59+
public function getConfigHash(): string
60+
{
61+
return $this->configHash ?? throw new \PSX\Sql\Exception\NoValueAvailable('No value for required column "config_hash" was provided');
62+
}
5463
public function setConfig(string $config): void
5564
{
5665
$this->config = $config;
@@ -76,6 +85,7 @@ public function toRecord(): \PSX\Record\RecordInterface
7685
$record->put('user_id', $this->userId);
7786
$record->put('prev_hash', $this->prevHash);
7887
$record->put('commit_hash', $this->commitHash);
88+
$record->put('config_hash', $this->configHash);
7989
$record->put('config', $this->config);
8090
$record->put('insert_date', $this->insertDate);
8191
return $record;
@@ -92,6 +102,7 @@ public static function from(array|\ArrayAccess $data): self
92102
$row->userId = isset($data['user_id']) && is_int($data['user_id']) ? $data['user_id'] : null;
93103
$row->prevHash = isset($data['prev_hash']) && is_string($data['prev_hash']) ? $data['prev_hash'] : null;
94104
$row->commitHash = isset($data['commit_hash']) && is_string($data['commit_hash']) ? $data['commit_hash'] : null;
105+
$row->configHash = isset($data['config_hash']) && is_string($data['config_hash']) ? $data['config_hash'] : null;
95106
$row->config = isset($data['config']) && is_string($data['config']) ? $data['config'] : null;
96107
$row->insertDate = isset($data['insert_date']) && $data['insert_date'] instanceof \DateTimeInterface ? \PSX\DateTime\LocalDateTime::from($data['insert_date']) : null;
97108
return $row;

src/Table/Generated/ActionCommitTable.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ActionCommitTable extends \PSX\Sql\TableAbstract
1313
public const COLUMN_USER_ID = 'user_id';
1414
public const COLUMN_PREV_HASH = 'prev_hash';
1515
public const COLUMN_COMMIT_HASH = 'commit_hash';
16+
public const COLUMN_CONFIG_HASH = 'config_hash';
1617
public const COLUMN_CONFIG = 'config';
1718
public const COLUMN_INSERT_DATE = 'insert_date';
1819
public function getName(): string
@@ -21,7 +22,7 @@ public function getName(): string
2122
}
2223
public function getColumns(): array
2324
{
24-
return [self::COLUMN_ID => 0x3020000a, self::COLUMN_ACTION_ID => 0x20000a, self::COLUMN_USER_ID => 0x20000a, self::COLUMN_PREV_HASH => 0xa00028, self::COLUMN_COMMIT_HASH => 0xa00028, self::COLUMN_CONFIG => 0xb00000, self::COLUMN_INSERT_DATE => 0x800000];
25+
return [self::COLUMN_ID => 0x3020000a, self::COLUMN_ACTION_ID => 0x20000a, self::COLUMN_USER_ID => 0x20000a, self::COLUMN_PREV_HASH => 0xa00028, self::COLUMN_COMMIT_HASH => 0xa00028, self::COLUMN_CONFIG_HASH => 0xa00028, self::COLUMN_CONFIG => 0xb00000, self::COLUMN_INSERT_DATE => 0x800000];
2526
}
2627
/**
2728
* @return array<\Fusio\Impl\Table\Generated\ActionCommitRow>
@@ -240,6 +241,43 @@ public function deleteByCommitHash(string $value): int
240241
$condition->like('commit_hash', $value);
241242
return $this->doDeleteBy($condition);
242243
}
244+
/**
245+
* @return array<\Fusio\Impl\Table\Generated\ActionCommitRow>
246+
* @throws \PSX\Sql\Exception\QueryException
247+
*/
248+
public function findByConfigHash(string $value, ?int $startIndex = null, ?int $count = null, ?\Fusio\Impl\Table\Generated\ActionCommitColumn $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null): array
249+
{
250+
$condition = \PSX\Sql\Condition::withAnd();
251+
$condition->like('config_hash', $value);
252+
return $this->doFindBy($condition, $startIndex, $count, $sortBy, $sortOrder);
253+
}
254+
/**
255+
* @throws \PSX\Sql\Exception\QueryException
256+
*/
257+
public function findOneByConfigHash(string $value): ?\Fusio\Impl\Table\Generated\ActionCommitRow
258+
{
259+
$condition = \PSX\Sql\Condition::withAnd();
260+
$condition->like('config_hash', $value);
261+
return $this->doFindOneBy($condition);
262+
}
263+
/**
264+
* @throws \PSX\Sql\Exception\ManipulationException
265+
*/
266+
public function updateByConfigHash(string $value, \Fusio\Impl\Table\Generated\ActionCommitRow $record): int
267+
{
268+
$condition = \PSX\Sql\Condition::withAnd();
269+
$condition->like('config_hash', $value);
270+
return $this->doUpdateBy($condition, $record->toRecord());
271+
}
272+
/**
273+
* @throws \PSX\Sql\Exception\ManipulationException
274+
*/
275+
public function deleteByConfigHash(string $value): int
276+
{
277+
$condition = \PSX\Sql\Condition::withAnd();
278+
$condition->like('config_hash', $value);
279+
return $this->doDeleteBy($condition);
280+
}
243281
/**
244282
* @return array<\Fusio\Impl\Table\Generated\ActionCommitRow>
245283
* @throws \PSX\Sql\Exception\QueryException

src/Table/Generated/SchemaCommitColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ enum SchemaCommitColumn : string implements \PSX\Sql\ColumnInterface
99
case USER_ID = \Fusio\Impl\Table\Generated\SchemaCommitTable::COLUMN_USER_ID;
1010
case PREV_HASH = \Fusio\Impl\Table\Generated\SchemaCommitTable::COLUMN_PREV_HASH;
1111
case COMMIT_HASH = \Fusio\Impl\Table\Generated\SchemaCommitTable::COLUMN_COMMIT_HASH;
12+
case SOURCE_HASH = \Fusio\Impl\Table\Generated\SchemaCommitTable::COLUMN_SOURCE_HASH;
1213
case SOURCE = \Fusio\Impl\Table\Generated\SchemaCommitTable::COLUMN_SOURCE;
1314
case INSERT_DATE = \Fusio\Impl\Table\Generated\SchemaCommitTable::COLUMN_INSERT_DATE;
1415
}

0 commit comments

Comments
 (0)