package mysql import "testing" func TestUnescapeExpression(t *testing.T) { for _, tc := range []struct{ input, want string }{ {`_utf8mb4\'one\\ntwo\'`, `_utf8mb4'one\ntwo'`}, {`concat(\'don\\\'t\',\' change\')`, `concat('don\'t',' change')`}, {`(uuid())`, `(uuid())`}, } { if got := unescapeExpression(tc.input); got != tc.want { t.Errorf("%q: got %q want %q", tc.input, got, tc.want) } } } func TestNativeColumnDefaults(t *testing.T) { create := "CREATE TABLE `odd(table` (\n" + " `a,b` varbinary(8) DEFAULT 0x0027FF,\n" + " `tick``name` varchar(30) DEFAULT 'don''t, (split)',\n" + " `expr` varchar(30) DEFAULT (concat('DEFAULT, ', (uuid()))),\n" + " `generated` varchar(30) GENERATED ALWAYS AS (concat('DEFAULT',`expr`)) VIRTUAL,\n" + " `new\nline` int DEFAULT 3,\n" + " PRIMARY KEY (`a,b`)\n) ENGINE=InnoDB" got := columnDefaults(create) want := map[string]string{"a,b": "0x0027FF", "tick`name": "'don''t, (split)'", "expr": "(concat('DEFAULT, ', (uuid())))", "new\nline": "3"} if len(got) != len(want) { t.Fatalf("defaults: %v", got) } for name, value := range want { if got[name] != value { t.Errorf("%s: got %q want %q", name, got[name], value) } } } func TestNativeOptionsIgnoreQuotedValues(t *testing.T) { create := "CREATE TABLE `WITH PARSER` (`STORAGE` int COMMENT 'KEY_BLOCK_SIZE', KEY `ENGINE_ATTRIBUTE` (`STORAGE`) WITH PARSER `ngram`) ENGINE=InnoDB" options := unsupportedNativeOptions(create) if len(options) != 1 || options[0] != "WITH PARSER" { t.Fatalf("native options: %v", options) } } func TestNativeAutoIncrementIgnoresQuotedValues(t *testing.T) { for _, tc := range []struct{ create, want string }{ {"CREATE TABLE `x` (`id` bigint AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=18446744073709551615", "18446744073709551615"}, {"CREATE TABLE `x` (`id` bigint AUTO_INCREMENT) COMMENT='AUTO_INCREMENT=12'", "1"}, } { if got := autoIncrementValue(tc.create); got != tc.want { t.Errorf("got %s want %s", got, tc.want) } } }