Browse Source

Fix #1315 "Ugly" migration fix for MariaDB after a beavior change in a minor release, dropping and recreating the foreign key

New migration to sync up a forgotten table during that fixed migration
pull/1356/head
Timothée Jaussoin 1 year ago
parent
commit
b49be80ed6
  1. 68
      database/migrations/20180510141439_change_length_contacts_id.php
  2. 8
      database/migrations/20180612215122_update_unique_constraint_sessions.php
  3. 51
      database/migrations/20240827184020_change_encrypted_passwords_user_id_length.php
  4. 6
      src/Movim/Migration.php

68
database/migrations/20180510141439_change_length_contacts_id.php

@ -27,6 +27,8 @@ class ChangeLengthContactsId extends Migration
$table->string('jid', 256)->change();
});
$this->dropMySQLForeignKeys();
$this->schema->table('users', function (Blueprint $table) {
$table->string('id', 256)->change();
});
@ -45,6 +47,10 @@ class ChangeLengthContactsId extends Migration
$table->string('user_id', 256)->change();
});
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->string('user_id', 256)->change();
});
$this->schema->table('messages', function (Blueprint $table) {
$table->string('user_id', 256)->change();
$table->string('jidto', 256)->change();
@ -52,6 +58,8 @@ class ChangeLengthContactsId extends Migration
$table->string('resource', 256)->change();
});
$this->recreateMySQLForeignKeys();
$this->enableForeignKeyCheck();
}
@ -77,6 +85,8 @@ class ChangeLengthContactsId extends Migration
$table->string('jid', 64)->change();
});
$this->dropMySQLForeignKeys();
$this->schema->table('users', function (Blueprint $table) {
$table->string('id', 64)->change();
});
@ -95,6 +105,10 @@ class ChangeLengthContactsId extends Migration
$table->string('user_id', 64)->change();
});
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->string('user_id', 64)->change();
});
$this->schema->table('messages', function (Blueprint $table) {
$table->string('user_id', 64)->change();
$table->string('jidto', 96)->change();
@ -102,6 +116,60 @@ class ChangeLengthContactsId extends Migration
$table->string('resource', 96)->change();
});
$this->recreateMySQLForeignKeys();
$this->enableForeignKeyCheck();
}
// Fu** MySQL, need to drop the foreign key to alter the ids
private function dropMySQLForeignKeys()
{
if ($this->schema->getConnection()->getDriverName() == 'mysql') {
$this->schema->table('sessions', function (Blueprint $table) {
$table->dropForeign('sessions_user_id_foreign');
});
$this->schema->table('invites', function (Blueprint $table) {
$table->dropForeign('invites_user_id_foreign');
});
$this->schema->table('caches', function (Blueprint $table) {
$table->dropForeign('caches_user_id_foreign');
});
$this->schema->table('messages', function (Blueprint $table) {
$table->dropForeign('messages_user_id_foreign');
});
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->dropForeign('encrypted_passwords_user_id_foreign');
});
}
}
private function recreateMySQLForeignKeys()
{
if ($this->schema->getConnection()->getDriverName() == 'mysql') {
$this->schema->table('sessions', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
$this->schema->table('invites', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
$this->schema->table('caches', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
$this->schema->table('messages', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
}
}

8
database/migrations/20180612215122_update_unique_constraint_sessions.php

@ -7,17 +7,25 @@ class UpdateUniqueConstraintSessions extends Migration
{
public function up()
{
$this->disableForeignKeyCheck();
$this->schema->table('sessions', function (Blueprint $table) {
$table->dropUnique('host');
$table->unique('user_id');
});
$this->enableForeignKeyCheck();
}
public function down()
{
$this->disableForeignKeyCheck();
$this->schema->table('sessions', function (Blueprint $table) {
$table->dropUnique('sessions_user_id_unique');
$table->unique('username', 'host');
});
$this->enableForeignKeyCheck();
}
}

51
database/migrations/20240827184020_change_encrypted_passwords_user_id_length.php

@ -0,0 +1,51 @@
<?php
use Movim\Migration;
use Illuminate\Database\Schema\Blueprint;
class ChangeEncryptedPasswordsUserIdLength extends Migration
{
public function up()
{
$this->disableForeignKeyCheck();
$this->dropMySQLForeignKeys();
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->string('user_id', 256)->change();
});
$this->recreateMySQLForeignKeys();
$this->enableForeignKeyCheck();
}
public function down()
{
$this->disableForeignKeyCheck();
$this->dropMySQLForeignKeys();
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->string('user_id', 64)->change();
});
$this->recreateMySQLForeignKeys();
$this->enableForeignKeyCheck();
}
private function dropMySQLForeignKeys()
{
if ($this->schema->getConnection()->getDriverName() == 'mysql') {
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->dropForeign('encrypted_passwords_user_id_foreign');
});
}
}
private function recreateMySQLForeignKeys()
{
if ($this->schema->getConnection()->getDriverName() == 'mysql') {
$this->schema->table('encrypted_passwords', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
}
}

6
src/Movim/Migration.php

@ -31,9 +31,6 @@ class Migration extends AbstractMigration
case 'mysql':
$this->schema->getConnection()->unprepared('SET foreign_key_checks = 1');
break;
/*case 'sqlite':
$this->schema->getConnection()->unprepared('PRAGMA foreign_keys = on');
break;*/
}
}
@ -43,9 +40,6 @@ class Migration extends AbstractMigration
case 'mysql':
$this->schema->getConnection()->unprepared('SET foreign_key_checks = 0');
break;
/*case 'sqlite':
$this->schema->getConnection()->unprepared('PRAGMA foreign_keys = off');
break;*/
}
}
}
Loading…
Cancel
Save