Updated migration to fix field names

This commit is contained in:
snipe 2017-01-26 04:49:01 -08:00
parent 19313e4b83
commit b0f84fa82b

View file

@ -3,8 +3,9 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use App\Models\CustomField;
use Illuminate\Database\Schema\Blueprint;
/**
/**
* Fixes issue #2551 where columns got donked if the field name in non-ascii
* format.
*
@ -16,8 +17,10 @@ use App\Models\CustomField;
* @since [v4.0]
* @return Array
*/
function updateLegacyColumnName($customfield) {
function updateLegacyColumnName($customfield) {
$name_to_db_name = CustomField::name_to_db_name($customfield->name);
\Log::debug('Trying to rename '.$name_to_db_name." to ".$customfield->convertUnicodeDbSlug()."...\n");
if (Schema::hasColumn(CustomField::$table_name, $name_to_db_name)) {
@ -27,9 +30,13 @@ use App\Models\CustomField;
}
);
} else {
\Log::debug('Legacy DB column '.$name_to_db_name.' was not found on the assets table.');
}
}
}
class FixUtf8CustomFieldColumnNames extends Migration
@ -42,10 +49,26 @@ class FixUtf8CustomFieldColumnNames extends Migration
public function up()
{
Schema::table('custom_fields', function ($table) {
$table->string('db_column')->nullable();
$table->text('help_text')->nullable();
});
foreach(CustomField::all() as $field) {
$db_column = $field->convertUnicodeDbSlug();
DB::table('custom_fields')
->where('id', $field->id)
->update(['db_column' => $db_column]);
// change the name of the column
updateLegacyColumnName($field);
}
}
/**
@ -55,7 +78,10 @@ class FixUtf8CustomFieldColumnNames extends Migration
*/
public function down()
{
//
Schema::table('custom_fields', function ($table) {
$table->dropColumn('db_column');
$table->dropColumn('help_text');
});
}
}