Skip to content

Commit

Permalink
Fixed database migration bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlosIsaris committed Nov 15, 2024
1 parent 7b99ba2 commit bcaee27
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"Tests\\": "tests/",
"Database\\Helpers\\": "database/helpers/"
}
},
"extra": {
Expand Down
20 changes: 20 additions & 0 deletions database/helpers/ColumnTypeHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Helpers;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class ColumnTypeHelper {
public static function getColumnType(string $tableName, string $columnName): string {
$databaseName = Config::get('database.connections.mysql.database');

return DB::selectOne('
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?
AND COLUMN_NAME = ?
', [$databaseName, $tableName, $columnName])->DATA_TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Database\Helpers\ColumnTypeHelper;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -10,11 +11,18 @@
*/
public function up(): void {
if (!Schema::hasTable('crowd_sourcing_project_problem_solution_upvotes')) {
Schema::create('crowd_sourcing_project_problem_solution_upvotes', function (Blueprint $table) {
$columnType = ColumnTypeHelper::getColumnType('users', 'id');
Schema::create('crowd_sourcing_project_problem_solution_upvotes', function (Blueprint $table) use ($columnType) {
$table->unsignedBigInteger('solution_id');
$table->foreign('solution_id', 'csp_problem_solution_upvotes_solution_id_foreign')->references('id')->on('crowd_sourcing_project_problem_solutions');

$table->unsignedInteger('user_voter_id');
if ($columnType === 'bigint') {
echo 'big int';
$table->unsignedBigInteger('user_voter_id');
} else {
echo 'int';
$table->unsignedInteger('user_voter_id');
}
$table->foreign('user_voter_id', 'csp_problem_solution_upvotes_user_voter_id_foreign')->references('id')->on('users');

$table->primary(['solution_id', 'user_voter_id'], 'crowd_sourcing_project_problem_solution_upvotes_primary');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Database\Helpers\ColumnTypeHelper;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -12,8 +13,17 @@ public function up(): void {
if (Schema::hasColumn('crowd_sourcing_project_problem_solutions', 'user_creator_id')) {
return;
}
Schema::table('crowd_sourcing_project_problem_solutions', function (Blueprint $table) {
$table->unsignedInteger('user_creator_id')->after('problem_id')->nullable(false);


// Check the data type of the users.id column
$columnType = ColumnTypeHelper::getColumnType('users', 'id');

Schema::table('crowd_sourcing_project_problem_solutions', function (Blueprint $table) use ($columnType) {
if ($columnType === 'bigint') {
$table->unsignedBigInteger('user_creator_id')->after('problem_id')->nullable(false);
} else {
$table->unsignedInteger('user_creator_id')->after('problem_id')->nullable(false);
}
$table->foreign('user_creator_id')->references('id')->on('users');
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Database\Helpers\ColumnTypeHelper;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -12,8 +13,14 @@ public function up(): void {
if (Schema::hasColumn('crowd_sourcing_project_problems', 'user_creator_id')) {
return;
}
Schema::table('crowd_sourcing_project_problems', function (Blueprint $table) {
$table->unsignedInteger('user_creator_id')->after('project_id')->nullable(true);
$columnType = ColumnTypeHelper::getColumnType('users', 'id');

Schema::table('crowd_sourcing_project_problems', function (Blueprint $table) use ($columnType) {
if ($columnType === 'bigint') {
$table->unsignedBigInteger('user_creator_id')->after('project_id')->nullable(true);
} else {
$table->unsignedInteger('user_creator_id')->after('project_id')->nullable(true);
}
$table->foreign('user_creator_id')->references('id')->on('users');
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Database\Helpers\ColumnTypeHelper;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -9,11 +10,17 @@
* Run the migrations.
*/
public function up(): void {
Schema::create('csp_problem_user_bookmarks', function (Blueprint $table) {
$columnType = ColumnTypeHelper::getColumnType('users', 'id');

Schema::create('csp_problem_user_bookmarks', function (Blueprint $table) use ($columnType) {
$table->unsignedBigInteger('problem_id');
$table->foreign('problem_id')->references('id')->on('crowd_sourcing_project_problems');

$table->unsignedInteger('user_id');
if ($columnType === 'bigint') {
$table->unsignedBigInteger('user_id');
} else {
$table->unsignedInteger('user_id');
}
$table->foreign('user_id')->references('id')->on('users');

$table->primary(['problem_id', 'user_id'], 'csp_problem_user_bookmarks_primary');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class="copy-clipboard action-btn dropdown-item">
@if (!$viewModel->isQuestionnaireArchived($questionnaire) && $questionnaire->project_slugs)
@foreach(explode(",", $questionnaire->project_slugs) as $project_slug)
<a class="action-btn dropdown-item"
href="{{route('questionnaire-moderator-add-response',
['questionnaire' => $questionnaire->id, 'project' => $project_slug])}}"><i
href="{{route('questionnaire-moderator-add-response', ['questionnaire' => $questionnaire->id, 'project' => $project_slug])}}"><i
class="fas fa-plus"></i> Add Response
| {{ explode(",", $questionnaire->project_names)[$loop->index] }}
</a>
Expand Down

0 comments on commit bcaee27

Please sign in to comment.