Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fixed an error that could occur after upgrading to Craft 6 when Laravel queue or other supporting database tables were missing. ([#19889](https://github.com/craftcms/cms/pull/19889))
- Fixed a bug where new control panel assets weren’t getting published when updating via the control panel or `craft:update` command.

## 6.0.0-alpha.20 - 2026-10-07
Expand Down
2 changes: 1 addition & 1 deletion src/Cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public const string VERSION = '6.0.0-alpha.20';

public const string SCHEMA_VERSION = '6.0.0.22';
public const string SCHEMA_VERSION = '6.0.0.23';

public const string MIN_VERSION_REQUIRED = '5.9.0';

Expand Down
84 changes: 84 additions & 0 deletions src/Database/LaravelMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,90 @@
#[Singleton]
class LaravelMigrations
{
public function ensureTables(): void
{
$this->ensurePasswordResetTokensTable();
$this->ensureCacheTables();
$this->ensureQueueTables();
$this->ensureSessionsTable();
$this->ensureNotificationsTable();
$this->ensurePassportTables();
}

public function ensurePasswordResetTokensTable(): void
{
if (Schema::hasTable(Table::PASSWORD_RESET_TOKENS)) {
return;
}

Schema::create(Table::PASSWORD_RESET_TOKENS, function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}

public function ensureCacheTables(): void
{
if (! Schema::hasTable(Table::CACHE)) {
Schema::create(Table::CACHE, function (Blueprint $table) {
$table->string('key')->primary();
$table->mediumText('value');
$table->bigInteger('expiration')->index();
});
}

if (! Schema::hasTable('cache_locks')) {
Schema::create('cache_locks', function (Blueprint $table) {
$table->string('key')->primary();
$table->string('owner');
$table->bigInteger('expiration')->index();
});
}
}

public function ensureQueueTables(): void
{
if (! Schema::hasTable('jobs')) {
Schema::create('jobs', function (Blueprint $table) {
$table->id();
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedSmallInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}

if (! Schema::hasTable('job_batches')) {
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
}

if (! Schema::hasTable('failed_jobs')) {
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
}

public function ensurePassportTables(): void
{
if (! Schema::hasTable(Table::OAUTH_AUTH_CODES)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use CraftCms\Cms\Database\LaravelMigrations;
use CraftCms\Cms\Database\Migration;

return new class extends Migration
{
public function up(): void
{
app(LaravelMigrations::class)->ensureTables();
}

public function down(): void {}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use CraftCms\Cms\Database\LaravelMigrations;
use CraftCms\Cms\Database\Migration;
use CraftCms\Cms\Database\Table;
use Illuminate\Database\Schema\Blueprint;
Expand All @@ -11,15 +12,7 @@
{
public function up(): void
{
if (Schema::hasTable(Table::PASSWORD_RESET_TOKENS)) {
return;
}

Schema::create(Table::PASSWORD_RESET_TOKENS, function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
app(LaravelMigrations::class)->ensurePasswordResetTokensTable();

$columns = array_filter([
'verificationCode',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
* - Drops the legacy `queue` table (used by Yii2 Queue)
* - Creates the `jobprogress` table (used by Laravel Queue for progress tracking)
*
* The Laravel Queue system uses its own tables (jobs, failed_jobs) which are
* managed by the host application, not by Craft.
* Laravel's queue tables are provisioned separately.
*/
return new class extends Migration
{
Expand Down
78 changes: 1 addition & 77 deletions src/Database/Migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,83 +133,7 @@ public function up(): void
*/
public function createLaravelTables(): void
{
if (! Schema::hasTable(Table::PASSWORD_RESET_TOKENS)) {
Schema::create(Table::PASSWORD_RESET_TOKENS, function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}

if (! Schema::hasTable(Table::CACHE)) {
Schema::create(Table::CACHE, function (Blueprint $table) {
$table->string('key')->primary();
$table->mediumText('value');
$table->bigInteger('expiration')->index();
});
}

if (! Schema::hasTable('cache_locks')) {
Schema::create('cache_locks', function (Blueprint $table) {
$table->string('key')->primary();
$table->string('owner');
$table->bigInteger('expiration')->index();
});
}

if (! Schema::hasTable('jobs')) {
Schema::create('jobs', function (Blueprint $table) {
$table->id();
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedSmallInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}

if (! Schema::hasTable('job_batches')) {
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
}

if (! Schema::hasTable('failed_jobs')) {
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}

if (! Schema::hasTable(Table::SESSIONS)) {
Schema::create(Table::SESSIONS, function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->longText('payload');
$table->integer('last_activity')->index();
});
}

$laravelMigrations = app(LaravelMigrations::class);
$laravelMigrations->ensureNotificationsTable();
$laravelMigrations->ensurePassportTables();
app(LaravelMigrations::class)->ensureTables();
}

/**
Expand Down
92 changes: 92 additions & 0 deletions tests/Feature/Database/Migrations/CreateLaravelTablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

use CraftCms\Cms\Database\Table;
use CraftCms\Cms\Update\Updates;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Schema;

test('upgrades provision a working database queue', function (bool $transitionPending) {
Schema::drop('jobs');
Schema::drop('job_batches');
Schema::drop('failed_jobs');

$pendingMigrations = ['0000_00_00_000000_create_laravel_tables'];

if ($transitionPending) {
$pendingMigrations[] = '0000_00_00_000007_migrate_to_laravel_queue';
}

DB::table(Table::MIGRATIONS)
->where('track', 'craft')
->whereIn('migration', $pendingMigrations)
->delete();

app(Updates::class)->runMigrations(['craft']);

$queue = Queue::connection('database');
expect($queue->size())->toBe(0);

$payload = json_encode(['job' => 'ExampleJob', 'data' => ['message' => 'Upgrade complete']], JSON_THROW_ON_ERROR);
$queue->pushRaw($payload);

DB::table(Table::MIGRATIONS)
->where('track', 'craft')
->where('migration', '0000_00_00_000000_create_laravel_tables')
->delete();

app(Updates::class)->runMigrations(['craft']);

expect($queue->size())->toBe(1)
->and($queue->pop()->getRawBody())->toBe($payload);
})->with([
'pending queue transition' => true,
'previously applied queue transition' => false,
]);

test('upgrades provision database cache and lock stores', function () {
Schema::drop(Table::CACHE);
Schema::drop('cache_locks');

DB::table(Table::MIGRATIONS)
->where('track', 'craft')
->where('migration', '0000_00_00_000000_create_laravel_tables')
->delete();

app(Updates::class)->runMigrations(['craft']);

$cache = Cache::store('database');
$cache->put('upgrade', 'complete', 60);

expect($cache->get('upgrade'))->toBe('complete');

$lock = $cache->lock('upgrade-lock', 60);
expect($lock->get())->toBeTrue();

$lock->release();
expect($cache->lock('upgrade-lock', 60)->get())->toBeTrue();
});

test('password reset upgrades remove legacy verification columns when the token table already exists', function () {
Schema::table(Table::USERS, function (Blueprint $table) {
$table->string('verificationCode')->nullable();
$table->dateTime('verificationCodeIssuedDate')->nullable();
});

DB::table(Table::MIGRATIONS)
->where('track', 'craft')
->whereIn('migration', [
'0000_00_00_000000_create_laravel_tables',
'0000_00_00_000006_create_password_reset_tokens_table',
])
->delete();

app(Updates::class)->runMigrations(['craft']);

expect(Schema::hasColumn(Table::USERS, 'verificationCode'))->toBeFalse()
->and(Schema::hasColumn(Table::USERS, 'verificationCodeIssuedDate'))->toBeFalse();
});