@@ -179,40 +179,77 @@ public function status(): Status
179
179
180
180
/** @var SchemaBuilder $schema */
181
181
$ schema = $ this ->connection ->getSchemaBuilder ();
182
- if (!$ schema ->hasTable ($ this ->eventTableName )) {
182
+ if (! $ schema ->hasTable ($ this ->eventTableName )) {
183
183
return Status::setupRequired (
184
184
sprintf ('Table `%s` does not exist. ' , $ this ->eventTableName )
185
185
);
186
186
}
187
187
188
+ // detect schema drift: make sure all the columns we created in setup() are still here
189
+ $ actual = $ schema ->getColumnListing ($ this ->eventTableName );
190
+ $ expected = [
191
+ 'sequencenumber ' ,
192
+ 'stream ' ,
193
+ 'version ' ,
194
+ 'type ' ,
195
+ 'payload ' ,
196
+ 'metadata ' ,
197
+ 'id ' ,
198
+ 'causationid ' ,
199
+ 'correlationid ' ,
200
+ 'recordedat ' ,
201
+ ];
202
+
203
+ if (count (array_diff ($ expected , $ actual )) > 0 ) {
204
+ return Status::setupRequired (
205
+ sprintf ('Table `%s` requires schema update. ' , $ this ->eventTableName )
206
+ );
207
+ }
208
+
188
209
return Status::ok ();
189
210
}
190
211
212
+
191
213
public function setup (): void
192
214
{
193
215
/** @var SchemaBuilder $schema */
194
216
$ schema = $ this ->connection ->getSchemaBuilder ();
195
- if (!$ schema ->hasTable ($ this ->eventTableName )) {
217
+
218
+ if (! $ schema ->hasTable ($ this ->eventTableName )) {
196
219
$ schema ->create ($ this ->eventTableName , function (Blueprint $ table ) {
197
- $ table ->unsignedBigInteger ('sequencenumber ' , true );
198
- $ table ->string ('stream ' , StreamName::MAX_LENGTH )->charset ('ascii ' );
220
+ // == PRIMARY KEY ==
221
+ // bigIncrements on MySQL => BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
222
+ // on SQLite Laravel will collapse it to INTEGER PRIMARY KEY AUTOINCREMENT
223
+ $ table ->bigIncrements ('sequencenumber ' );
224
+
225
+ // == COLUMNS ==
226
+ $ table ->string ('stream ' , StreamName::MAX_LENGTH )
227
+ ->charset ('ascii ' );
199
228
$ table ->unsignedBigInteger ('version ' );
200
- $ table ->string ('type ' , Event \EventType::MAX_LENGTH )->charset ('ascii ' );
229
+ $ table ->string ('type ' , Event \EventType::MAX_LENGTH )
230
+ ->charset ('ascii ' );
201
231
$ table ->text ('payload ' );
202
232
$ table ->json ('metadata ' )->nullable ();
203
- $ table ->char ('id ' , Event \EventId::MAX_LENGTH );
204
- $ table ->char ('causationid ' , Event \CausationId::MAX_LENGTH )->nullable ();
205
- $ table ->char ('correlationid ' , Event \CorrelationId::MAX_LENGTH )->nullable ();
206
- $ table ->timestamp ('recordedat ' );
233
+ $ table ->char ('id ' , Event \EventId::MAX_LENGTH )
234
+ ->charset ('ascii ' )
235
+ ->unique ();
236
+ $ table ->char ('causationid ' , Event \CausationId::MAX_LENGTH )
237
+ ->charset ('ascii ' )
238
+ ->nullable ();
239
+ $ table ->char ('correlationid ' , Event \CorrelationId::MAX_LENGTH )
240
+ ->charset ('ascii ' )
241
+ ->nullable ();
242
+ // Doctrine used DATETIME, so use dateTime() here (not timestamp)
243
+ $ table ->dateTime ('recordedat ' );
207
244
208
- $ table ->primary ('sequencenumber ' );
209
- $ table ->unique ('id ' );
245
+ // == INDEXES ==
210
246
$ table ->unique (['stream ' , 'version ' ]);
211
247
$ table ->index ('correlationid ' );
212
248
});
213
249
}
214
250
}
215
251
252
+
216
253
private function getStreamVersion (StreamName $ streamName ): MaybeVersion
217
254
{
218
255
$ version = $ this ->connection
0 commit comments