Skip to content

Commit 4fab4fa

Browse files
committed
Add configurable database connection support
Allows users to specify a custom database connection for chat tables via the `database_connection` config option. Defaults to null which uses Laravel's default connection, maintaining backward compatibility. Updates both models (via BaseModel) and migrations to respect the configured connection. Closes #267
1 parent f33ba06 commit 4fab4fa

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

config/musonza_chat.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
return [
4+
/*
5+
* Database connection to use for chat tables.
6+
* Set to null to use the default connection.
7+
*/
8+
'database_connection' => null,
9+
410
/*
511
* This will allow you to broadcast an event when a message is sent
612
* Example:

database/migrations/add_is_encrypted_to_messages_table.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77

88
class AddIsEncryptedToMessagesTable extends Migration
99
{
10+
protected function schema()
11+
{
12+
$connection = config('musonza_chat.database_connection');
13+
14+
return $connection ? Schema::connection($connection) : Schema::getFacadeRoot();
15+
}
16+
1017
/**
1118
* Run the migrations.
1219
*
1320
* @return void
1421
*/
1522
public function up()
1623
{
17-
Schema::table(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) {
24+
$this->schema()->table(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) {
1825
$table->boolean('is_encrypted')->default(false)->after('data');
1926
});
2027
}
@@ -26,7 +33,7 @@ public function up()
2633
*/
2734
public function down()
2835
{
29-
Schema::table(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) {
36+
$this->schema()->table(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) {
3037
$table->dropColumn('is_encrypted');
3138
});
3239
}

database/migrations/create_chat_tables.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,29 @@
77

88
class CreateChatTables extends Migration
99
{
10+
protected function schema()
11+
{
12+
$connection = config('musonza_chat.database_connection');
13+
14+
return $connection ? Schema::connection($connection) : Schema::getFacadeRoot();
15+
}
16+
1017
/**
1118
* Run the migrations.
1219
*
1320
* @return void
1421
*/
1522
public function up()
1623
{
17-
Schema::create(ConfigurationManager::CONVERSATIONS_TABLE, function (Blueprint $table) {
24+
$this->schema()->create(ConfigurationManager::CONVERSATIONS_TABLE, function (Blueprint $table) {
1825
$table->bigIncrements('id');
1926
$table->boolean('private')->default(true);
2027
$table->boolean('direct_message')->default(false);
2128
$table->text('data')->nullable();
2229
$table->timestamps();
2330
});
2431

25-
Schema::create(ConfigurationManager::PARTICIPATION_TABLE, function (Blueprint $table) {
32+
$this->schema()->create(ConfigurationManager::PARTICIPATION_TABLE, function (Blueprint $table) {
2633
$table->bigIncrements('id');
2734
$table->bigInteger('conversation_id')->unsigned();
2835
$table->bigInteger('messageable_id')->unsigned();
@@ -38,7 +45,7 @@ public function up()
3845
->onDelete('cascade');
3946
});
4047

41-
Schema::create(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) {
48+
$this->schema()->create(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) {
4249
$table->bigIncrements('id');
4350
$table->text('body');
4451
$table->bigInteger('conversation_id')->unsigned();
@@ -58,7 +65,7 @@ public function up()
5865
->onDelete('cascade');
5966
});
6067

61-
Schema::create(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE, function (Blueprint $table) {
68+
$this->schema()->create(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE, function (Blueprint $table) {
6269
$table->bigIncrements('id');
6370
$table->bigInteger('message_id')->unsigned();
6471
$table->bigInteger('messageable_id')->unsigned();
@@ -97,9 +104,9 @@ public function up()
97104
*/
98105
public function down()
99106
{
100-
Schema::dropIfExists(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE);
101-
Schema::dropIfExists(ConfigurationManager::MESSAGES_TABLE);
102-
Schema::dropIfExists(ConfigurationManager::PARTICIPATION_TABLE);
103-
Schema::dropIfExists(ConfigurationManager::CONVERSATIONS_TABLE);
107+
$this->schema()->dropIfExists(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE);
108+
$this->schema()->dropIfExists(ConfigurationManager::MESSAGES_TABLE);
109+
$this->schema()->dropIfExists(ConfigurationManager::PARTICIPATION_TABLE);
110+
$this->schema()->dropIfExists(ConfigurationManager::CONVERSATIONS_TABLE);
104111
}
105112
}

src/BaseModel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@
77
class BaseModel extends Model
88
{
99
protected $tablePrefix = 'chat_';
10+
11+
public function __construct(array $attributes = [])
12+
{
13+
parent::__construct($attributes);
14+
15+
if ($connection = config('musonza_chat.database_connection')) {
16+
$this->setConnection($connection);
17+
}
18+
}
1019
}

0 commit comments

Comments
 (0)