Skip to content

Commit

Permalink
Fix custom connection in database migrations (#118)
Browse files Browse the repository at this point in the history
* Fix custom connection in migrations

* Fix custom connection in setup commands
  • Loading branch information
antonkomarev authored Sep 13, 2019
1 parent f75c84f commit 6aec097
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 21 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to `laravel-love` will be documented in this file.

## [Unreleased]

## [8.1.1] - 2019-09-13

### Fixed

- ([#118]) Fix custom connection in database migrations

## [8.1.0] - 2019-09-03

### Added
Expand Down Expand Up @@ -389,7 +395,8 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to

- Initial release

[Unreleased]: https://github.com/cybercog/laravel-love/compare/8.1.0...master
[Unreleased]: https://github.com/cybercog/laravel-love/compare/8.1.1...master
[8.1.0]: https://github.com/cybercog/laravel-love/compare/8.1.0...8.1.1
[8.1.0]: https://github.com/cybercog/laravel-love/compare/8.0.0...8.1.0
[8.0.0]: https://github.com/cybercog/laravel-love/compare/7.2.1...8.0.0
[7.2.1]: https://github.com/cybercog/laravel-love/compare/7.2.0...7.2.1
Expand Down Expand Up @@ -424,6 +431,7 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to
[1.1.1]: https://github.com/cybercog/laravel-love/compare/1.1.0...1.1.1
[1.1.0]: https://github.com/cybercog/laravel-love/compare/1.0.0...1.1.0

[#118]: https://github.com/cybercog/laravel-love/pull/118
[#114]: https://github.com/cybercog/laravel-love/pull/114
[#113]: https://github.com/cybercog/laravel-love/pull/113
[#111]: https://github.com/cybercog/laravel-love/pull/111
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreateLoveReactersTable extends Migration
{
Expand All @@ -24,7 +23,7 @@ final class CreateLoveReactersTable extends Migration
*/
public function up(): void
{
Schema::create('love_reacters', function (Blueprint $table) {
$this->schema->create('love_reacters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('type');
$table->timestamps();
Expand All @@ -40,6 +39,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('love_reacters');
$this->schema->dropIfExists('love_reacters');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreateLoveReactantsTable extends Migration
{
Expand All @@ -24,7 +23,7 @@ final class CreateLoveReactantsTable extends Migration
*/
public function up(): void
{
Schema::create('love_reactants', function (Blueprint $table) {
$this->schema->create('love_reactants', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('type');
$table->timestamps();
Expand All @@ -40,6 +39,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('love_reactants');
$this->schema->dropIfExists('love_reactants');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreateLoveReactionTypesTable extends Migration
{
Expand All @@ -24,7 +23,7 @@ final class CreateLoveReactionTypesTable extends Migration
*/
public function up(): void
{
Schema::create('love_reaction_types', function (Blueprint $table) {
$this->schema->create('love_reaction_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->tinyInteger('mass');
Expand All @@ -41,6 +40,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('love_reaction_types');
$this->schema->dropIfExists('love_reaction_types');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreateLoveReactionsTable extends Migration
{
Expand All @@ -24,7 +23,7 @@ final class CreateLoveReactionsTable extends Migration
*/
public function up(): void
{
Schema::create('love_reactions', function (Blueprint $table) {
$this->schema->create('love_reactions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('reactant_id');
$table->unsignedBigInteger('reacter_id');
Expand Down Expand Up @@ -75,6 +74,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('love_reactions');
$this->schema->dropIfExists('love_reactions');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreateLoveReactantReactionCountersTable extends Migration
{
Expand All @@ -24,7 +23,7 @@ final class CreateLoveReactantReactionCountersTable extends Migration
*/
public function up(): void
{
Schema::create('love_reactant_reaction_counters', function (Blueprint $table) {
$this->schema->create('love_reactant_reaction_counters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('reactant_id');
$table->unsignedBigInteger('reaction_type_id');
Expand Down Expand Up @@ -57,6 +56,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('love_reactant_reaction_counters');
$this->schema->dropIfExists('love_reactant_reaction_counters');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreateLoveReactantReactionTotalsTable extends Migration
{
Expand All @@ -24,7 +23,7 @@ final class CreateLoveReactantReactionTotalsTable extends Migration
*/
public function up(): void
{
Schema::create('love_reactant_reaction_totals', function (Blueprint $table) {
$this->schema->create('love_reactant_reaction_totals', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('reactant_id');
$table->unsignedBigInteger('count');
Expand All @@ -46,6 +45,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('love_reactant_reaction_totals');
$this->schema->dropIfExists('love_reactant_reaction_totals');
}
}
3 changes: 2 additions & 1 deletion src/Console/Commands/SetupReactable.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ public function handle(): int

$table = $model->getTable();
$referencedModel = new Reactant();
$referencedSchema = Schema::connection($referencedModel->getConnectionName());
$referencedTable = $referencedModel->getTable();
$referencedColumn = $referencedModel->getKeyName();

if (!Schema::hasTable($referencedTable)) {
if (!$referencedSchema->hasTable($referencedTable)) {
$this->error(sprintf(
'Referenced table `%s` does not exists in database.',
$referencedTable
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/SetupReacterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ public function handle(): int

$table = $model->getTable();
$referencedModel = new Reacter();
$referencedSchema = Schema::connection($referencedModel->getConnectionName());
$referencedTable = $referencedModel->getTable();
$referencedColumn = $referencedModel->getKeyName();

if (!Schema::hasTable($referencedTable)) {
if (!$referencedSchema->hasTable($referencedTable)) {
$this->error(sprintf(
'Referenced table `%s` does not exists in database.',
$referencedTable
Expand Down
13 changes: 13 additions & 0 deletions src/Support/Database/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@

use Illuminate\Database\Migrations\Migration as IlluminateMigration;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;

abstract class Migration extends IlluminateMigration
{
/**
* The database schema.
*
* @var \Illuminate\Support\Facades\Schema
*/
protected $schema;

public function __construct()
{
$this->schema = Schema::connection($this->getConnection());
}

/**
* Get the migration connection name.
*
Expand Down

0 comments on commit 6aec097

Please sign in to comment.