Skip to content

Commit bb58591

Browse files
committed
Refactor migration prefix and service bindings
1 parent 7fc2f26 commit bb58591

10 files changed

Lines changed: 88 additions & 58 deletions

File tree

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
"autoload": {
1919
"psr-4": {
2020
"DirectoryTree\\OpenSearchMigrations\\": "src"
21-
},
22-
"files": [
23-
"src/helpers.php"
24-
]
21+
}
2522
},
2623
"autoload-dev": {
2724
"psr-4": {

src/Adapters/IndexManagerAdapter.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
99
use DirectoryTree\OpenSearchAdapter\Indices\Settings;
1010
use DirectoryTree\OpenSearchMigrations\IndexManagerInterface;
11-
12-
use function DirectoryTree\OpenSearchMigrations\prefix_alias_name;
13-
use function DirectoryTree\OpenSearchMigrations\prefix_index_name;
11+
use DirectoryTree\OpenSearchMigrations\Support\Prefix;
1412

1513
/**
1614
* Adapts the OpenSearch index manager for migration-friendly operations.
@@ -29,7 +27,7 @@ public function __construct(
2927
*/
3028
public function create(string $index, ?callable $modifier = null): IndexManagerInterface
3129
{
32-
$prefixedIndex = prefix_index_name($index);
30+
$prefixedIndex = Prefix::index($index);
3331

3432
if (isset($modifier)) {
3533
$mapping = new Mapping;
@@ -52,7 +50,7 @@ public function create(string $index, ?callable $modifier = null): IndexManagerI
5250
*/
5351
public function createIfNotExists(string $index, ?callable $modifier = null): IndexManagerInterface
5452
{
55-
$prefixedIndex = prefix_index_name($index);
53+
$prefixedIndex = Prefix::index($index);
5654

5755
if (! $this->indexManager->exists($prefixedIndex)) {
5856
$this->create($index, $modifier);
@@ -66,7 +64,7 @@ public function createIfNotExists(string $index, ?callable $modifier = null): In
6664
*/
6765
public function putMapping(string $index, callable $modifier): IndexManagerInterface
6866
{
69-
$prefixedIndex = prefix_index_name($index);
67+
$prefixedIndex = Prefix::index($index);
7068

7169
$mapping = new Mapping;
7270
$modifier($mapping);
@@ -81,7 +79,7 @@ public function putMapping(string $index, callable $modifier): IndexManagerInter
8179
*/
8280
public function putSettings(string $index, callable $modifier): IndexManagerInterface
8381
{
84-
$prefixedIndex = prefix_index_name($index);
82+
$prefixedIndex = Prefix::index($index);
8583

8684
$settings = new Settings;
8785
$modifier($settings);
@@ -96,7 +94,7 @@ public function putSettings(string $index, callable $modifier): IndexManagerInte
9694
*/
9795
public function pushSettings(string $index, callable $modifier): IndexManagerInterface
9896
{
99-
$prefixedIndex = prefix_index_name($index);
97+
$prefixedIndex = Prefix::index($index);
10098

10199
$this->indexManager->close($prefixedIndex);
102100
$this->putSettings($index, $modifier);
@@ -110,7 +108,7 @@ public function pushSettings(string $index, callable $modifier): IndexManagerInt
110108
*/
111109
public function drop(string $index): IndexManagerInterface
112110
{
113-
$prefixedIndex = prefix_index_name($index);
111+
$prefixedIndex = Prefix::index($index);
114112

115113
$this->indexManager->delete($prefixedIndex);
116114

@@ -122,7 +120,7 @@ public function drop(string $index): IndexManagerInterface
122120
*/
123121
public function dropIfExists(string $index): IndexManagerInterface
124122
{
125-
$prefixedIndex = prefix_index_name($index);
123+
$prefixedIndex = Prefix::index($index);
126124

127125
if ($this->indexManager->exists($prefixedIndex)) {
128126
$this->drop($index);
@@ -138,8 +136,8 @@ public function dropIfExists(string $index): IndexManagerInterface
138136
*/
139137
public function putAlias(string $index, string $aliasName, ?array $filter = null): IndexManagerInterface
140138
{
141-
$prefixedIndex = prefix_index_name($index);
142-
$prefixedAliasName = prefix_alias_name($aliasName);
139+
$prefixedIndex = Prefix::index($index);
140+
$prefixedAliasName = Prefix::alias($aliasName);
143141

144142
$this->indexManager->putAlias($prefixedIndex, new Alias($prefixedAliasName, $filter));
145143

@@ -151,8 +149,8 @@ public function putAlias(string $index, string $aliasName, ?array $filter = null
151149
*/
152150
public function deleteAlias(string $index, string $aliasName): IndexManagerInterface
153151
{
154-
$prefixedIndex = prefix_index_name($index);
155-
$prefixedAliasName = prefix_alias_name($aliasName);
152+
$prefixedIndex = Prefix::index($index);
153+
$prefixedAliasName = Prefix::alias($aliasName);
156154

157155
$this->indexManager->deleteAlias($prefixedIndex, $prefixedAliasName);
158156

src/Filesystem/MigrationStorage.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ class MigrationStorage implements ReadinessInterface
2020
* Create a new migration storage instance.
2121
*/
2222
public function __construct(
23-
protected Filesystem $filesystem
23+
protected Filesystem $filesystem,
24+
?string $directory = null
2425
) {
25-
$this->directory = rtrim(config('opensearch-migrations.storage_directory', ''), '/');
26+
$this->directory = rtrim($directory ?? '', '/');
2627
}
2728

2829
/**

src/OpenSearchMigrationsServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use DirectoryTree\OpenSearchMigrations\Console\ResetCommand;
1313
use DirectoryTree\OpenSearchMigrations\Console\RollbackCommand;
1414
use DirectoryTree\OpenSearchMigrations\Console\StatusCommand;
15+
use DirectoryTree\OpenSearchMigrations\Filesystem\MigrationStorage;
16+
use DirectoryTree\OpenSearchMigrations\Repositories\MigrationRepository;
17+
use Illuminate\Filesystem\Filesystem;
1518
use Illuminate\Support\ServiceProvider;
1619

1720
/**
@@ -43,6 +46,20 @@ public function register(): void
4346

4447
$this->app->bindIf(IndexManagerInterface::class, IndexManagerAdapter::class);
4548

49+
$this->app->bindIf(MigrationRepository::class, function () {
50+
return new MigrationRepository(
51+
config('opensearch-migrations.table'),
52+
config('opensearch-migrations.connection')
53+
);
54+
});
55+
56+
$this->app->bindIf(MigrationStorage::class, function ($app) {
57+
return new MigrationStorage(
58+
$app->make(Filesystem::class),
59+
config('opensearch-migrations.storage_directory')
60+
);
61+
});
62+
4663
$this->app->singletonIf(IndexManager::class, function ($app) {
4764
return new IndexManager($app->make(OpenSearchManager::class)->default());
4865
});

src/Repositories/MigrationRepository.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,13 @@
1414
*/
1515
class MigrationRepository implements ReadinessInterface
1616
{
17-
/**
18-
* @var string
19-
*/
20-
protected $table;
21-
22-
/**
23-
* @var string
24-
*/
25-
protected $connection;
26-
2717
/**
2818
* Create a new migration repository instance.
2919
*/
30-
public function __construct()
31-
{
32-
$this->table = config('opensearch-migrations.table');
33-
$this->connection = config('opensearch-migrations.connection');
34-
}
20+
public function __construct(
21+
protected string $table,
22+
protected ?string $connection = null
23+
) {}
3524

3625
/**
3726
* Insert an executed migration record.

src/Support/Prefix.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace DirectoryTree\OpenSearchMigrations\Support;
4+
5+
/**
6+
* Applies configured OpenSearch migration prefixes.
7+
*/
8+
class Prefix
9+
{
10+
/**
11+
* Prefix an OpenSearch index name.
12+
*/
13+
public static function index(string $index): string
14+
{
15+
return config('opensearch-migrations.index_name_prefix').$index;
16+
}
17+
18+
/**
19+
* Prefix an OpenSearch alias name.
20+
*/
21+
public static function alias(string $alias): string
22+
{
23+
return config('opensearch-migrations.alias_name_prefix').$alias;
24+
}
25+
}

src/helpers.php

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

tests/Integration/Repositories/MigrationRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function setUp(): void
3434
['migration' => '2018_12_01_081000_create_test_index', 'batch' => 1],
3535
]);
3636

37-
$this->migrationRepository = new MigrationRepository;
37+
$this->migrationRepository = $this->app->make(MigrationRepository::class);
3838
}
3939

4040
public function test_record_can_be_inserted(): void
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace DirectoryTree\OpenSearchMigrations\Tests\Integration\Support;
4+
5+
use DirectoryTree\OpenSearchMigrations\Support\Prefix;
6+
use DirectoryTree\OpenSearchMigrations\Tests\Integration\TestCase;
7+
8+
class PrefixTest extends TestCase
9+
{
10+
public function test_index_names_can_be_prefixed(): void
11+
{
12+
$this->app['config']->set('opensearch-migrations.index_name_prefix', 'tenant_');
13+
14+
$this->assertSame('tenant_posts', Prefix::index('posts'));
15+
}
16+
17+
public function test_alias_names_can_be_prefixed(): void
18+
{
19+
$this->app['config']->set('opensearch-migrations.alias_name_prefix', 'tenant_');
20+
21+
$this->assertSame('tenant_posts_read', Prefix::alias('posts_read'));
22+
}
23+
}

tests/migrations/2018_12_01_081000_create_test_index.php

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

33
use DirectoryTree\OpenSearchMigrations\Facades\Index;
44
use DirectoryTree\OpenSearchMigrations\MigrationInterface;
5+
use DirectoryTree\OpenSearchMigrations\Support\Prefix;
56
use OpenSearch\Client;
67

7-
use function DirectoryTree\OpenSearchMigrations\prefix_index_name;
8-
98
class CreateTestIndex implements MigrationInterface
109
{
1110
/**
@@ -20,7 +19,7 @@ public function up(): void
2019
Index::create('test');
2120

2221
$this->client->indices()->clearCache([
23-
'index' => prefix_index_name('test'),
22+
'index' => Prefix::index('test'),
2423
]);
2524
}
2625

0 commit comments

Comments
 (0)