Skip to content

Commit 454eebe

Browse files
committed
Create migration repository table automatically
1 parent 36a9b73 commit 454eebe

6 files changed

Lines changed: 43 additions & 48 deletions

File tree

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ Publish the migration configuration:
2222
php artisan vendor:publish --provider="DirectoryTree\OpenSearchMigrations\OpenSearchMigrationsServiceProvider"
2323
```
2424

25-
Run Laravel migrations to create the OpenSearch migrations table:
26-
27-
```bash
28-
php artisan migrate
29-
```
30-
3125
## Configuration
3226

3327
The migration configuration is published to `config/opensearch-migrations.php`:

database/migrations/2019_15_12_112000_create_opensearch_migrations_table.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/OpenSearchMigrationsServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ public function boot(): void
7878
__DIR__.'/../config/opensearch-migrations.php' => config_path('opensearch-migrations.php'),
7979
]);
8080

81-
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
82-
8381
$this->commands($this->commands);
8482
}
8583
}

src/Repositories/MigrationRepository.php

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

55
use DirectoryTree\OpenSearchMigrations\ReadinessInterface;
66
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Database\Schema\Blueprint;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\Facades\DB;
910
use Illuminate\Support\Facades\Schema;
@@ -71,6 +72,18 @@ public function truncate(): void
7172
$this->table()->truncate();
7273
}
7374

75+
/**
76+
* Create the migration repository table.
77+
*/
78+
public function create(): void
79+
{
80+
Schema::connection($this->connection)->create($this->table, function (Blueprint $table) {
81+
$table->increments('id');
82+
$table->string('migration');
83+
$table->integer('batch');
84+
});
85+
}
86+
7487
/**
7588
* Get the latest migration batch number.
7689
*/
@@ -123,6 +136,10 @@ protected function table(): Builder
123136
*/
124137
public function isReady(): bool
125138
{
139+
if (! Schema::connection($this->connection)->hasTable($this->table)) {
140+
$this->create();
141+
}
142+
126143
return Schema::connection($this->connection)->hasTable($this->table);
127144
}
128145
}

tests/Integration/MigratorTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use DirectoryTree\OpenSearchMigrations\Facades\Index;
44
use DirectoryTree\OpenSearchMigrations\Migrator;
5+
use DirectoryTree\OpenSearchMigrations\Repositories\MigrationRepository;
56
use Illuminate\Console\OutputStyle;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Support\Facades\DB;
@@ -13,6 +14,8 @@ function seededMigrator(OutputStyle $output): array
1314
{
1415
$table = config('opensearch-migrations.table');
1516

17+
app(MigrationRepository::class)->isReady();
18+
1619
DB::table($table)->insert([
1720
['migration' => '2018_12_01_081000_create_test_index', 'batch' => 1],
1821
]);
@@ -223,13 +226,14 @@ function outputExpectingLines(array $lines): OutputStyle
223226
expect($migrator->isReady())->toBeTrue();
224227
});
225228

226-
it('is not ready when the repository is not ready', function (): void {
227-
$output = outputExpectingLines(['<error>Migration table is not yet created</error>']);
229+
it('creates the repository table when checking readiness', function (): void {
230+
$output = Mockery::mock(OutputStyle::class);
228231
[$table, $migrator] = seededMigrator($output);
229232

230233
Schema::drop($table);
231234

232-
expect($migrator->isReady())->toBeFalse();
235+
expect($migrator->isReady())->toBeTrue();
236+
expect(Schema::hasTable($table))->toBeTrue();
233237
});
234238

235239
it('is not ready when the storage is not ready', function (): void {

tests/Integration/Repositories/MigrationRepositoryTest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
function seedMigrationRepositoryTable(): string
1111
{
1212
$table = config('opensearch-migrations.table');
13+
$repository = app(MigrationRepository::class);
14+
15+
$repository->isReady();
1316

1417
DB::table($table)->insert([
1518
['migration' => '2019_08_10_142230_update_test_index_mapping', 'batch' => 2],
@@ -19,6 +22,16 @@ function seedMigrationRepositoryTable(): string
1922
return $table;
2023
}
2124

25+
it('creates the repository table when it is missing', function (): void {
26+
$table = config('opensearch-migrations.table');
27+
28+
expect(Schema::hasTable($table))->toBeFalse();
29+
30+
expect(app(MigrationRepository::class)->isReady())->toBeTrue();
31+
32+
expect(Schema::hasTable($table))->toBeTrue();
33+
});
34+
2235
it('inserts records', function (): void {
2336
$table = seedMigrationRepositoryTable();
2437
$repository = app(MigrationRepository::class);
@@ -42,7 +55,7 @@ function seedMigrationRepositoryTable(): string
4255
expect(DB::table($table)->max('id'))->toBe(3);
4356
});
4457

45-
it('uses the configured database connection for the table migration', function (): void {
58+
it('uses the configured database connection for the repository table', function (): void {
4659
seedMigrationRepositoryTable();
4760

4861
config()->set('database.connections.opensearch_migrations', [
@@ -53,16 +66,12 @@ function seedMigrationRepositoryTable(): string
5366
config()->set('opensearch-migrations.connection', 'opensearch_migrations');
5467
config()->set('opensearch-migrations.table', 'custom_opensearch_migrations');
5568

56-
$migration = require dirname(__DIR__, 3).'/database/migrations/2019_15_12_112000_create_opensearch_migrations_table.php';
69+
$repository = app(MigrationRepository::class);
5770

58-
$migration->up();
71+
$repository->isReady();
5972

6073
expect(Schema::hasTable('custom_opensearch_migrations'))->toBeFalse();
6174
expect(Schema::connection('opensearch_migrations')->hasTable('custom_opensearch_migrations'))->toBeTrue();
62-
63-
$migration->down();
64-
65-
expect(Schema::connection('opensearch_migrations')->hasTable('custom_opensearch_migrations'))->toBeFalse();
6675
});
6776

6877
it('checks whether records exist', function (): void {
@@ -120,12 +129,13 @@ function seedMigrationRepositoryTable(): string
120129
expect(app(MigrationRepository::class)->isReady())->toBeTrue();
121130
});
122131

123-
it('is not ready when the table does not exist', function (): void {
132+
it('is ready after creating a missing table', function (): void {
124133
$table = seedMigrationRepositoryTable();
125134

126135
Schema::drop($table);
127136

128-
expect(app(MigrationRepository::class)->isReady())->toBeFalse();
137+
expect(app(MigrationRepository::class)->isReady())->toBeTrue();
138+
expect(Schema::hasTable($table))->toBeTrue();
129139
});
130140

131141
it('deletes all records', function (): void {

0 commit comments

Comments
 (0)