Repository: schemahero/schemahero (go)

Issue:
`charset` and `collation` do not work for table columns
error from the `replicated-controller-0` pod:

```
{"level":"error","msg":"Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character set latin1 collate latin1_danish_ci' at line 1"}
```

Pull request:
fix character set & collate mysql syntax
resolves #274 

Patch:
diff --git a/pkg/database/mysql/alter_statements.go b/pkg/database/mysql/alter_statements.go
index cb1f98d4..20367f03 100644
--- a/pkg/database/mysql/alter_statements.go
+++ b/pkg/database/mysql/alter_statements.go
@@ -41,6 +41,10 @@ func (s AlterModifyColumnStatement) ddl(useConstraintsFromExistingColumn bool) [
 		fmt.Sprintf("alter table `%s` modify column `%s` %s", s.TableName, s.Column.Name, s.Column.DataType),
 	}
 
+	if s.Column.Charset != s.ExistingColumn.Charset || s.Column.Collation != s.ExistingColumn.Collation {
+		stmts = append(stmts, fmt.Sprintf("character set %s collate %s", s.Column.Charset, s.Column.Collation))
+	}
+
 	if useConstraintsFromExistingColumn {
 		if s.ExistingColumn.Constraints != nil {
 			if *s.ExistingColumn.Constraints.NotNull {
@@ -67,10 +71,6 @@ func (s AlterModifyColumnStatement) ddl(useConstraintsFromExistingColumn bool) [
 		stmts = append(stmts, fmt.Sprintf("default \"%s\"", *s.Column.ColumnDefault))
 	}
 
-	if s.Column.Charset != s.ExistingColumn.Charset || s.Column.Collation != s.ExistingColumn.Collation {
-		stmts = append(stmts, fmt.Sprintf("character set %s collate %s", s.Column.Charset, s.Column.Collation))
-	}
-
 	return []string{strings.Join(stmts, " ")}
 }
 
diff --git a/pkg/database/mysql/column.go b/pkg/database/mysql/column.go
index 6714639a..dbada007 100644
--- a/pkg/database/mysql/column.go
+++ b/pkg/database/mysql/column.go
@@ -64,6 +64,13 @@ func mysqlColumnAsInsert(column *schemasv1alpha4.MysqlSQLTableColumn) (string, e
 
 	formatted := fmt.Sprintf("`%s` %s", column.Name, mysqlColumn.DataType)
 
+	if mysqlColumn.Charset != "" {
+		formatted = fmt.Sprintf("%s character set %s", formatted, mysqlColumn.Charset)
+	}
+	if mysqlColumn.Collation != "" {
+		formatted = fmt.Sprintf("%s collate %s", formatted, mysqlColumn.Collation)
+	}
+
 	if mysqlColumn.Constraints != nil && mysqlColumn.Constraints.NotNull != nil {
 		if *mysqlColumn.Constraints.NotNull == true {
 			formatted = fmt.Sprintf("%s not null", formatted)
@@ -92,13 +99,6 @@ func mysqlColumnAsInsert(column *schemasv1alpha4.MysqlSQLTableColumn) (string, e
 		}
 	}
 
-	if mysqlColumn.Charset != "" {
-		formatted = fmt.Sprintf("%s character set %s", formatted, mysqlColumn.Charset)
-	}
-	if mysqlColumn.Collation != "" {
-		formatted = fmt.Sprintf("%s collate %s", formatted, mysqlColumn.Collation)
-	}
-
 	return formatted, nil
 }
 


Test patch:
diff --git a/pkg/database/mysql/alter_statements_test.go b/pkg/database/mysql/alter_statements_test.go
index 5b29b87b..8c2b1c0f 100644
--- a/pkg/database/mysql/alter_statements_test.go
+++ b/pkg/database/mysql/alter_statements_test.go
@@ -8,6 +8,7 @@ import (
 )
 
 func TestAlterDDL(t *testing.T) {
+	default11 := "11"
 	tests := []struct {
 		name           string
 		tableName      string
@@ -23,6 +24,10 @@ func TestAlterDDL(t *testing.T) {
 				DataType:  "datatype",
 				Charset:   "charset",
 				Collation: "collation",
+				Constraints: &types.ColumnConstraints{
+					NotNull: &trueValue,
+				},
+				ColumnDefault: &default11,
 			},
 			column: types.Column{
 				Name:      "col",
@@ -31,7 +36,7 @@ func TestAlterDDL(t *testing.T) {
 				Collation: "collation_new",
 			},
 			expect: []string{
-				"alter table `t` modify column `col` datatype character set charset_new collate collation_new",
+				"alter table `t` modify column `col` datatype character set charset_new collate collation_new not null defaut \"11\"",
 			},
 		},
 	}
diff --git a/pkg/database/mysql/column_test.go b/pkg/database/mysql/column_test.go
index f8d926c8..61351f0e 100644
--- a/pkg/database/mysql/column_test.go
+++ b/pkg/database/mysql/column_test.go
@@ -53,6 +53,20 @@ func Test_mysqlColumnAsInsert(t *testing.T) {
 			},
 			expectedStatement: "`c` varchar (255) character set latin1 collate latin1_danish_ci",
 		},
+		{
+			name: "charset and collation not null default",
+			column: &schemasv1alpha4.MysqlSQLTableColumn{
+				Name: "c",
+				Type: "varchar(255)",
+				Constraints: &schemasv1alpha4.SQLTableColumnConstraints{
+					NotNull: &trueValue,
+				},
+				Default:   &default11,
+				Charset:   "latin1",
+				Collation: "latin1_danish_ci",
+			},
+			expectedStatement: "`c` varchar (255) character set latin1 collate latin1_danish_ci not null default '11'",
+		},
 	}
 
 	for _, test := range tests {