Repository: ariga/atlas
Pull request #2026 (2023-08-26 10:08:22)

sql/mysql: do not add mariadb json checks to table definition


Patch:
diff --git a/doc/website/docusaurus.config.js b/doc/website/docusaurus.config.js
index 681991b4..be126ac2 100644
--- a/doc/website/docusaurus.config.js
+++ b/doc/website/docusaurus.config.js
@@ -9,7 +9,7 @@ module.exports = {
   organizationName: 'ariga',
   projectName: 'atlas',
   themeConfig: {
-    image: "https://atlas-og-img.vercel.app/**Atlas%20%7C**%20Open-source%20database%20schema%20management.png?theme=dark",
+    image: "https://og.atlasgo.io/image?title=Schema%20Migration%20Tool%20for%20any%20Language",
     prism: {
       additionalLanguages: ['hcl'],
       magicComments: [
diff --git a/sql/mysql/inspect.go b/sql/mysql/inspect.go
index 4561ea2a..2c785468 100644
--- a/sql/mysql/inspect.go
+++ b/sql/mysql/inspect.go
@@ -451,6 +451,9 @@ func (i *inspect) checks(ctx context.Context, s *schema.Schema) error {
 				// Unset the inspected CHARSET/COLLATE attributes
 				// as they are valid only for character types.
 				c.UnsetCharset().UnsetCollation()
+				// Skip adding this CHECK constraint to the table definition
+				// as it is implicitly created by MariaDB for this JSON column.
+				continue
 			}
 		} else if enforced.String == "NO" {
 			// The ENFORCED attribute is not supported by MariaDB.


Test patch:
diff --git a/internal/integration/mysql_test.go b/internal/integration/mysql_test.go
index f92afc43..509d4d3d 100644
--- a/internal/integration/mysql_test.go
+++ b/internal/integration/mysql_test.go
@@ -1268,21 +1268,11 @@ create table atlas_types_sanity
 			require.True(t, ok)
 			expected := schema.Table{
 				Name: n,
-				Attrs: func() []schema.Attr {
-					if t.version == "maria107" {
-						return []schema.Attr{
-							&schema.Charset{V: "latin1"},
-							&schema.Collation{V: "latin1_swedish_ci"},
-							&mysql.Engine{V: "InnoDB", Default: true},
-							&schema.Check{Name: "tJSON", Expr: "json_valid(`tJSON`)"},
-						}
-					}
-					return []schema.Attr{
-						&schema.Charset{V: "latin1"},
-						&schema.Collation{V: "latin1_swedish_ci"},
-						&mysql.Engine{V: "InnoDB", Default: true},
-					}
-				}(),
+				Attrs: []schema.Attr{
+					&schema.Charset{V: "latin1"},
+					&schema.Collation{V: "latin1_swedish_ci"},
+					&mysql.Engine{V: "InnoDB", Default: true},
+				},
 				Schema: realm.Schemas[0],
 				Columns: []*schema.Column{
 					func() *schema.Column {
diff --git a/internal/integration/testdata/mysql/column-json.txt b/internal/integration/testdata/mysql/column-json.txt
new file mode 100644
index 00000000..62407968
--- /dev/null
+++ b/internal/integration/testdata/mysql/column-json.txt
@@ -0,0 +1,53 @@
+only maria*
+
+apply 1.hcl
+cmpshow users 1.sql
+
+# The CHECK "json_valid(`name`)" should not be present in the HCL
+# description because the "longtext" is converted to "json" type.
+cmphcl 1.inspect.hcl
+
+-- 1.hcl --
+schema "script_column_json" {}
+
+table "users" {
+  schema = schema.script_column_json
+  column "id" {
+    null = false
+    type = int
+  }
+  column "name" {
+    null = false
+    type = json
+  }
+  primary_key {
+    columns = [column.id]
+  }
+}
+
+-- 1.sql --
+CREATE TABLE `users` (
+  `id` int(11) NOT NULL,
+  `name` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`name`)),
+  PRIMARY KEY (`id`)
+)
+
+-- 1.inspect.hcl --
+table "users" {
+  schema = schema.script_column_json
+  column "id" {
+    null = false
+    type = int
+  }
+  column "name" {
+    null = false
+    type = json
+  }
+  primary_key {
+    columns = [column.id]
+  }
+}
+schema "script_column_json" {
+  charset = "$charset"
+  collate = "$collate"
+}
\ No newline at end of file
diff --git a/sql/mysql/inspect_test.go b/sql/mysql/inspect_test.go
index dc2f6ac1..b2d4126d 100644
--- a/sql/mysql/inspect_test.go
+++ b/sql/mysql/inspect_test.go
@@ -194,7 +194,6 @@ func TestDriver_InspectTable(t *testing.T) {
 					{Name: "jsonc", Type: &schema.ColumnType{Raw: "json", Type: &schema.JSONType{T: "json"}}},
 				}, t.Columns)
 				require.EqualValues([]schema.Attr{
-					&schema.Check{Name: "jsonc", Expr: "json_valid(`jsonc`)"},
 					&schema.Check{Name: "users_chk_1", Expr: `longtext <> '\'\'""'`},
 				}, t.Attrs)
 			},