Repository: schemahero/schemahero
Pull request #975 (2023-09-27 09:44:10)

feat: add support for mysql json column type
adds support to create mysql columns of type json.


Patch:
diff --git a/pkg/database/mysql/parameterized.go b/pkg/database/mysql/parameterized.go
index 44ed8742..26e1ddbf 100644
--- a/pkg/database/mysql/parameterized.go
+++ b/pkg/database/mysql/parameterized.go
@@ -305,6 +305,8 @@ func maybeParseParameterizedColumnType(requestedType string) (string, error) {
 			}
 			columnType = fmt.Sprintf("text (%d)", max)
 		}
+	} else if strings.HasPrefix(requestedType, "json") {
+		columnType = "json"
 	}
 
 	return columnType, nil


Test patch:
diff --git a/integration/tests/mysql/json/Dockerfile b/integration/tests/mysql/json/Dockerfile
new file mode 100644
index 00000000..367f3cf1
--- /dev/null
+++ b/integration/tests/mysql/json/Dockerfile
@@ -0,0 +1,9 @@
+FROM mysql:8.0
+
+ENV MYSQL_USER=schemahero
+ENV MYSQL_PASSWORD=password
+ENV MYSQL_DATABASE=schemahero
+ENV MYSQL_RANDOM_ROOT_PASSWORD=1
+
+## Insert fixtures
+COPY ./fixtures.sql /docker-entrypoint-initdb.d/
diff --git a/integration/tests/mysql/json/Makefile b/integration/tests/mysql/json/Makefile
new file mode 100644
index 00000000..c4a987bd
--- /dev/null
+++ b/integration/tests/mysql/json/Makefile
@@ -0,0 +1,4 @@
+include ../common.mk
+
+TEST_NAME := json
+SPEC_FILE := ./specs
diff --git a/integration/tests/mysql/json/expect.sql b/integration/tests/mysql/json/expect.sql
new file mode 100644
index 00000000..2fb3e7de
--- /dev/null
+++ b/integration/tests/mysql/json/expect.sql
@@ -0,0 +1,2 @@
+create table `complex_data` (`id` int (11), `complex_data` json not null, primary key (`id`));
+alter table `other` add column `complex_data` json not null;
diff --git a/integration/tests/mysql/json/fixtures.sql b/integration/tests/mysql/json/fixtures.sql
new file mode 100644
index 00000000..6b4a6cc8
--- /dev/null
+++ b/integration/tests/mysql/json/fixtures.sql
@@ -0,0 +1,4 @@
+create table other (
+  id integer primary key not null,
+  address mediumtext
+);
diff --git a/integration/tests/mysql/json/specs/json.yaml b/integration/tests/mysql/json/specs/json.yaml
new file mode 100644
index 00000000..0d3f6606
--- /dev/null
+++ b/integration/tests/mysql/json/specs/json.yaml
@@ -0,0 +1,14 @@
+database: schemahero
+name: complex_data
+requires: []
+schema:
+  mysql:
+    primaryKey:
+      - id
+    columns:
+      - name: id
+        type: integer
+      - name: complex_data
+        type: json
+        constraints:
+          notNull: true
diff --git a/integration/tests/mysql/json/specs/other.yaml b/integration/tests/mysql/json/specs/other.yaml
new file mode 100644
index 00000000..1fc3047d
--- /dev/null
+++ b/integration/tests/mysql/json/specs/other.yaml
@@ -0,0 +1,15 @@
+database: schemahero
+name: other
+schema:
+  mysql:
+    primaryKey:
+      - id
+    columns:
+      - name: id
+        type: integer
+      - name: address
+        type: mediumtext
+      - name: complex_data
+        type: json
+        constraints:
+          notNull: true
diff --git a/pkg/database/mysql/column_test.go b/pkg/database/mysql/column_test.go
index fceb8f3e..3860b5c5 100644
--- a/pkg/database/mysql/column_test.go
+++ b/pkg/database/mysql/column_test.go
@@ -67,6 +67,17 @@ func Test_mysqlColumnAsInsert(t *testing.T) {
 			},
 			expectedStatement: "`c` varchar (255) character set latin1 collate latin1_danish_ci not null default '11'",
 		},
+		{
+			name: "json field type",
+			column: &schemasv1alpha4.MysqlTableColumn{
+				Name: "obj",
+				Type: "json",
+				Constraints: &schemasv1alpha4.MysqlTableColumnConstraints{
+					NotNull: &trueValue,
+				},
+			},
+			expectedStatement: "`obj` json not null",
+		},
 	}
 
 	for _, test := range tests {
@@ -132,6 +143,18 @@ func Test_InsertColumnStatement(t *testing.T) {
 			},
 			expectedStatement: "alter table `t` add column `a` int (11) auto_increment",
 		},
+		{
+			name:      "add json column",
+			tableName: "t",
+			desiredColumn: &schemasv1alpha4.MysqlTableColumn{
+				Name: "a",
+				Type: "json",
+				Constraints: &schemasv1alpha4.MysqlTableColumnConstraints{
+					NotNull: &trueValue,
+				},
+			},
+			expectedStatement: "alter table `t` add column `a` json not null",
+		},
 	}
 
 	for _, test := range tests {
@@ -192,6 +215,17 @@ func Test_schemaColumnToMysqlColumn(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "json",
+			schemaColumn: &schemasv1alpha4.MysqlTableColumn{
+				Name: "j",
+				Type: "json",
+			},
+			expectedColumn: &types.Column{
+				Name:     "j",
+				DataType: "json",
+			},
+		},
 	}
 
 	for _, test := range tests {