feat: clean credits users
This commit is contained in:
+4
-33
@@ -8,16 +8,11 @@ return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('ai_credits_balance')->default(3)->after('trial_ends_at');
|
||||
});
|
||||
|
||||
Schema::create('credit_products', function (Blueprint $table): void {
|
||||
Schema::create('billing_products', function (Blueprint $table): void {
|
||||
$table->ulid('id')->primary();
|
||||
$table->string('name');
|
||||
$table->text('description')->nullable();
|
||||
$table->string('type', 32);
|
||||
$table->unsignedInteger('credits');
|
||||
$table->string('purpose', 32)->unique();
|
||||
$table->unsignedInteger('amount');
|
||||
$table->string('currency', 3)->default('eur');
|
||||
$table->string('stripe_product_id')->nullable()->index();
|
||||
@@ -26,36 +21,12 @@ return new class extends Migration
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['type', 'is_active', 'sort_order']);
|
||||
});
|
||||
|
||||
Schema::create('credit_ledger_entries', function (Blueprint $table): void {
|
||||
$table->ulid('id')->primary();
|
||||
$table->foreignUlid('user_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->foreignUlid('credit_product_id')->nullable()->constrained('credit_products')->nullOnDelete();
|
||||
$table->string('type', 64);
|
||||
$table->integer('credits');
|
||||
$table->unsignedInteger('balance_after');
|
||||
$table->string('source')->nullable()->unique();
|
||||
$table->string('stripe_checkout_session_id')->nullable()->index();
|
||||
$table->string('stripe_invoice_id')->nullable()->index();
|
||||
$table->string('stripe_payment_intent_id')->nullable()->index();
|
||||
$table->string('stripe_subscription_id')->nullable()->index();
|
||||
$table->json('metadata')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['user_id', 'created_at']);
|
||||
$table->index(['user_id', 'type', 'created_at']);
|
||||
$table->index(['purpose', 'is_active', 'sort_order']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('credit_ledger_entries');
|
||||
Schema::dropIfExists('credit_products');
|
||||
|
||||
Schema::table('users', function (Blueprint $table): void {
|
||||
$table->dropColumn('ai_credits_balance');
|
||||
});
|
||||
Schema::dropIfExists('billing_products');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,8 +11,7 @@ return new class extends Migration
|
||||
Schema::create('payment_transactions', function (Blueprint $table): void {
|
||||
$table->ulid('id')->primary();
|
||||
$table->foreignUlid('user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->foreignUlid('credit_product_id')->nullable()->constrained('credit_products')->nullOnDelete();
|
||||
$table->foreignUlid('credit_ledger_entry_id')->nullable()->constrained('credit_ledger_entries')->nullOnDelete();
|
||||
$table->foreignUlid('billing_product_id')->nullable()->constrained('billing_products')->nullOnDelete();
|
||||
$table->string('type', 64);
|
||||
$table->string('status', 64);
|
||||
$table->string('stripe_event_id')->nullable()->index();
|
||||
@@ -25,8 +24,6 @@ return new class extends Migration
|
||||
$table->string('stripe_price_id')->nullable()->index();
|
||||
$table->unsignedInteger('amount')->nullable();
|
||||
$table->string('currency', 3)->nullable();
|
||||
$table->unsignedInteger('credits_expected')->nullable();
|
||||
$table->unsignedInteger('credits_granted')->default(0);
|
||||
$table->text('error_message')->nullable();
|
||||
$table->json('payload')->nullable();
|
||||
$table->timestamp('processed_at')->nullable();
|
||||
|
||||
@@ -11,9 +11,11 @@ return new class extends Migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('credit_products', function (Blueprint $table): void {
|
||||
$table->string('purpose', 32)->nullable()->after('type')->unique();
|
||||
});
|
||||
if (Schema::hasTable('credit_products') && ! Schema::hasColumn('credit_products', 'purpose')) {
|
||||
Schema::table('credit_products', function (Blueprint $table): void {
|
||||
$table->string('purpose', 32)->nullable()->after('type')->unique();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,8 +23,10 @@ return new class extends Migration
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('credit_products', function (Blueprint $table): void {
|
||||
$table->dropColumn('purpose');
|
||||
});
|
||||
if (Schema::hasTable('credit_products') && Schema::hasColumn('credit_products', 'purpose')) {
|
||||
Schema::table('credit_products', function (Blueprint $table): void {
|
||||
$table->dropColumn('purpose');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
<?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
|
||||
{
|
||||
if (Schema::hasTable('credit_products')) {
|
||||
if (Schema::hasColumn('payment_transactions', 'credit_product_id')) {
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->dropForeign(['credit_product_id']);
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('payment_transactions', 'credit_ledger_entry_id')) {
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->dropForeign(['credit_ledger_entry_id']);
|
||||
});
|
||||
}
|
||||
|
||||
Schema::dropIfExists('credit_ledger_entries');
|
||||
Schema::rename('credit_products', 'billing_products');
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('billing_products', 'type')) {
|
||||
Schema::table('billing_products', function (Blueprint $table): void {
|
||||
$table->dropColumn('type');
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('billing_products', 'credits')) {
|
||||
Schema::table('billing_products', function (Blueprint $table): void {
|
||||
$table->dropColumn('credits');
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('payment_transactions', 'credit_product_id')) {
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->renameColumn('credit_product_id', 'billing_product_id');
|
||||
});
|
||||
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->foreign('billing_product_id')
|
||||
->references('id')
|
||||
->on('billing_products')
|
||||
->nullOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
$paymentColumns = array_values(array_filter([
|
||||
Schema::hasColumn('payment_transactions', 'credit_ledger_entry_id') ? 'credit_ledger_entry_id' : null,
|
||||
Schema::hasColumn('payment_transactions', 'credits_expected') ? 'credits_expected' : null,
|
||||
Schema::hasColumn('payment_transactions', 'credits_granted') ? 'credits_granted' : null,
|
||||
]));
|
||||
|
||||
if ($paymentColumns !== []) {
|
||||
Schema::table('payment_transactions', function (Blueprint $table) use ($paymentColumns): void {
|
||||
$table->dropColumn($paymentColumns);
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('users', 'ai_credits_balance')) {
|
||||
Schema::table('users', function (Blueprint $table): void {
|
||||
$table->dropColumn('ai_credits_balance');
|
||||
});
|
||||
}
|
||||
|
||||
DB::table('payment_transactions')
|
||||
->where('status', 'credited')
|
||||
->update(['status' => 'paid']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->dropForeign(['billing_product_id']);
|
||||
});
|
||||
|
||||
Schema::rename('billing_products', 'credit_products');
|
||||
|
||||
Schema::table('credit_products', function (Blueprint $table): void {
|
||||
$table->string('type', 32)->default('one_time');
|
||||
$table->unsignedInteger('credits')->default(0);
|
||||
});
|
||||
|
||||
DB::table('credit_products')
|
||||
->where('purpose', 'subscription')
|
||||
->update(['type' => 'monthly']);
|
||||
|
||||
Schema::table('users', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('ai_credits_balance')->default(0);
|
||||
});
|
||||
|
||||
Schema::create('credit_ledger_entries', function (Blueprint $table): void {
|
||||
$table->ulid('id')->primary();
|
||||
$table->foreignUlid('user_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->foreignUlid('credit_product_id')->nullable()->constrained('credit_products')->nullOnDelete();
|
||||
$table->string('type', 64);
|
||||
$table->integer('credits');
|
||||
$table->unsignedInteger('balance_after');
|
||||
$table->string('source')->nullable()->unique();
|
||||
$table->string('stripe_checkout_session_id')->nullable()->index();
|
||||
$table->string('stripe_invoice_id')->nullable()->index();
|
||||
$table->string('stripe_payment_intent_id')->nullable()->index();
|
||||
$table->string('stripe_subscription_id')->nullable()->index();
|
||||
$table->json('metadata')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->renameColumn('billing_product_id', 'credit_product_id');
|
||||
});
|
||||
|
||||
Schema::table('payment_transactions', function (Blueprint $table): void {
|
||||
$table->foreign('credit_product_id')->references('id')->on('credit_products')->nullOnDelete();
|
||||
$table->foreignUlid('credit_ledger_entry_id')->nullable()->constrained('credit_ledger_entries')->nullOnDelete();
|
||||
$table->unsignedInteger('credits_expected')->nullable();
|
||||
$table->unsignedInteger('credits_granted')->default(0);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user