From 67824d4ab5fbdf9b1035efdc7d7eaf3dca772bf9 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 7 Jul 2024 19:10:00 +1000 Subject: [PATCH] Remove binary attributes from DB, should be json columns --- app/Casts/CollectionOrNull.php | 29 +++++++++++++++++++ app/Models/Charge.php | 3 +- app/Models/Invoice.php | 6 ++-- app/Models/Service/Host.php | 5 ++++ app/Models/UserOauth.php | 3 +- database/migrations/0210-create_charges.php | 2 +- database/migrations/0227-create_host.php | 2 +- database/migrations/0260-create_invoice.php | 2 +- .../migrations/0271-create_user_oauth.php | 2 +- 9 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 app/Casts/CollectionOrNull.php diff --git a/app/Casts/CollectionOrNull.php b/app/Casts/CollectionOrNull.php new file mode 100644 index 0000000..dc1079d --- /dev/null +++ b/app/Casts/CollectionOrNull.php @@ -0,0 +1,29 @@ + $attributes + */ + public function get(Model $model, string $key, mixed $value, array $attributes): mixed + { + return collect(json_decode($value, true)); + } + + /** + * Prepare the given value for storage. + * + * @param array $attributes + */ + public function set(Model $model, string $key, mixed $value, array $attributes): mixed + { + return $value->count() ? json_encode($value) : NULL; + } +} \ No newline at end of file diff --git a/app/Models/Charge.php b/app/Models/Charge.php index e210c67..e3849be 100644 --- a/app/Models/Charge.php +++ b/app/Models/Charge.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; +use App\Casts\CollectionOrNull; use App\Traits\SiteID; /** @@ -19,7 +20,7 @@ class Charge extends Model use SiteID; protected $casts = [ - 'attributes' => 'json', + 'attributes' => CollectionOrNull::class, ]; protected $dates = [ diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 1957351..560b522 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -7,8 +7,10 @@ use Clarkeash\Doorman\Facades\Doorman; use Clarkeash\Doorman\Models\Invite; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; +use Leenooks\Casts\LeenooksCarbon; use Leenooks\Traits\ScopeActive; +use App\Casts\CollectionOrNull; use App\Interfaces\IDs; use App\Traits\PushNew; @@ -37,8 +39,8 @@ class Invoice extends Model implements IDs protected $casts = [ 'created_at' => 'datetime:Y-m-d', - 'due_at' => 'datetime:Y-m-d', - 'reminders' => 'json', + 'due_at' => LeenooksCarbon::class, + 'reminders' => CollectionOrNull::class, '_paid_at' => 'datetime:Y-m-d', ]; diff --git a/app/Models/Service/Host.php b/app/Models/Service/Host.php index acf1efb..9cce01d 100644 --- a/app/Models/Service/Host.php +++ b/app/Models/Service/Host.php @@ -2,6 +2,7 @@ namespace App\Models\Service; +use App\Casts\CollectionOrNull; use App\Models\SupplierHostServer; use App\Traits\ServiceDomains; @@ -13,6 +14,10 @@ class Host extends Type { use ServiceDomains; + protected $casts = [ + 'server_data' => CollectionOrNull::class, + ]; + protected $table = 'service_host'; protected $with = [ 'tld', diff --git a/app/Models/UserOauth.php b/app/Models/UserOauth.php index 423ef9c..50e90be 100644 --- a/app/Models/UserOauth.php +++ b/app/Models/UserOauth.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use App\Casts\CollectionOrNull; use App\Traits\SiteID; class UserOauth extends Model @@ -13,7 +14,7 @@ class UserOauth extends Model protected $table = 'user_oauth'; protected $casts = [ - 'oauth_data'=>'json', + 'oauth_data' => CollectionOrNull::class, ]; public function User() diff --git a/database/migrations/0210-create_charges.php b/database/migrations/0210-create_charges.php index 6ec1964..4d9a146 100644 --- a/database/migrations/0210-create_charges.php +++ b/database/migrations/0210-create_charges.php @@ -26,7 +26,7 @@ return new class extends Migration $table->float('amount', 10, 0)->nullable(); $table->float('quantity', 10, 0)->nullable(); $table->boolean('taxable')->default(true); - $table->binary('attributes', 65535)->nullable(); + $table->jsonb('attributes')->nullable(); $table->string('description', 128)->nullable(); $table->date('start_at')->nullable(); diff --git a/database/migrations/0227-create_host.php b/database/migrations/0227-create_host.php index de4b330..ad7f295 100644 --- a/database/migrations/0227-create_host.php +++ b/database/migrations/0227-create_host.php @@ -60,7 +60,7 @@ return new class extends Migration $table->string('host_password',45)->nullable(); $table->string('ftp_username',16)->nullable(); $table->string('ftp_password',16)->nullable(); - $table->binary('server_data',65535)->nullable(); + $table->jsonb('server_data')->nullable(); $table->date('expire_at')->nullable(); $table->integer('service_id')->unsigned(); diff --git a/database/migrations/0260-create_invoice.php b/database/migrations/0260-create_invoice.php index 77416ad..1a052b1 100644 --- a/database/migrations/0260-create_invoice.php +++ b/database/migrations/0260-create_invoice.php @@ -27,7 +27,7 @@ return new class extends Migration $table->boolean('print_status')->default(false); $table->integer('account_billing_id')->nullable(); $table->float('discount_amt',10,0)->nullable(); - $table->binary('reminders', 65535)->nullable(); + $table->jsonb('reminders')->nullable(); $table->integer('account_id')->unsigned(); $table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts'); diff --git a/database/migrations/0271-create_user_oauth.php b/database/migrations/0271-create_user_oauth.php index 5c14dbd..5899bcd 100644 --- a/database/migrations/0271-create_user_oauth.php +++ b/database/migrations/0271-create_user_oauth.php @@ -20,7 +20,7 @@ return new class extends Migration $table->integer('site_id')->unsigned(); $table->string('userid',128); - $table->binary('oauth_data', 65535)->nullable(); + $table->jsonb('oauth_data')->nullable(); $table->integer('user_id')->unsigned()->nullable();