Skip to content

Commit c811a01

Browse files
authored
Add order to sites column (#466)
* Add order to sites column * Add test coverage * Ensure migration runs on existing installs
1 parent 30fca9f commit c811a01

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed

database/migrations/2024_07_16_100000_create_sites_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
$table->string('locale');
1717
$table->string('lang');
1818
$table->jsonb('attributes');
19+
$table->integer('order')->default(0)->index();
1920
$table->timestamps();
2021
});
2122
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Statamic\Eloquent\Database\BaseMigration as Migration;
6+
use Statamic\Facades\Entry;
7+
8+
return new class extends Migration {
9+
public function up()
10+
{
11+
Schema::table($this->prefix('sites'), function (Blueprint $table) {
12+
$table->integer('order')->default(0)->after('attributes')->index();
13+
});
14+
15+
$count = 0;
16+
app('statamic.eloquent.sites.model')::all()
17+
->each(function ($siteModel) use (&$count) {
18+
$siteModel->order = ++$count;
19+
$siteModel->save();
20+
});
21+
}
22+
23+
public function down()
24+
{
25+
Schema::table($this->prefix('sites'), function (Blueprint $table) {
26+
$table->dropColumn('order');
27+
});
28+
}
29+
};

src/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ServiceProvider extends AddonServiceProvider
5858
\Statamic\Eloquent\Updates\DropStatusOnEntries::class,
5959
\Statamic\Eloquent\Updates\ChangeFormSubmissionsIdType::class,
6060
\Statamic\Eloquent\Updates\AddIndexToDateOnEntriesTable::class,
61+
\Statamic\Eloquent\Updates\AddOrderToSitesTable::class,
6162
];
6263

6364
public function boot()

src/Sites/Sites.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function getSavedSites()
1515
return $this->getFallbackConfig();
1616
}
1717

18-
$sites = app('statamic.eloquent.sites.model')::all();
18+
$sites = app('statamic.eloquent.sites.model')::query()->orderBy('order')->get();
1919

2020
return $sites->isEmpty() ? $this->getFallbackConfig() : $sites->mapWithKeys(function ($model) {
2121
return [
@@ -32,6 +32,7 @@ protected function getSavedSites()
3232

3333
protected function saveToStore()
3434
{
35+
$count = 0;
3536
foreach ($this->config() as $handle => $config) {
3637
$lang = $config['lang'] ?? Str::before($config['locale'] ?? '', '_') ?? 'en';
3738

@@ -42,6 +43,7 @@ protected function saveToStore()
4243
'locale' => $config['locale'] ?? '',
4344
'url' => $config['url'] ?? '',
4445
'attributes' => $config['attributes'] ?? [],
46+
'order' => ++$count,
4547
])
4648
->save();
4749
}

src/Updates/AddOrderToSitesTable.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Statamic\Eloquent\Updates;
4+
5+
use Illuminate\Support\Facades\Schema;
6+
use Statamic\UpdateScripts\UpdateScript;
7+
8+
class AddOrderToSitesTable extends UpdateScript
9+
{
10+
public function shouldUpdate($newVersion, $oldVersion)
11+
{
12+
$sitesTable = config('statamic.eloquent-driver.table_prefix', '').'sites';
13+
14+
return Schema::hasTable($sitesTable) && ! Schema::hasColumn($sitesTable, 'order');
15+
}
16+
17+
public function update()
18+
{
19+
$source = __DIR__.'/../../database/migrations/updates/add_order_to_sites_table.php.stub';
20+
$dest = database_path('migrations/2025_07_03_add_order_to_sites_table.php');
21+
22+
$this->files->copy($source, $dest);
23+
24+
$this->console()->info('Migration created');
25+
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
26+
}
27+
}

tests/Sites/SitesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,27 @@ public function it_deletes_sites()
7373
$this->assertCount(3, SiteModel::all());
7474
$this->assertSame(['en', 'fr', 'es'], SiteModel::all()->pluck('handle')->all());
7575
}
76+
77+
#[Test]
78+
public function it_honours_order_when_saving_sites()
79+
{
80+
$this->assertCount(0, SiteModel::all());
81+
82+
$this->setSites([
83+
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
84+
'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'],
85+
'es' => ['name' => 'Spanish', 'locale' => 'es_ES', 'url' => 'http://test.com/es/'],
86+
'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'],
87+
]);
88+
89+
Site::save();
90+
91+
$this->assertSame([1, 2, 3, 4], SiteModel::all()->pluck('order')->all());
92+
93+
SiteModel::all()->reverse()->each(fn ($site) => $site->update(['order' => 5 - $site->order]));
94+
95+
Site::setSites();
96+
97+
$this->assertSame(['de', 'es', 'fr', 'en'], Site::all()->pluck('handle')->all());
98+
}
7699
}

0 commit comments

Comments
 (0)