Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make multiple db connections work #18

Closed
wants to merge 7 commits into from
Closed

Make multiple db connections work #18

wants to merge 7 commits into from

Conversation

spresnac
Copy link

@spresnac spresnac commented Aug 5, 2019

Description

When having multiple db connection, one can choose which one to use

Motivation and context

Having multiple db connections and needed to have fulltext-index NOT in the default one, this lead me to this ;)

How has this been tested?

Build into our system and let it run on a "not common" connection -> work
Put it into a default laravel and let it run unchanged -> works a before

Screenshots (if appropriate)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our continuous integration server to make sure your tests and code style pass.

  • I have read the CONTRIBUTING document.
  • My pull request addresses exactly one patch/feature.
  • I have created a branch for this patch/feature.
  • Each individual commit in the pull request is meaningful.
  • I have added tests to cover my changes.
  • If my change requires a change to the documentation, I have updated it accordingly.

If you're unsure about any of these, don't hesitate to ask. We're here to help!

@vblinden
Copy link
Contributor

vblinden commented Aug 6, 2019

Hello Sascha,

Thanks for the pull request!

Can you make it that we have a fallback (default) database connection, so that setting a database isn't required?

@spresnac
Copy link
Author

spresnac commented Aug 6, 2019

Hej @vblinden : Sry, i missed that, here it is ;)

README.md Outdated Show resolved Hide resolved
config/laravel-fulltext.php Outdated Show resolved Hide resolved
src/IndexedRecord.php Outdated Show resolved Hide resolved
- making the readme more clear
@vblinden vblinden self-requested a review August 6, 2019 08:34
@vblinden vblinden requested a review from bbrala August 6, 2019 08:41
@bbrala
Copy link
Member

bbrala commented Aug 6, 2019

Wouldn't setting the connection in the constructor break when using static methods? From what I remember you need to set the protected $connection property to set it then.

IndexedRecord::where('indexable_id', $id)->where('indexable_type', $class);

I don't have a project here right now to test if this is an issue or not. But if it is, perhaps we need another way to set the connection.

@spresnac
Copy link
Author

spresnac commented Aug 6, 2019

Wouldn't setting the connection in the constructor break when using static methods? From what I remember you need to set the protected $connection property to set it then.

IndexedRecord::where('indexable_id', $id)->where('indexable_type', $class);

I don't have a project here right now to test if this is an issue or not. But if it is, perhaps we need another way to set the connection.

When setting the protected $connection, you cannot set it to a variable, only to a fixed string, so this way is not working. Next solution was to set it into the constructor.

Copy link
Member

@JaZo JaZo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the connection in the constructor looks fine to me as it's not a static property. I'd slightly change the config to make it more bulletproof. Please see my other comments for that.

* The database connection to be used
* This will use the db connection from config/database.php
*/
'db_connection' => env('DB_CONNECTION'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a potential breaking change and/or it can lead to strange behaviour. Someone might use a different environment variable or has a fixed value for config('database.default'). If we default this to null and retrieve the value using config('laravel-fulltext.db_connection') ?? config('database.default') we make sure we use the same value Laravel uses now, while still allowing a custom connection.

public function __construct()
{
parent::__construct();
$this->connection = Config::get('laravel-fulltext.db_connection', config('database.default'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project uses the config helper instead of the facade. I prefer to keep it consistent, certainly in one line.

@@ -12,7 +12,7 @@ class CreateLaravelFulltextTable extends Migration
*/
public function up()
{
Schema::create('laravel_fulltext', function (Blueprint $table) {
Schema::connection(config('laravel-fulltext.db_connection'))->create('laravel_fulltext', function (Blueprint $table) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add the default/fallback here!

@JaZo JaZo mentioned this pull request Oct 13, 2019
9 tasks
@JaZo JaZo closed this in #20 Oct 13, 2019
@JaZo
Copy link
Member

JaZo commented Oct 13, 2019

Thanks @spresnac! I completed the implementation in #20 and released it as 0.17.0. Please note the namepsace has changed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants