-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for non unique state IDs #144
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1dc521e
Update snapshots to allow for duplicate state IDs
inxilpro f97548f
WIP
inxilpro 36f04ea
Merge branch 'main' into allow-for-non-unique-state-ids
inxilpro 53a98e5
Remove spatie migrations
inxilpro f6af692
Basic test
inxilpro 1b0bd3f
Fix styling
inxilpro 63abbb3
Prevent renamed migrations from running twice
inxilpro 6929345
WIP
inxilpro ce6dbeb
Quick test
inxilpro d20864e
Merge branch 'main' into allow-for-non-unique-state-ids
inxilpro fc2d9e2
Fix styling
inxilpro d7a79e8
Merge branch 'allow-for-non-unique-state-ids' of https://github.com/h…
inxilpro 1b62597
Upgrade details
inxilpro 1d23281
Fix styling
inxilpro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
## Upgrading to `v0.5.0` | ||
|
||
The structure of the `verbs_snapshots` table changed after version `0.4.5` to better account for | ||
non-Snowflake IDs (like ULIDs/etc). This change included: | ||
|
||
- Adding a new `id` column that is unique to the **snapshot** (the ID column had previously | ||
been mapped to the **state**, which caused issues if the different states of different types | ||
had the same ID) | ||
- Replaced the existing `id` column with a `state_id` that is not a primary index (allowing | ||
non-unique state IDs) | ||
- Changed the `unique` index on `state_id` and `type` to a regular index to allow for future features | ||
that may let you store multiple snapshots per state | ||
- Added an `expires_at` column to allow for snapshot purging in the future | ||
|
||
For more details about the change, please [see the Verbs PR](https://github.com/hirethunk/verbs/pull/144) | ||
that applied these changes. | ||
|
||
If you’re having trouble figuring out how to migrate your existing data, please check this page in | ||
a few days, or [ask on Discord](https://discord.gg/hDhZmD6ZC9) — we hope to have a sample migration | ||
ready shortly! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Thunk\Verbs\Attributes\Autodiscovery\StateId; | ||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\Facades\Verbs; | ||
use Thunk\Verbs\Models\VerbSnapshot; | ||
use Thunk\Verbs\State; | ||
|
||
it('only stores one snapshot per state', function () { | ||
$state1_id = snowflake_id(); | ||
$state2_id = snowflake_id(); | ||
|
||
DatabaseSnapshotTestEvent::fire(message: 'state 1, message 1', sid: $state1_id); | ||
DatabaseSnapshotTestEvent::fire(message: 'state 1, message 2', sid: $state1_id); | ||
DatabaseSnapshotTestEvent::fire(message: 'state 2, message 1', sid: $state2_id); | ||
DatabaseSnapshotTestEvent::fire(message: 'state 2, message 2', sid: $state2_id); | ||
|
||
Verbs::commit(); | ||
|
||
$snapshots = VerbSnapshot::all(); | ||
|
||
expect($snapshots)->toHaveCount(2) | ||
->and($snapshots->firstWhere('state_id', $state1_id)->state()->last_message)->toBe('state 1, message 2') | ||
->and($snapshots->firstWhere('state_id', $state2_id)->state()->last_message)->toBe('state 2, message 2'); | ||
|
||
DatabaseSnapshotTestEvent::fire(message: 'state 1, message 3', sid: $state1_id); | ||
DatabaseSnapshotTestEvent::fire(message: 'state 2, message 3', sid: $state2_id); | ||
|
||
Verbs::commit(); | ||
|
||
$snapshots = VerbSnapshot::all(); | ||
|
||
expect($snapshots)->toHaveCount(2) | ||
->and($snapshots->firstWhere('state_id', $state1_id)->state()->last_message)->toBe('state 1, message 3') | ||
->and($snapshots->firstWhere('state_id', $state2_id)->state()->last_message)->toBe('state 2, message 3'); | ||
}); | ||
|
||
class DatabaseSnapshotTestEvent extends Event | ||
{ | ||
public function __construct( | ||
public string $message, | ||
#[StateId(DatabaseSnapshotTestState::class)] public ?int $sid, | ||
) {} | ||
|
||
public function apply(DatabaseSnapshotTestState $state) | ||
{ | ||
$state->last_message = $this->message; | ||
} | ||
} | ||
|
||
class DatabaseSnapshotTestState extends State | ||
{ | ||
public string $last_message = ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use Glhd\Bits\Bits; | ||
use Ramsey\Uuid\UuidInterface; | ||
use Symfony\Component\Uid\AbstractUid; | ||
use Thunk\Verbs\Contracts\StoresSnapshots; | ||
use Thunk\Verbs\Models\VerbSnapshot; | ||
use Thunk\Verbs\State; | ||
|
||
it('can store multiple different states with the same ID', function () { | ||
$store = app(StoresSnapshots::class); | ||
|
||
$state1 = new SnapshotStoreTestStateOne(1, 'state one'); | ||
$state2 = new SnapshotStoreTestStateTwo(1, 'state two'); | ||
|
||
$store->write([$state1, $state2]); | ||
|
||
$snapshot1 = VerbSnapshot::where(['type' => SnapshotStoreTestStateOne::class, 'state_id' => 1])->sole(); | ||
$snapshot2 = VerbSnapshot::where(['type' => SnapshotStoreTestStateTwo::class, 'state_id' => 1])->sole(); | ||
|
||
expect($snapshot1)->state()->name->toBe('state one') | ||
->and($snapshot2)->state()->name->toBe('state two') | ||
->and($snapshot1)->id->not()->toBe($snapshot2->id); | ||
}); | ||
|
||
class SnapshotStoreTestStateOne extends State | ||
{ | ||
public function __construct( | ||
public Bits|UuidInterface|AbstractUid|int|string|null $id, | ||
public string $name, | ||
) {} | ||
} | ||
|
||
class SnapshotStoreTestStateTwo extends State | ||
{ | ||
public function __construct( | ||
public Bits|UuidInterface|AbstractUid|int|string|null $id, | ||
public string $name, | ||
) {} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the standard behavior of the
$default
value in one of these getters, but is convenient here. I could see an argument for making this more explicit in the calling code, but it would add a number of new lines of boilerplate…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is fine