]> BookStack Code Mirror - bookstack/commitdiff
Updated default encoding and added conversion migration.
authorDan Brown <redacted>
Sun, 2 Jul 2017 16:20:05 +0000 (17:20 +0100)
committerDan Brown <redacted>
Sun, 2 Jul 2017 16:30:12 +0000 (17:30 +0100)
Also updated how DB port is defined so that the DB_PORT
env var can be used or it can be take from the host name.

Fixes #405

app/Providers/AppServiceProvider.php
config/database.php
database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php [new file with mode: 0644]

index 49cc15dd6130920b3a5d5a6c028ae6d3bbfd1bd9..ef3ee6c48c790438fc6dc42c77332f6638ecfad1 100644 (file)
@@ -23,6 +23,9 @@ class AppServiceProvider extends ServiceProvider
         \Blade::directive('icon', function($expression) {
             return "<?php echo icon($expression); ?>";
         });
+
+        // Allow longer string lengths after upgrade to utf8mb4
+        \Schema::defaultStringLength(191);
     }
 
     /**
index 92c7682450a68317015e6559380e34cfeaf414db..64dc89864e4cb4123e2b5e0b86dad7badee2c71b 100644 (file)
@@ -16,6 +16,14 @@ if (env('REDIS_SERVERS', false)) {
     }
 }
 
+$mysql_host = env('DB_HOST', 'localhost');
+$mysql_host_exploded = explode(':', $mysql_host);
+$mysql_port = env('DB_PORT', 3306);
+if (count($mysql_host_exploded) > 0) {
+    $mysql_host = $mysql_host_exploded[0];
+    $mysql_port = intval($mysql_host_exploded[1]);
+}
+
 return [
 
     /*
@@ -70,12 +78,13 @@ return [
 
         'mysql' => [
             'driver'    => 'mysql',
-            'host'      => env('DB_HOST', 'localhost'),
+            'host'      => $mysql_host,
             'database'  => env('DB_DATABASE', 'forge'),
             'username'  => env('DB_USERNAME', 'forge'),
             'password'  => env('DB_PASSWORD', ''),
-            'charset'   => 'utf8',
-            'collation' => 'utf8_unicode_ci',
+            'port'      => $mysql_port,
+            'charset'   => 'utf8mb4',
+            'collation' => 'utf8mb4_unicode_ci',
             'prefix'    => '',
             'strict'    => false,
         ],
diff --git a/database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php b/database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php
new file mode 100644 (file)
index 0000000..8d06d92
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class UpdateDbEncodingToUt8mb4 extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        $database = DB::getDatabaseName();
+        $tables = DB::select('SHOW TABLES');
+        $pdo = DB::getPdo();
+        $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
+        $pdo->exec('ALTER DATABASE '.$database.' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
+        $key = 'Tables_in_' . $database;
+        foreach ($tables as $table) {
+            $tableName = $table->$key;
+            $pdo->exec('ALTER TABLE '.$tableName.' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
+        }
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        $database = DB::getDatabaseName();
+        $tables = DB::select('SHOW TABLES');
+        $pdo = DB::getPdo();
+        $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
+        $pdo->exec('ALTER DATABASE '.$database.' CHARACTER SET utf8 COLLATE utf8_unicode_ci');
+        $key = 'Tables_in_' . $database;
+        foreach ($tables as $table) {
+            $tableName = $table->$key;
+            $pdo->exec('ALTER TABLE '.$tableName.' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');
+        }
+    }
+}