Skip to content

Commit aa7aa0e

Browse files
qschmickroshangautam
authored andcommitted
feature ( table-prefix) : add a config parameter for table prefix to avoid table name collisions
1 parent 0f76c80 commit aa7aa0e

12 files changed

+36
-21
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ php artisan migrate
5050
php artisan totem:assets
5151
```
5252

53+
##### Table Prefix
54+
55+
Totems' tables use generic names which may conflict with existing tables in a project. To alleviate this the `.env` param `TOTEM_TABLE_PREFIX` can be set which will apply a prefix to all of Totems tables and their models.
56+
5357
#### Updating
5458

5559
Please republish totem assets after updating totem to a new version

config/totem.php

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
'api' => [
223223
'middleware' => env('TOTEM_API_MIDDLEWARE', 'api'),
224224
],
225+
'table_prefix' => env('TOTEM_TABLE_PREFIX', ''),
225226
'artisan' => [
226227
'command_filter' => [],
227228
],

database/migrations/2017_08_05_194349_create_tasks_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateTasksTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('tasks', function (Blueprint $table) {
16+
Schema::create(config('totem.table_prefix').'tasks', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('description');
1919
$table->string('command');
@@ -35,6 +35,6 @@ public function up()
3535
*/
3636
public function down()
3737
{
38-
Schema::dropIfExists('tasks');
38+
Schema::dropIfExists(config('totem.table_prefix').'tasks');
3939
}
4040
}

database/migrations/2017_08_05_195539_create_task_frequencies_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateTaskFrequenciesTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('task_frequencies', function (Blueprint $table) {
16+
Schema::create(config('totem.table_prefix').'task_frequencies', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->unsignedInteger('task_id');
1919
$table->string('label');
@@ -29,6 +29,6 @@ public function up()
2929
*/
3030
public function down()
3131
{
32-
Schema::dropIfExists('task_frequencies');
32+
Schema::dropIfExists(config('totem.table_prefix').'task_frequencies');
3333
}
3434
}

database/migrations/2017_08_05_201914_create_task_results_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateTaskResultsTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('task_results', function (Blueprint $table) {
16+
Schema::create(config('totem.table_prefix').'task_results', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->unsignedInteger('task_id');
1919
$table->timestamp('ran_at')->useCurrent();
@@ -30,6 +30,6 @@ public function up()
3030
*/
3131
public function down()
3232
{
33-
Schema::dropIfExists('task_results');
33+
Schema::dropIfExists(config('totem.table_prefix').'task_results');
3434
}
3535
}

database/migrations/2017_08_24_085132_create_frequency_parameters_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateFrequencyParametersTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('frequency_parameters', function (Blueprint $table) {
16+
Schema::create(config('totem.table_prefix').'frequency_parameters', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->unsignedInteger('frequency_id');
1919
$table->string('name');
@@ -29,6 +29,6 @@ public function up()
2929
*/
3030
public function down()
3131
{
32-
Schema::dropIfExists('frequency_parameters');
32+
Schema::dropIfExists(config('totem.table_prefix').'frequency_parameters');
3333
}
3434
}

database/migrations/2017_08_26_083622_alter_tasks_table_add_notifications_fields.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AlterTasksTableAddNotificationsFields extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::table('tasks', function (Blueprint $table) {
16+
Schema::table(config('totem.table_prefix').'tasks', function (Blueprint $table) {
1717
$table->string('notification_phone_number')->nullable()->after('notification_email_address');
1818
$table->string('notification_slack_webhook')->nullable()->after('notification_phone_number');
1919
});
@@ -26,11 +26,11 @@ public function up()
2626
*/
2727
public function down()
2828
{
29-
Schema::table('tasks', function (Blueprint $table) {
29+
Schema::table(config('totem.table_prefix').'tasks', function (Blueprint $table) {
3030
$table->dropColumn('notification_phone_number');
3131
});
3232

33-
Schema::table('tasks', function (Blueprint $table) {
33+
Schema::table(config('totem.table_prefix').'tasks', function (Blueprint $table) {
3434
$table->dropColumn('notification_slack_webhook');
3535
});
3636
}

src/Frequency.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace Studio\Totem;
44

55
use Studio\Totem\Traits\HasParameters;
6-
use Illuminate\Database\Eloquent\Model;
76

8-
class Frequency extends Model
7+
class Frequency extends TotemModel
98
{
109
use HasParameters;
1110

src/Parameter.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Studio\Totem;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
7-
class Parameter extends Model
5+
class Parameter extends TotemModel
86
{
97
protected $table = 'frequency_parameters';
108

src/Result.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Studio\Totem;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
7-
class Result extends Model
5+
class Result extends TotemModel
86
{
97
protected $table = 'task_results';
108

src/Task.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace Studio\Totem;
44

55
use Cron\CronExpression;
6-
use Illuminate\Database\Eloquent\Model;
76
use Studio\Totem\Traits\HasFrequencies;
87
use Illuminate\Notifications\Notifiable;
98

10-
class Task extends Model
9+
class Task extends TotemModel
1110
{
1211
use Notifiable, HasFrequencies;
1312

src/TotemModel.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Studio\Totem;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class TotemModel extends Model
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function getTable()
13+
{
14+
return config('totem.table_prefix').parent::getTable();
15+
}
16+
}

0 commit comments

Comments
 (0)