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

Use default config instead of env as connection #615

Closed
Gummibeer opened this issue Oct 10, 2019 · 8 comments · Fixed by #616
Closed

Use default config instead of env as connection #615

Gummibeer opened this issue Oct 10, 2019 · 8 comments · Fixed by #616
Assignees
Labels
enhancement good first issue hacktoberfest https://hacktoberfest.digitalocean.com

Comments

@Gummibeer
Copy link
Collaborator

Gummibeer commented Oct 10, 2019

This issue relates to archtechx/tenancy#164

The idea is to drop the double env() call in package config and use package env or null as default value. In model construct we use the database.default as fallback value.

PS: I would prefer null coalescing operator over function nesting.

// good
config('key') ?? config('key') ?? 'default';
config('key') ?? config('key', 'default');

// bad
config('key', config('key', 'default'));
@Gummibeer Gummibeer added enhancement hacktoberfest https://hacktoberfest.digitalocean.com good first issue labels Oct 10, 2019
micc83 added a commit to micc83/laravel-activitylog that referenced this issue Oct 10, 2019
@micc83
Copy link
Contributor

micc83 commented Oct 10, 2019

Even though tests are green, I see that config('activitylog.database_connection') is clearly used also in the migration. Unless I add the same logic there as well I would break the package. But, in relation to archtechx/tenancy#164 and as I'm not very familiar with this scenario, I'm not sure how you would handle migrations for multiple tenants. Is that migration re-run for each new tenant with an updated database.default config?

@Gummibeer
Copy link
Collaborator Author

Gummibeer commented Oct 10, 2019

@micc83 Good catch and thanks for doing it! :)

@stancl could you help how the migration should be handled?

https://tenancy.samuelstancl.me/docs/v2/tenant-migrations/
It seems like there is a dedicated command for it and the migration has to be placed in a new directory. So the user has to adjust it anyway. In this case I would say that we should only pass the package connection and null as default. The connection() method accepts a string or null and uses the default connection if connection name is null.
So this should be the same like we do in the model construct!?

And something I've noted after taking a look in the code - we could omit the fallback at all. If our package config is null it will use the app default connection. So we only pass in the configured explicit connection or null and let the resolver do its job. This will make it even more consistent with the migration and we don't tinker around with default behavior.

@micc83
Copy link
Contributor

micc83 commented Oct 10, 2019

@Gummibeer I'll reply above point by point:

  1. So this should be the same like we do in the model construct!?

So the user will have to manually copy the CreateActivityLogTable migration to the tenants folder anyway, right? Let's wait for @stancl for more info about it.

  1. We could omit the fallback at all

I see that both DB::connection($param) and Model::getConnection($param) already fallback to database.default when $param is null so, unless there's some other scenario I'm not considering, I'm reverting my change on the App\Activity model to use only config('activitylog.database_connection').

I'll update my fork in a minute.

micc83 added a commit to micc83/laravel-activitylog that referenced this issue Oct 10, 2019
@Gummibeer
Copy link
Collaborator Author

The last point was what I meant. So yes, go for it. And the second seems right to me after reading the docs for 5min.

@micc83
Copy link
Contributor

micc83 commented Oct 10, 2019

@Gummibeer great, check the commit on my fork. If it's ok, while waiting for @stancl feedback I'll open a PR.

@stancl
Copy link

stancl commented Oct 10, 2019

we could omit the fallback at all

This is what a lot of Laravel components/packages do. The published config has no connection key, so it's implicitly null, but it can be set to a specific value.

So this should be the same like we do in the model construct!?

Yes. All my package needs is a fallback to 'default'.

With regards to migrations, they use the database.default connection. They need to be in a special folder just to distinguish them as tenant migrations.

@drbyte Pointed out that he'd want to use the activity log both in the central and the tenant parts of his application. The fallback to default makes that possible.

This use case also requires that the migration is included in both folders.

@micc83
Copy link
Contributor

micc83 commented Oct 10, 2019

@stancl fair enough, so we should be good to go. I'm opening the PR.

micc83 added a commit to micc83/laravel-activitylog that referenced this issue Oct 10, 2019
Gummibeer added a commit that referenced this issue Oct 15, 2019
Fix #615 - Use default config instead of env as connection
@Gummibeer
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue hacktoberfest https://hacktoberfest.digitalocean.com
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants