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

Ability to use a dedicated Canvas database connection #1283

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/canvas.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

return [

/*
|--------------------------------------------------------------------------
| Canvas Database Connection
|--------------------------------------------------------------------------
|
| This is the database connection you want Canvas to use while storing &
| reading your content. By default Canvas uses the default one. However, you can change that
| to anything you want.
|
*/

'database_connection' => env('CANVAS_DB_CONNECTION', 'mysql'),

/*
|--------------------------------------------------------------------------
| Base Domain
Expand Down
28 changes: 14 additions & 14 deletions database/migrations/2020_09_21_000000_create_canvas_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
*/
public function up(): void
{
Schema::create(self::USERS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::USERS_TABLE, function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
Expand All @@ -84,7 +84,7 @@ public function up(): void
$table->softDeletes();
});

Schema::create(self::TAGS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::TAGS_TABLE, function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('slug');
$table->string('name');
Expand All @@ -99,7 +99,7 @@ public function up(): void
$table->unique(['slug', 'user_id']);
});

Schema::create(self::TOPICS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::TOPICS_TABLE, function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('slug');
$table->string('name');
Expand All @@ -114,7 +114,7 @@ public function up(): void
$table->unique(['slug', 'user_id']);
});

Schema::create(self::POSTS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::POSTS_TABLE, function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('slug');
$table->string('title');
Expand All @@ -132,14 +132,14 @@ public function up(): void
$table->unique(['slug', 'user_id']);
});

Schema::create(self::POSTS_TAGS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::POSTS_TAGS_TABLE, function (Blueprint $table) {
$table->foreignUuid('post_id')->index()->references('id')->on(self::POSTS_TABLE);
$table->foreignUuid('tag_id')->index()->references('id')->on(self::TAGS_TABLE);

$table->unique(['post_id', 'tag_id']);
});

Schema::create(self::VIEWS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::VIEWS_TABLE, function (Blueprint $table) {
$table->increments('id');
$table->foreignUuid('post_id')->index()->references('id')->on(self::POSTS_TABLE);
$table->string('ip')->nullable();
Expand All @@ -150,7 +150,7 @@ public function up(): void
$table->index('created_at');
});

Schema::create(self::VISITS_TABLE, function (Blueprint $table) {
Schema::connection(config('canvas.database_connection'))->create(self::VISITS_TABLE, function (Blueprint $table) {
$table->increments('id');
$table->foreignUuid('post_id')->references('id')->on(self::POSTS_TABLE);
$table->string('ip')->nullable();
Expand All @@ -169,12 +169,12 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists(self::USERS_TABLE);
Schema::dropIfExists(self::TAGS_TABLE);
Schema::dropIfExists(self::TOPICS_TABLE);
Schema::dropIfExists(self::POSTS_TABLE);
Schema::dropIfExists(self::POSTS_TAGS_TABLE);
Schema::dropIfExists(self::VIEWS_TABLE);
Schema::dropIfExists(self::VISITS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::USERS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::TAGS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::TOPICS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::POSTS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::POSTS_TAGS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::VIEWS_TABLE);
Schema::connection(config('canvas.database_connection'))->dropIfExists(self::VISITS_TABLE);
}
};
1 change: 1 addition & 0 deletions src/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MigrateCommand extends Command
public function handle()
{
$this->callSilent('migrate', [
'--database' => config('canvas.database_connection'),
'--path' => 'vendor/austintoddj/canvas/database/migrations',
'--force' => $this->option('force') ?? true,
]);
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Requests/AuthenticatedSessionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public function rules()
{
$this->redirect = route('canvas.login.view');

$connection = config('canvas.database_connection');

return [
'email' => 'required|email:filter|exists:canvas_users',
'email' => "required|email:filter|exists:{$connection}.canvas_users",
'password' => 'required',
];
}
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Requests/NewPasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public function authorize()
*/
public function rules()
{
$connection = config('canvas.database_connection');

return [
'token' => 'required',
'email' => 'required|email:filter|exists:canvas_users',
'email' => "required|email:filter|exists:{$connection}.canvas_users",
'password' => 'required|confirmed|min:8',
];
}
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Requests/PasswordResetLinkRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public function authorize()
*/
public function rules()
{
$connection = config('canvas.database_connection');

return [
'email' => 'required|email:filter|exists:canvas_users',
'email' => "required|email:filter|exists:{$connection}.canvas_users",
];
}

Expand Down
4 changes: 3 additions & 1 deletion src/Http/Requests/StorePostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public function authorize()
*/
public function rules()
{
$connection = config('canvas.database_connection');

return [
'slug' => [
'required',
'alpha_dash',
Rule::unique('canvas_posts')->where(function (Builder $query) {
Rule::unique("{$connection}.canvas_posts")->where(function (Builder $query) {
return $query->where('slug', request('slug'))->where('user_id', request()->user('canvas')->id);
})->ignore($this->route('id'))->whereNull('deleted_at'),
],
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Requests/StoreTagRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public function authorize()
*/
public function rules()
{
$connection = config('canvas.database_connection');

return [
'name' => 'required|string',
'slug' => [
'required',
'alpha_dash',
Rule::unique('canvas_tags')->where(function (Builder $query) {
Rule::unique("{$connection}.canvas_tags")->where(function (Builder $query) {
return $query->where('slug', request('slug'))->where('user_id', request()->user('canvas')->id);
})->ignore($this->route('id'))->whereNull('deleted_at'),
],
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Requests/StoreTopicRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public function authorize()
*/
public function rules()
{
$connection = config('canvas.database_connection');

return [
'name' => 'required|string',
'slug' => [
'required',
'alpha_dash',
Rule::unique('canvas_topics')->where(function (Builder $query) {
Rule::unique("{$connection}.canvas_topics")->where(function (Builder $query) {
return $query->where('slug', request('slug'))->where('user_id', request()->user('canvas')->id);
})->ignore($this->route('id'))->whereNull('deleted_at'),
],
Expand Down
6 changes: 4 additions & 2 deletions src/Http/Requests/StoreUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ public function authorize()
*/
public function rules()
{
$connection = config('canvas.database_connection');

return [
'name' => 'required|string',
'email' => [
'required',
'email:filter',
Rule::unique('canvas_users')->where(function (Builder $query) {
Rule::unique("{$connection}.canvas_users")->where(function (Builder $query) {
return $query->where('email', request('email'));
})->ignore($this->route('id'))->whereNull('deleted_at'),
],
'username' => [
'nullable',
'alpha_dash',
Rule::unique('canvas_users')->where(function (Builder $query) {
Rule::unique("{$connection}.canvas_users")->where(function (Builder $query) {
return $query->where('username', request('username'));
})->ignore($this->route('id'))->whereNull('deleted_at'),
],
Expand Down
20 changes: 20 additions & 0 deletions src/Models/AbstractCanvasModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Canvas\Models;

use Illuminate\Database\Eloquent\Model;

abstract class AbstractCanvasModel extends Model
{
/**
* Get the current connection name for the model.
*
* @return string
*/
public function getConnectionName()
{
return config('canvas.database_connection');
}
}
3 changes: 1 addition & 2 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Canvas\Database\Factories\PostFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
class Post extends AbstractCanvasModel
{
use HasFactory, SoftDeletes;

Expand Down
3 changes: 1 addition & 2 deletions src/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

use Canvas\Database\Factories\TagFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tag extends Model
class Tag extends AbstractCanvasModel
{
use HasFactory, SoftDeletes;

Expand Down
3 changes: 1 addition & 2 deletions src/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

use Canvas\Database\Factories\TopicFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Topic extends Model
class Topic extends AbstractCanvasModel
{
use HasFactory, SoftDeletes;

Expand Down
69 changes: 67 additions & 2 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Canvas\Canvas;
use Canvas\Database\Factories\UserFactory;
use Canvas\Traits\HasRole;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
class User extends AbstractCanvasModel implements Authenticatable
{
use HasFactory, HasRole, SoftDeletes;

Expand Down Expand Up @@ -169,6 +169,71 @@ public function getDefaultLocaleAttribute()
return config('app.locale');
}

/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getAuthIdentifierName()};
}

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}

/**
* Get the token value for the "remember me" session.
*
* @return string|null
*/
public function getRememberToken()
{
if (! empty($this->getRememberTokenName())) {
return (string) $this->{$this->getRememberTokenName()};
}
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
if (! empty($this->getRememberTokenName())) {
$this->{$this->getRememberTokenName()} = $value;
}
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return $this->rememberTokenName;
}

/**
* Create a new factory instance for the model.
*
Expand Down
3 changes: 1 addition & 2 deletions src/Models/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use Canvas\Database\Factories\ViewFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class View extends Model
class View extends AbstractCanvasModel
{
use HasFactory;

Expand Down
3 changes: 1 addition & 2 deletions src/Models/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use Canvas\Database\Factories\VisitFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Visit extends Model
class Visit extends AbstractCanvasModel
{
use HasFactory;

Expand Down