Skip to content

Commit

Permalink
Merge pull request #4 from one2tek/2.x
Browse files Browse the repository at this point in the history
Added users table to logs db (with migrations)
  • Loading branch information
AgonAziri authored Feb 10, 2021
2 parents d24120c + 66f4746 commit 42b8d5e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 11 deletions.
22 changes: 13 additions & 9 deletions migrations/create_logs_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ class CreateLogsTable extends Migration
*/
public function up()
{
Schema::connection(config('laralog.database_connection'))->create(config('laralog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('event_type')->nullable();
$table->nullableMorphs('subject', 'subject');
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->timestamps();
});
if(!Schema::connection(config('laralog.database_connection'))->hasTable(config('laralog.table_name'))){
Schema::connection(config('laralog.database_connection'))->create(config('laralog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('event_type')->nullable();
$table->nullableMorphs('subject', 'subject');
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->timestamps();
});
}
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::connection(config('laralog.database_connection'))->dropIfExists(config('laralog.table_name'));
if(Schema::connection(config('laralog.database_connection'))->hasTable(config('laralog.table_name'))){
Schema::connection(config('laralog.database_connection'))->dropIfExists(config('laralog.table_name'));
}
}
}
34 changes: 34 additions & 0 deletions migrations/create_users_table_on_logs_db.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

class CreateUsersTableOnLogsDb extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
if(!Schema::connection(config('laralog.database_connection'))->hasTable('users')){
Schema::connection(config('laralog.database_connection'))->create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',191);
$table->string('role',20);
$table->timestamps();
});
}
}


/**
* Reverse the migrations.
*/
public function down()
{
if(Schema::connection(config('laralog.database_connection'))->hasTable('users')){
Schema::connection(config('laralog.database_connection'))->dropIfExists('users');
}
}
}
33 changes: 33 additions & 0 deletions migrations/drop_morph_from_logs_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class DropMorphFromLogsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::connection(config('laralog.database_connection'))->table(config('laralog.table_name'), function (Blueprint $table) {
if(Schema::hasColumn(config('laralog.table_name'),'causer_type')){
$table->dropColumn('causer_type');
$table->dropColumn('causer_id');
}
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::connection(config('laralog.database_connection'))->table(config('laralog.table_name'), function (Blueprint $table) {
if(!Schema::hasColumn(config('laralog.table_name'),'causer_type')){
$table->nullableMorphs('causer', 'causer')->after('subject_id');
}
});
}
}
34 changes: 34 additions & 0 deletions migrations/logs_table_causer_id_add.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

class LogsTableCauserIdAdd extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::connection(config('laralog.database_connection'))->table(config('laralog.table_name'), function (Blueprint $table) {
if(!Schema::hasColumn(config('laralog.table_name'),'causer_id')){
$table->bigInteger('causer_id')->unsigned()->nullable()->after('subject_id');
$table->foreign('causer_id')->references('id')->on('users');
}
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::connection(config('laralog.database_connection'))->table(config('laralog.table_name'), function (Blueprint $table) {
if(Schema::hasColumn(config('laralog.table_name'),'causer_id')){
$table->dropForeign(['causer_id']);
$table->dropColumn('causer_id');
}
});
}
}
28 changes: 26 additions & 2 deletions src/Providers/LaraLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,37 @@ public function boot()
$this->publishes([
__DIR__. '/../../config/laralog.php' => config_path('laralog.php'),
]);

if (! class_exists('CreateLogsTable')) {
if (!class_exists('CreateLogsTable')) {
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__.'/../../migrations/create_logs_table.php.stub' => database_path("/migrations/{$timestamp}_create_logs_table.php"),
], 'migrations');
}

if (!class_exists('CreateUsersTableOnLogsDb')) {
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__.'/../../migrations/create_users_table_on_logs_db.php.stub' => database_path("/migrations/{$timestamp}_create_users_table_on_logs_db.php")
], 'migrations');
}

if(!class_exists('DropMorphFromLogsTable')){
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__.'/../../migrations/drop_morph_from_logs_table.php.stub' => database_path("/migrations/{$timestamp}_drop_morph_from_logs_table.php")
], 'migrations');
}

if(!class_exists('LogsTableCauserIdAdd')){
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__.'/../../migrations/logs_table_causer_id_add.php.stub' => database_path("/migrations/{$timestamp}_logs_table_causer_id_add.php")
], 'migrations');
}
}
}

0 comments on commit 42b8d5e

Please sign in to comment.