Skip to content

Commit

Permalink
Merge branch 'main' into outside-listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Sep 20, 2024
2 parents 9c4ecd3 + 738bf58 commit 125c584
Show file tree
Hide file tree
Showing 70 changed files with 1,581 additions and 535 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": ">=8.1",
"glhd/bits": "^0.4|^0.3",
"glhd/bits": ">=0.3.0",
"illuminate/contracts": "^10.0|^11.0",
"internachi/modular": "^2.0",
"laravel/prompts": "^0.1.15",
Expand Down
31 changes: 15 additions & 16 deletions config/verbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
PropertyNormalizer::NORMALIZE_VISIBILITY => PropertyNormalizer::NORMALIZE_PUBLIC,
],

/*
|--------------------------------------------------------------------------
| Connection Names
|--------------------------------------------------------------------------
|
| By default, Verbs will use your default database connection, However, you may
| wish to customize these connection names to better fit your application.
|
*/
'connections' => [
'events' => env('VERBS_EVENTS_CONNECTION'),
'snapshots' => env('VERBS_SNAPSHOT_CONNECTION'),
'state_events' => env('VERBS_STATE_EVENTS_CONNECTION'),
],

/*
|--------------------------------------------------------------------------
| Table Names
Expand Down Expand Up @@ -108,20 +123,4 @@
| If you want to always manually commit, you can disable auto-commit.
*/
'autocommit' => true,

/*
|--------------------------------------------------------------------------
| State Cache Size
|--------------------------------------------------------------------------
|
| By default, Verbs will keep up to 100 State objects in memory at one time.
| Most applications will never need more than a handful of States for any
| given request, so this limit will only make any difference when you're
| replaying events.
|
| If your application needs to operate on more than 100 States at any given
| time, you should increase this setting to some value higher than the
| maximum number of States you will ever need.
*/
'state_cache_size' => 100,
];
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
{
public function up()
{
Schema::create($this->tableName(), function (Blueprint $table) {
// If they've already migrated under the previous migration name, just skip
if (Schema::connection($this->connectionName())->hasTable($this->tableName())) {
return;
}

Schema::connection($this->connectionName())->create($this->tableName(), function (Blueprint $table) {
$table->snowflakeId();

$table->string('type')->index();
Expand All @@ -21,7 +26,12 @@ public function up()

public function down()
{
Schema::dropIfExists($this->tableName());
Schema::connection($this->connectionName())->dropIfExists($this->tableName());
}

protected function connectionName(): ?string
{
return config('verbs.connections.events');
}

protected function tableName(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

use Glhd\Bits\Contracts\MakesSnowflakes;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Thunk\Verbs\Facades\Id;

return new class extends Migration
{
public function up()
{
// If we migrated before Verbs 0.5.0 we need to do a little extra work
$migrating = Schema::connection($this->connectionName())->hasTable($this->tableName());

if ($migrating) {
Schema::connection($this->connectionName())->rename($this->tableName(), '__verbs_snapshots_pre_050');
}

Schema::connection($this->connectionName())->create($this->tableName(), function (Blueprint $table) {
$table->snowflakeId();

// The 'state_id' column needs to be set up differently depending on
// if you're using Snowflakes vs. ULIDs/etc.
Id::createColumnDefinition($table, 'state_id');

$table->string('type')->index();
$table->json('data');

$table->snowflake('last_event_id')->nullable();

$table->timestamp('expires_at')->nullable()->index();
$table->timestamps();

$table->index(['state_id', 'type']);
});

if ($migrating) {
DB::connection($this->connectionName())
->table('__verbs_snapshots_pre_050')
->select('*')
->chunkById(100, $this->migrateChunk(...));
}
}

public function down()
{
Schema::connection($this->connectionName())->dropIfExists($this->tableName());

if (Schema::connection($this->connectionName())->hasTable('__verbs_snapshots_pre_050')) {
Schema::connection($this->connectionName())->rename('__verbs_snapshots_pre_050', $this->tableName());
}
}

protected function migrateChunk(Collection $chunk): void
{
$rows = $chunk->map(fn ($row) => [
'id' => app(MakesSnowflakes::class)->makeFromTimestamp(Date::parse($row->created_at))->id(),
'type' => $row->type,
'state_id' => $row->id,
'data' => $row->data,
'last_event_id' => $row->last_event_id,
'expires_at' => null,
'created_at' => $row->created_at,
'updated_at' => $row->updated_at,
]);

DB::connection($this->connectionName())->table($this->tableName())->insert($rows->toArray());
}

protected function connectionName(): ?string
{
return config('verbs.connections.snapshots');
}

protected function tableName(): string
{
return config('verbs.tables.snapshots', 'verb_snapshots');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
{
public function up()
{
Schema::create($this->tableName(), function (Blueprint $table) {
// If they've already migrated under the previous migration name, just skip
if (Schema::connection($this->connectionName())->hasTable($this->tableName())) {
return;
}

Schema::connection($this->connectionName())->create($this->tableName(), function (Blueprint $table) {
$table->snowflakeId();

$table->snowflake('event_id')->index();
Expand All @@ -26,7 +31,12 @@ public function up()

public function down()
{
Schema::dropIfExists($this->tableName());
Schema::connection($this->connectionName())->dropIfExists($this->tableName());
}

protected function connectionName(): ?string
{
return config('verbs.connections.state_events');
}

protected function tableName(): string
Expand Down
37 changes: 0 additions & 37 deletions database/migrations/create_verb_snapshots_table.php

This file was deleted.

19 changes: 19 additions & 0 deletions docs/ids.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ class JobApplication extends Model
}
```

Bits also provides helpers for your migrations:

```php
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('job_applications', function (Blueprint $table) {
$table->snowflakeId();
$table->snowflake('user_id')->index();
$table->foreign('user_id')->references('id')->on('users');
// ...
});
}
```

The `snowflakeId()` method creates a new primary key column with a default name of 'id'. The `snowflake()` method adds a regular snowflake column which is ideal for creating foreign keys.

### Automatically generate snowflake ids

Verbs allows for `snowflake_id` auto-generation by default when using most of our [attributes](/docs/technical/attributes).
Expand Down
5 changes: 5 additions & 0 deletions docs/navigation.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"title": "Quickstart",
"slug": "quickstart",
"icon": "arrow-right"
},
{
"title": "Upgrading",
"slug": "upgrading",
"icon": "map"
}
]
},
Expand Down
Loading

0 comments on commit 125c584

Please sign in to comment.