Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Add transaction ID to logs table, prevent double-publishing old migra…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Lotuashvili committed Oct 21, 2022
1 parent 930980b commit ee30e45
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table(config('payze.logs_table'), function (Blueprint $table) {
$table->foreignId('transaction_id')->nullable()->after('id')->constrained(config('payze.transactions_table'))->cascadeOnUpdate()->nullOnDelete();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(config('payze.logs_table'), function (Blueprint $table) {
$table->dropConstrainedForeignId('transaction_id');
});
}
}
3 changes: 3 additions & 0 deletions src/Models/PayzeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* PayzeIO\LaravelPayze\Models\PayzeLog
*
* @property int $id
* @property int|null $transaction_id
* @property string|null $message
* @property array|null $payload
* @property \Illuminate\Support\Carbon|null $created_at
Expand All @@ -19,6 +20,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|PayzeLog whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|PayzeLog whereMessage($value)
* @method static \Illuminate\Database\Eloquent\Builder|PayzeLog wherePayload($value)
* @method static \Illuminate\Database\Eloquent\Builder|PayzeLog whereTransactionId($value)
* @method static \Illuminate\Database\Eloquent\Builder|PayzeLog whereUpdatedAt($value)
* @mixin \Eloquent
*/
Expand All @@ -30,6 +32,7 @@ class PayzeLog extends Model
* @var array
*/
protected $fillable = [
'transaction_id',
'message',
'payload',
];
Expand Down
51 changes: 38 additions & 13 deletions src/PayzeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PayzeIO\LaravelPayze;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use PayzeIO\LaravelPayze\Models\PayzeTransaction;
use PayzeIO\LaravelPayze\Observers\PayzeTransactionObserver;
Expand All @@ -10,19 +12,22 @@ class PayzeServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__ . '/../database/migrations/create_payze_transactions_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time() - 10) . '_create_payze_transactions_table.php'),
__DIR__ . '/../database/migrations/create_payze_logs_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time() - 5) . '_create_payze_logs_table.php'),
__DIR__ . '/../database/migrations/create_payze_card_tokens_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_payze_card_tokens_table.php'),
], 'migrations');

$this->publishes([
__DIR__ . '/../config/payze.php' => config_path('payze.php'),
], 'config');

$this->publishes([
__DIR__ . '/controllers/PayzeController.php.stub' => app_path('Http/Controllers/PayzeController.php'),
], 'controllers');
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../database/migrations/create_payze_transactions_table.php.stub' => $this->getMigrationFileName('create_payze_transactions_table.php', '_01'),
__DIR__ . '/../database/migrations/create_payze_logs_table.php.stub' => $this->getMigrationFileName('create_payze_logs_table.php', '_02'),
__DIR__ . '/../database/migrations/create_payze_card_tokens_table.php.stub' => $this->getMigrationFileName('create_payze_card_tokens_table.php', '_03'),
__DIR__ . '/../database/migrations/add_transaction_id_to_payze_logs_table.php.stub' => $this->getMigrationFileName('add_transaction_id_to_payze_logs_table.php', '_04'),
], 'migrations');

$this->publishes([
__DIR__ . '/../config/payze.php' => config_path('payze.php'),
], 'config');

$this->publishes([
__DIR__ . '/controllers/PayzeController.php.stub' => app_path('Http/Controllers/PayzeController.php'),
], 'controllers');
}

$this->mergeConfigFrom(__DIR__ . '/../config/payze.php', 'payze');

Expand All @@ -31,6 +36,26 @@ public function boot()
PayzeTransaction::observe(PayzeTransactionObserver::class);
}

/**
* Returns existing migration file if found, else uses the current timestamp.
*
* @param $migrationFileName
* @param string $prefix
*
* @return string
*/
protected function getMigrationFileName($migrationFileName, string $prefix = ''): string
{
$timestamp = date('Y_m_d_His') . $prefix;

return Collection::make($this->app->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR)
->flatMap(function ($path) use ($migrationFileName) {
return File::glob($path . '*_' . $migrationFileName);
})
->push($this->app->databasePath() . "/migrations/{$timestamp}_$migrationFileName")
->first();
}

public function register()
{
$this->app->bind(Payze::class);
Expand Down

0 comments on commit ee30e45

Please sign in to comment.