Repository: ariga/atlas
Pull request #365 (2022-01-03 15:17:34)

sql/internal/sqlx: reject mismatched schema or table names


Patch:
diff --git a/sql/internal/sqlx/diff.go b/sql/internal/sqlx/diff.go
index 550d78c8..85895839 100644
--- a/sql/internal/sqlx/diff.go
+++ b/sql/internal/sqlx/diff.go
@@ -93,6 +93,9 @@ func (d *Diff) RealmDiff(from, to *schema.Realm) ([]schema.Change, error) {
 // SchemaDiff implements the schema.Differ interface and returns a list of
 // changes that need to be applied in order to move from one state to the other.
 func (d *Diff) SchemaDiff(from, to *schema.Schema) ([]schema.Change, error) {
+	if from.Name != to.Name {
+		return nil, fmt.Errorf("mismatched schema names: %q != %q", from.Name, to.Name)
+	}
 	var changes []schema.Change
 	// Drop or modify attributes (collations, checks, etc).
 	if change := d.SchemaAttrDiff(from, to); len(change) > 0 {
@@ -131,11 +134,13 @@ func (d *Diff) SchemaDiff(from, to *schema.Schema) ([]schema.Change, error) {
 // TableDiff implements the schema.TableDiffer interface and returns a list of
 // changes that need to be applied in order to move from one state to the other.
 func (d *Diff) TableDiff(from, to *schema.Table) ([]schema.Change, error) {
+	if from.Name != to.Name {
+		return nil, fmt.Errorf("mismatched table names: %q != %q", from.Name, to.Name)
+	}
 	// Normalizing tables before starting the diff process.
 	if n, ok := d.DiffDriver.(Normalizer); ok {
 		n.Normalize(from, to)
 	}
-
 	var changes []schema.Change
 	if from.Name != to.Name {
 		return nil, fmt.Errorf("mismatched table names: %q != %q", from.Name, to.Name)


Test patch:
diff --git a/internal/integration/sqlite_test.go b/internal/integration/sqlite_test.go
index 0eda8f0e..011be511 100644
--- a/internal/integration/sqlite_test.go
+++ b/internal/integration/sqlite_test.go
@@ -359,10 +359,10 @@ func TestSQLite_ForeignKey(t *testing.T) {
 
 func TestSQLite_HCL(t *testing.T) {
 	full := `
-schema "public" {
+schema "main" {
 }
 table "users" {
-	schema = schema.public
+	schema = schema.main
 	column "id" {
 		type = int
 	}
@@ -371,7 +371,7 @@ table "users" {
 	}
 }
 table "posts" {
-	schema = schema.public
+	schema = schema.main
 	column "id" {
 		type = int
 	}
@@ -392,7 +392,7 @@ table "posts" {
 }
 `
 	empty := `
-schema "public" {
+schema "main" {
 }
 `
 	liteRun(t, func(t *liteTest) {
diff --git a/sql/mysql/diff_test.go b/sql/mysql/diff_test.go
index f6d021f2..ae6f427d 100644
--- a/sql/mysql/diff_test.go
+++ b/sql/mysql/diff_test.go
@@ -21,6 +21,12 @@ func TestDiff_TableDiff(t *testing.T) {
 		wantErr     bool
 	}
 	tests := []testcase{
+		{
+			name:    "mismatched names",
+			from:    &schema.Table{Name: "users"},
+			to:      &schema.Table{Name: "pets"},
+			wantErr: true,
+		},
 		{
 			name: "no changes",
 			from: &schema.Table{Name: "users", Schema: &schema.Schema{Name: "public"}},
@@ -286,6 +292,11 @@ func TestDiff_SchemaDiff(t *testing.T) {
 	mock{m}.version("8.0.19")
 	drv, err := Open(db)
 	require.NoError(t, err)
+
+	changes, err := drv.SchemaDiff(&schema.Schema{Name: "public"}, &schema.Schema{Name: "test"})
+	require.Error(t, err)
+	require.Nil(t, changes)
+
 	from := &schema.Schema{
 		Realm: &schema.Realm{
 			Attrs: []schema.Attr{
@@ -316,7 +327,7 @@ func TestDiff_SchemaDiff(t *testing.T) {
 	}
 	from.Tables[0].Schema = from
 	from.Tables[1].Schema = from
-	changes, err := drv.SchemaDiff(from, to)
+	changes, err = drv.SchemaDiff(from, to)
 	require.NoError(t, err)
 	require.EqualValues(t, []schema.Change{
 		&schema.ModifySchema{Changes: []schema.Change{&schema.ModifyAttr{From: from.Attrs[0], To: to.Attrs[0]}}},