Certified/database/migrations/2026_08_09_000001_create_subscriptions_table.php
2026-08-09 16:33:53 +02:00

43 lines
1.5 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
$table->string('plan')->default('free');
$table->string('status')->default('active');
$table->string('moneybird_contact_id')->nullable();
$table->string('moneybird_recurring_invoice_id')->nullable();
$table->date('current_period_starts_at')->nullable();
$table->date('current_period_ends_at')->nullable();
$table->date('grace_ends_at')->nullable();
$table->boolean('cancel_at_period_end')->default(false);
$table->timestamps();
});
// Backfill a Free subscription for any user that already existed
// before this table did, so every user always has a row.
DB::table('users')->pluck('id')->each(function (int $userId) {
DB::table('subscriptions')->insert([
'user_id' => $userId,
'plan' => 'free',
'status' => 'active',
'created_at' => now(),
'updated_at' => now(),
]);
});
}
public function down(): void
{
Schema::dropIfExists('subscriptions');
}
};