Skip to content
Permalink
Browse files

Export SQL in utf8mb4 if available (part of #2321)

  • Loading branch information...
dmoagx committed Nov 20, 2015
1 parent 8b2376d commit 7d4ec977bfdc0fea0af962d3859ba6d39c3c3a2d
Showing with 12 additions and 0 deletions.
  1. +12 −0 Source/SPSQLExporter.m
@@ -170,6 +170,8 @@ - (void)exportOperation

// we require utf8
[connection setEncoding:@"utf8"];
// …but utf8mb4 (aka "really" utf8) would be even better.
BOOL utf8mb4 = [connection setEncoding:@"utf8mb4"];

// Add the dump header to the dump file
[metaString appendString:@"# ************************************************************\n"];
@@ -186,6 +188,15 @@ - (void)exportOperation
[metaString appendString:@"/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"];
[metaString appendString:@"/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"];
[metaString appendString:@"/*!40101 SET NAMES utf8 */;\n"];
if(utf8mb4) {
// !! This being outside of a conditional comment is FULLY INTENTIONAL !!
// We *absolutely* want that to fail if the export includes utf8mb4 data, but the server can't handle it.
// MySQL would _normally_ just drop-replace such characters with "?" (a literal questionmark) without any (visible) complaint.
// Since that means irreversible (and often hard to notice) data corruption,
// the user should CONSCIOUSLY make a decision for that to happen!
//TODO we should link to a website explaining the risk here
[metaString appendString:@"SET NAMES utf8mb4;\n"];
}

[metaString appendString:@"/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"];
[metaString appendString:@"/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"];
@@ -464,6 +475,7 @@ - (void)exportOperation
NSString *data = [[NSString alloc] initWithData:object encoding:[self exportOutputEncoding]];

if (data == nil) {
#warning This can corrupt data! Check if this case ever happens and if so, export as hex-string
data = [[NSString alloc] initWithData:object encoding:NSASCIIStringEncoding];
}

0 comments on commit 7d4ec97

Please sign in to comment.
You can’t perform that action at this time.