Repository: ariga/atlas
Pull request #274 (2021-12-01 15:01:17)

sql/sqlspec: changing Table.Schema to be a schemspec.Ref and fixing tests
- [X] Fix code
- [X] Fix Unit tests
- [x] Fix Integration tests
- [x] Fix Cmd
- [x] Fix Docs


Patch:
diff --git a/doc/md/sql.md b/doc/md/sql.md
index 5053b8bb..96287186 100644
--- a/doc/md/sql.md
+++ b/doc/md/sql.md
@@ -25,7 +25,7 @@ A `table` describes a table in a SQL database.
 #### Full Example:
 ```hcl
 table "users" {
-  schema = "default"
+  schema = schema.default
   column "id" {
     type = "int"
   }
@@ -57,13 +57,13 @@ table "users" {
 
 #### Properties
 
-| Name        | Kind            | Type        | Description                                             |
-|-------------|-----------------|-------------|---------------------------------------------------------|
-| schema      | attribute       | string      | References the name of the schema containing the table. |
-| column      | resource (list) | column      | Describes a column in the table.                        |
-| primary_key | resource        | primary_key | Describes the table's primary key.                      |
-| foreign_key | resource (list) | foreign_key | Describes the table's foreign keys.                     |
-| index       | resource (list) | index       | Describes the table's indexes.                          |
+| Name        | Kind            | Type        | Description                                  |
+|-------------|-----------------|-------------|----------------------------------------------|
+| schema      | attribute       | reference   | References the  schema containing the table. |
+| column      | resource (list) | column      | Describes a column in the table.             |
+| primary_key | resource        | primary_key | Describes the table's primary key.           |
+| foreign_key | resource (list) | foreign_key | Describes the table's foreign keys.          |
+| index       | resource (list) | index       | Describes the table's indexes.               |
 
 ### Column
 
diff --git a/sql/internal/specutil/convert.go b/sql/internal/specutil/convert.go
index 116ad6fd..4c123333 100644
--- a/sql/internal/specutil/convert.go
+++ b/sql/internal/specutil/convert.go
@@ -224,7 +224,9 @@ func FromSchema(s *schema.Schema, fn TableSpecFunc) (*sqlspec.Schema, []*sqlspec
 		if err != nil {
 			return nil, nil, err
 		}
-		table.SchemaName = s.Name
+		if s.Name != "" {
+			table.Schema = schemaRef(s.Name)
+		}
 		tables = append(tables, table)
 	}
 	return spec, tables, nil
@@ -270,7 +272,7 @@ func FromTable(t *schema.Table, colFn ColumnSpecFunc, pkFn PrimaryKeySpecFunc, i
 func FromPrimaryKey(s *schema.Index) (*sqlspec.PrimaryKey, error) {
 	c := make([]*schemaspec.Ref, 0, len(s.Parts))
 	for _, v := range s.Parts {
-		c = append(c, toReference(v.C.Name, s.Table.Name))
+		c = append(c, colRef(v.C.Name, s.Table.Name))
 	}
 	return &sqlspec.PrimaryKey{
 		Columns: c,
@@ -284,7 +286,7 @@ func FromIndex(idx *schema.Index) (*sqlspec.Index, error) {
 		if p.C == nil {
 			return nil, errors.New("index expression is not supported")
 		}
-		c = append(c, toReference(p.C.Name, idx.Table.Name))
+		c = append(c, colRef(p.C.Name, idx.Table.Name))
 	}
 	return &sqlspec.Index{
 		Name:    idx.Name,
@@ -297,11 +299,11 @@ func FromIndex(idx *schema.Index) (*sqlspec.Index, error) {
 func FromForeignKey(s *schema.ForeignKey) (*sqlspec.ForeignKey, error) {
 	c := make([]*schemaspec.Ref, 0, len(s.Columns))
 	for _, v := range s.Columns {
-		c = append(c, toReference(v.Name, s.Table.Name))
+		c = append(c, colRef(v.Name, s.Table.Name))
 	}
 	r := make([]*schemaspec.Ref, 0, len(s.RefColumns))
 	for _, v := range s.RefColumns {
-		r = append(r, toReference(v.Name, s.RefTable.Name))
+		r = append(r, colRef(v.Name, s.RefTable.Name))
 	}
 	return &sqlspec.ForeignKey{
 		Symbol:     s.Symbol,
@@ -334,9 +336,13 @@ func tableName(ref *schemaspec.Ref) (string, error) {
 	return s[1], nil
 }
 
-func toReference(cName string, tName string) *schemaspec.Ref {
+func colRef(cName string, tName string) *schemaspec.Ref {
 	v := "$table." + tName + ".$column." + cName
 	return &schemaspec.Ref{
 		V: v,
 	}
 }
+
+func schemaRef(n string) *schemaspec.Ref {
+	return &schemaspec.Ref{V: "$schema." + n}
+}
diff --git a/sql/sqlspec/sqlspec.go b/sql/sqlspec/sqlspec.go
index 39c86e0b..957e2fbd 100644
--- a/sql/sqlspec/sqlspec.go
+++ b/sql/sqlspec/sqlspec.go
@@ -15,12 +15,12 @@ type (
 
 	// Table holds a specification for an SQL table.
 	Table struct {
-		Name        string        `spec:",name"`
-		SchemaName  string        `spec:"schema"`
-		Columns     []*Column     `spec:"column"`
-		PrimaryKey  *PrimaryKey   `spec:"primary_key"`
-		ForeignKeys []*ForeignKey `spec:"foreign_key"`
-		Indexes     []*Index      `spec:"index"`
+		Name        string          `spec:",name"`
+		Schema      *schemaspec.Ref `spec:"schema"`
+		Columns     []*Column       `spec:"column"`
+		PrimaryKey  *PrimaryKey     `spec:"primary_key"`
+		ForeignKeys []*ForeignKey   `spec:"foreign_key"`
+		Indexes     []*Index        `spec:"index"`
 		schemaspec.DefaultExtension
 	}
 


Test patch:
diff --git a/cmd/internal/base/diff_test.go b/cmd/internal/base/diff_test.go
index ab96c638..22327fbc 100644
--- a/cmd/internal/base/diff_test.go
+++ b/cmd/internal/base/diff_test.go
@@ -16,7 +16,7 @@ func TestDiffPrint(t *testing.T) {
 schema "default" {
 }
 table "users" {
-	schema = "default"
+	schema = schema.default
 	column "id" {
 		type = "int"
 	}
@@ -26,7 +26,7 @@ table "users" {
 schema "default" {
 }
 table "users" {
-	schema = "default"
+	schema = schema.default
 	column "id" {
 		type = "string"
 	}
@@ -36,7 +36,7 @@ table "users" {
 }
 
 table "groups" {
-	schema = "default"
+	schema = schema.default
 	column "id" {
 		type = "int"
 	}
diff --git a/internal/integration/hclsqlspec/hclsqlspec_test.go b/internal/integration/hclsqlspec/hclsqlspec_test.go
index acb502d1..7f0334d6 100644
--- a/internal/integration/hclsqlspec/hclsqlspec_test.go
+++ b/internal/integration/hclsqlspec/hclsqlspec_test.go
@@ -17,7 +17,7 @@ schema "hi" {
 }
 
 table "users" {
-	schema = "hi"
+	schema = schema.hi
 	
 	column "id" {
 		type = "uint"
@@ -60,7 +60,7 @@ table "users" {
 }
 
 table "accounts" {
-	schema = "hi"
+	schema = schema.hi
 	
 	column "id" {
 		type = "uint"
@@ -110,8 +110,8 @@ table "accounts" {
 		},
 		Tables: []*sqlspec.Table{
 			{
-				Name:       "users",
-				SchemaName: "hi",
+				Name:   "users",
+				Schema: &schemaspec.Ref{V: "$schema.hi"},
 				Columns: []*sqlspec.Column{
 					{
 						Name:    "id",
@@ -186,8 +186,8 @@ table "accounts" {
 				},
 			},
 			{
-				Name:       "accounts",
-				SchemaName: "hi",
+				Name:   "accounts",
+				Schema: &schemaspec.Ref{V: "$schema.hi"},
 				Columns: []*sqlspec.Column{
 					{
 						Name:    "id",
@@ -292,7 +292,7 @@ schema "hi" {
 }
 
 table "users" {
-	schema = "hi"
+	schema = schema.hi
 	column "id" {
 		type = "uint"
 		null = false
@@ -301,7 +301,7 @@ table "users" {
 }
 
 table "accounts" {
-	schema = "hi"
+	schema = schema.hi
 	column "id" {
 		type = "string"
 	}
diff --git a/internal/integration/mysql_test.go b/internal/integration/mysql_test.go
index 1152575a..acb6adcb 100644
--- a/internal/integration/mysql_test.go
+++ b/internal/integration/mysql_test.go
@@ -456,7 +456,7 @@ func TestMySQL_HCL(t *testing.T) {
 schema "test" {
 }
 table "users" {
-	schema = "test"
+	schema = schema.test
 	column "id" {
 		type = "int"
 	}
@@ -465,7 +465,7 @@ table "users" {
 	}
 }
 table "posts" {
-	schema = "test"
+	schema = schema.test
 	column "id" {
 		type = "int"
 	}
@@ -499,7 +499,7 @@ func TestMySQL_CLI(t *testing.T) {
 			schema "test" {
 			}
 			table "users" {
-				schema = "public"
+				schema = schema.test
 				column "id" {
 					type = "int"
 				}
diff --git a/internal/integration/postgres_test.go b/internal/integration/postgres_test.go
index 5cc0a013..24dc8ee9 100644
--- a/internal/integration/postgres_test.go
+++ b/internal/integration/postgres_test.go
@@ -373,7 +373,7 @@ func TestPostgres_HCL(t *testing.T) {
 schema "public" {
 }
 table "users" {
-	schema = "public"
+	schema = schema.public
 	column "id" {
 		type = "int"
 	}
@@ -382,7 +382,7 @@ table "users" {
 	}
 }
 table "posts" {
-	schema = "public"
+	schema = schema.public
 	column "id" {
 		type = "int"
 	}
@@ -419,7 +419,7 @@ func TestPostgres_CLI(t *testing.T) {
 			schema "public" {
 			}
 			table "users" {
-				schema = "public"
+				schema = schema.public
 				column "id" {
 					type = "int"
 				}
diff --git a/internal/integration/sqlite_test.go b/internal/integration/sqlite_test.go
index f9cabdc9..410f0553 100644
--- a/internal/integration/sqlite_test.go
+++ b/internal/integration/sqlite_test.go
@@ -358,7 +358,7 @@ func TestSQLite_HCL(t *testing.T) {
 schema "public" {
 }
 table "users" {
-	schema = "public"
+	schema = schema.public
 	column "id" {
 		type = "int"
 	}
@@ -367,7 +367,7 @@ table "users" {
 	}
 }
 table "posts" {
-	schema = "public"
+	schema = schema.public
 	column "id" {
 		type = "int"
 	}
diff --git a/sql/internal/specutil/convert_test.go b/sql/internal/specutil/convert_test.go
index f0d8163e..24865956 100644
--- a/sql/internal/specutil/convert_test.go
+++ b/sql/internal/specutil/convert_test.go
@@ -36,7 +36,7 @@ func TestFromSpec_SchemaName(t *testing.T) {
 	})
 	require.NoError(t, err)
 	require.Equal(t, sc.Name, s.Name)
-	require.Equal(t, sc.Name, ta[0].SchemaName)
+	require.Equal(t, "$schema." + sc.Name, ta[0].Schema.V)
 }
 
 func TestFromForeignKey(t *testing.T) {
diff --git a/sql/mysql/sqlspec_test.go b/sql/mysql/sqlspec_test.go
index 668e3c61..a33eecb8 100644
--- a/sql/mysql/sqlspec_test.go
+++ b/sql/mysql/sqlspec_test.go
@@ -298,7 +298,7 @@ func hcl(c *sqlspec.Column) []byte {
 schema "default" {
 }
 table "table" {
-	schema = "default"
+	schema = schema.default
 	%s
 }
 `
diff --git a/sql/postgres/sqlspec_test.go b/sql/postgres/sqlspec_test.go
index b46cb8d6..20203dd4 100644
--- a/sql/postgres/sqlspec_test.go
+++ b/sql/postgres/sqlspec_test.go
@@ -305,7 +305,7 @@ func hcl(c *sqlspec.Column) []byte {
 schema "default" {
 }
 table "table" {
-	schema = "default"
+	schema = schema.default
 	%s
 }
 `
diff --git a/sql/sqlite/sqlspec_test.go b/sql/sqlite/sqlspec_test.go
index 2df05b37..5aed31e0 100644
--- a/sql/sqlite/sqlspec_test.go
+++ b/sql/sqlite/sqlspec_test.go
@@ -290,7 +290,7 @@ func hcl(t *testing.T, c *sqlspec.Column) []byte {
 schema "default" {
 }
 table "table" {
-	schema = "default"
+	schema = schema.default
 	%s
 }
 `