Skip to content

Commit

Permalink
Merge pull request #8643 from dolthub/zachmu/enginetest10
Browse files Browse the repository at this point in the history
Support for schema names in table renames
  • Loading branch information
zachmu authored Dec 7, 2024
2 parents 161a217 + f783a21 commit aba8d2a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.18.2-0.20241206195637-151301aa7662
github.com/dolthub/go-mysql-server v0.18.2-0.20241206235354-d3cdbb6be4b4
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662 h1:aC17hZD6iwzBwwfO5M+3oBT5E5gGRiQPdn+vzpDXqIA=
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.18.2-0.20241206195637-151301aa7662 h1:tqpaQ8F4wamAf7cpGkRzxEIu6bzHAXKOFnctAYX3oyo=
github.com/dolthub/go-mysql-server v0.18.2-0.20241206195637-151301aa7662/go.mod h1:5j8LgJ+mRFpm4TG9Dtp7XQRTq13GlwpkPGPicMx2tl8=
github.com/dolthub/go-mysql-server v0.18.2-0.20241206235354-d3cdbb6be4b4 h1:2T/vff9AhzGoaqZAgl7bNYFrqeZ9ft38t7tKXTM9NY0=
github.com/dolthub/go-mysql-server v0.18.2-0.20241206235354-d3cdbb6be4b4/go.mod h1:5j8LgJ+mRFpm4TG9Dtp7XQRTq13GlwpkPGPicMx2tl8=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=
Expand Down
5 changes: 2 additions & 3 deletions go/libraries/doltcore/sqle/alterschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ import (
)

// renameTable renames a table with in a RootValue and returns the updated root.
func renameTable(ctx context.Context, root doltdb.RootValue, oldName, newName string) (doltdb.RootValue, error) {
func renameTable(ctx context.Context, root doltdb.RootValue, oldName, newName doltdb.TableName) (doltdb.RootValue, error) {
if newName == oldName {
return root, nil
} else if root == nil {
panic("invalid parameters")
}

// TODO: schema name
return root.RenameTable(ctx, doltdb.TableName{Name: oldName}, doltdb.TableName{Name: newName})
return root.RenameTable(ctx, oldName, newName)
}

// Nullable represents whether a column can have a null value.
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/alterschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestRenameTable(t *testing.T) {
require.NoError(t, err)
beforeSch := schemas[doltdb.TableName{Name: tt.oldName}]

updatedRoot, err := renameTable(ctx, root, tt.oldName, tt.newName)
updatedRoot, err := renameTable(ctx, root, doltdb.TableName{Name: tt.oldName}, doltdb.TableName{Name: tt.newName})
if len(tt.expectedErr) > 0 {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedErr)
Expand Down
17 changes: 14 additions & 3 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,12 +1726,23 @@ func (db Database) RenameTable(ctx *sql.Context, oldName, newName string) error
return ErrInvalidTableName.New(newName)
}

if _, ok, _ := db.GetTableInsensitive(ctx, newName); ok {
return sql.ErrTableAlreadyExists.New(newName)
oldNameWithSchema, _, exists, err := resolve.Table(ctx, root, oldName)
if err != nil {
return err
}

newRoot, err := renameTable(ctx, root, oldName, newName)
if !exists {
return sql.ErrTableNotFound.New(oldName)
}

// TODO: we have no way to rename a table to a different schema, need to change the GMS interface for that
newNameWithSchema := doltdb.TableName{Schema: oldNameWithSchema.Schema, Name: newName}
_, exists, err = root.ResolveTableName(ctx, newNameWithSchema)
if exists {
return sql.ErrTableAlreadyExists.New(newName)
}

newRoot, err := renameTable(ctx, root, oldNameWithSchema, newNameWithSchema)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go/libraries/doltcore/sqle/enginetest/dolt_engine_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ func RunDoltRevisionDbScriptsTest(t *testing.T, h DoltEnginetestHarness) {
require.NoError(t, err)
defer e.Close()
ctx := h.NewContext()
ctx.SetCurrentDatabase("mydb")

setupScripts := []setup.SetupScript{
{"create table t01 (pk int primary key, c1 int)"},
Expand Down
16 changes: 9 additions & 7 deletions go/libraries/doltcore/sqle/enginetest/dolt_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ func (d *DoltHarness) NewEngine(t *testing.T) (enginetest.QueryEngine, error) {
if err != nil {
return nil, err
}

// Get a fresh session after running setup scripts, since some setup scripts can change the session state
d.session, err = dsess.NewDoltSession(enginetest.NewBaseSession(), d.provider, d.multiRepoEnv.Config(), d.branchControl, d.statsPro, writer.NewWriteSession)
require.NoError(t, err)
}

if d.configureStats {
Expand Down Expand Up @@ -304,16 +308,14 @@ func (d *DoltHarness) NewEngine(t *testing.T) (enginetest.QueryEngine, error) {
d.engine.Analyzer.Catalog.MySQLDb.AddRootAccount()
d.engine.Analyzer.Catalog.StatsProvider = statspro.NewProvider(d.provider.(*sqle.DoltDatabaseProvider), statsnoms.NewNomsStatsFactory(d.multiRepoEnv.RemoteDialProvider()))

// Get a fresh session if we are reusing the engine
if !initializeEngine {
var err error
d.session, err = dsess.NewDoltSession(enginetest.NewBaseSession(), d.provider, d.multiRepoEnv.Config(), d.branchControl, d.statsPro, writer.NewWriteSession)
require.NoError(t, err)
}

var err error
ctx := enginetest.NewContext(d)
e, err := enginetest.RunSetupScripts(ctx, d.engine, d.resetScripts(), d.SupportsNativeIndexCreation())

// Get a fresh session after running setup scripts, since some setup scripts can change the session state
d.session, err = dsess.NewDoltSession(enginetest.NewBaseSession(), d.provider, d.multiRepoEnv.Config(), d.branchControl, d.statsPro, writer.NewWriteSession)
require.NoError(t, err)

return e, err
}

Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/sqle/enginetest/dolt_queries_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ var MergeScripts = []queries.ScriptTest{
Expected: []sql.Row{{doltCommit, 1, 0, "merge successful"}},
},
{
Query: "INSERT INTO t VALUES (NULL,5),(6,6),(NULL,7);",
Query: "INSERT INTO t(c0) VALUES (5),(6),(7);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 3, InsertID: 5}}},
},
{
Expand Down Expand Up @@ -1698,7 +1698,7 @@ var MergeScripts = []queries.ScriptTest{
Expected: []sql.Row{{doltCommit, 0, 0, "merge successful"}},
},
{
Query: "INSERT INTO t VALUES (NULL,6),(7,7),(NULL,8);",
Query: "INSERT INTO t(c0) VALUES (6),(7),(8);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 3, InsertID: 6}}},
},
{
Expand Down

0 comments on commit aba8d2a

Please sign in to comment.