Skip to content

Commit 7d4ec97

Browse files
committed
Export SQL in utf8mb4 if available (part of #2321)
1 parent 8b2376d commit 7d4ec97

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Source/SPSQLExporter.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ - (void)exportOperation
170170

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

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

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

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

0 commit comments

Comments
 (0)