mirror of
https://github.com/dolthub/dolt.git
synced 2026-04-21 02:57:46 -05:00
Added RenameTable.go and tests. Moved a test utility method into the dtestutils package.
This commit is contained in:
@@ -49,7 +49,7 @@ func Mv(commandStr string, args []string, dEnv *env.DoltEnv) int {
|
||||
verr = errhand.BuildDError("Data already exists in '%s'. Use -f to overwrite.", new).Build()
|
||||
} else {
|
||||
working = working.PutTable(context.Background(), dEnv.DoltDB, new, tbl)
|
||||
working, err := working.RemoveTables(context.TODO(), []string{old})
|
||||
working, err := working.RemoveTables(context.TODO(), old)
|
||||
|
||||
if err != nil {
|
||||
verr = errhand.BuildDError("Unable to remove '%s'", old).Build()
|
||||
|
||||
@@ -47,7 +47,7 @@ func Rm(commandStr string, args []string, dEnv *env.DoltEnv) int {
|
||||
}
|
||||
|
||||
func removeTables(dEnv *env.DoltEnv, tables []string, working *doltdb.RootValue) errhand.VerboseError {
|
||||
working, err := working.RemoveTables(context.TODO(), tables)
|
||||
working, err := working.RemoveTables(context.TODO(), tables...)
|
||||
|
||||
if err != nil {
|
||||
return errhand.BuildDError("Unable to remove table(s)").AddCause(err).Build()
|
||||
|
||||
@@ -13,6 +13,7 @@ var ErrFoundHashNotACommit = errors.New("the value retrieved for this hash is no
|
||||
var ErrHashNotFound = errors.New("could not find a value for this hash")
|
||||
var ErrBranchNotFound = errors.New("branch not found")
|
||||
var ErrTableNotFound = errors.New("table not found")
|
||||
var ErrTableExists = errors.New("table already exists")
|
||||
var ErrAlreadyOnBranch = errors.New("Already on branch")
|
||||
|
||||
var ErrNomsIO = errors.New("error reading from or writing to noms")
|
||||
|
||||
@@ -231,7 +231,7 @@ func (root *RootValue) UpdateTablesFromOther(ctx context.Context, tblNames []str
|
||||
return newRootValue(root.vrw, rootValSt)
|
||||
}
|
||||
|
||||
func (root *RootValue) RemoveTables(ctx context.Context, tables []string) (*RootValue, error) {
|
||||
func (root *RootValue) RemoveTables(ctx context.Context, tables ...string) (*RootValue, error) {
|
||||
tableMap := root.valueSt.Get(tablesKey).(types.Map)
|
||||
me := tableMap.Edit()
|
||||
for _, tbl := range tables {
|
||||
|
||||
@@ -26,7 +26,8 @@ const (
|
||||
|
||||
var tableNameRegex, _ = regexp.Compile(TableNameRegexStr)
|
||||
|
||||
// IsValidTableName returns true if the name matches the regular expression `[0-9a-z]+[-_0-9a-z]*[0-9a-z]+$`
|
||||
// IsValidTableName returns true if the name matches the regular expression TableNameRegexStr.
|
||||
// Table names must be composed of 1 or more letters and non-initial numerals, as well as the characters _ and -
|
||||
func IsValidTableName(name string) bool {
|
||||
return tableNameRegex.MatchString(name)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
package dtestutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/env"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/row"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/schema"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/table"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/table/typed/noms"
|
||||
"github.com/stretchr/testify/require"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// CreateSchema returns a schema from the columns given, panicking on any errors.
|
||||
@@ -47,3 +54,24 @@ var FloatComparer = cmp.Comparer(func(x, y types.Float) bool {
|
||||
return math.Abs(float64(x)-float64(y)) < .001
|
||||
})
|
||||
|
||||
// CreateTestTable creates a new test table with the name, schema, and rows given.
|
||||
func CreateTestTable(t *testing.T, dEnv *env.DoltEnv, tableName string, sch schema.Schema, rs ...row.Row) {
|
||||
imt := table.NewInMemTable(sch)
|
||||
|
||||
for _, r := range rs {
|
||||
imt.AppendRow(r)
|
||||
}
|
||||
|
||||
rd := table.NewInMemTableReader(imt)
|
||||
wr := noms.NewNomsMapCreator(context.Background(), dEnv.DoltDB.ValueReadWriter(), sch)
|
||||
|
||||
_, _, err := table.PipeRows(context.Background(), rd, wr, false)
|
||||
rd.Close(context.Background())
|
||||
wr.Close(context.Background())
|
||||
|
||||
require.Nil(t, err, "Failed to seed initial data")
|
||||
|
||||
err = dEnv.PutTableToWorking(context.Background(), *wr.GetMap(), wr.GetSchema(), tableName)
|
||||
require.Nil(t, err, "Unable to put initial value of table in in-mem noms db")
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ func MergeCommits(ctx context.Context, ddb *doltdb.DoltDB, cm1, cm2 *doltdb.Comm
|
||||
root = root.PutTable(ctx, ddb, tblName, mergedTable)
|
||||
} else if root.HasTable(ctx, tblName) {
|
||||
tblToStats[tblName] = &merge.MergeStats{Operation: merge.TableRemoved}
|
||||
root, err = root.RemoveTables(ctx, []string{tblName})
|
||||
root, err = root.RemoveTables(ctx, tblName)
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ func checkoutTables(ctx context.Context, dEnv *env.DoltEnv, roots map[RootType]*
|
||||
|
||||
if len(unknown) > 0 {
|
||||
var err error
|
||||
currRoot, err = currRoot.RemoveTables(ctx, unknown)
|
||||
currRoot, err = currRoot.RemoveTables(ctx, unknown...)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -272,3 +272,5 @@ func createEnvWithSeedData(t *testing.T) *env.DoltEnv {
|
||||
|
||||
return dEnv
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package alterschema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/doltdb"
|
||||
)
|
||||
|
||||
// RenameTable renames a table with in a RootValue and returns the updated root.
|
||||
func RenameTable(ctx context.Context, doltDb *doltdb.DoltDB , root *doltdb.RootValue, oldName, newName string) (*doltdb.RootValue, error) {
|
||||
if newName == oldName {
|
||||
return root, nil
|
||||
} else if root == nil {
|
||||
panic("invalid parameters")
|
||||
}
|
||||
|
||||
tbl, ok := root.GetTable(ctx, oldName)
|
||||
if !ok {
|
||||
return nil, doltdb.ErrTableNotFound
|
||||
}
|
||||
|
||||
if root.HasTable(ctx, newName) {
|
||||
return nil, doltdb.ErrTableExists
|
||||
}
|
||||
|
||||
var err error
|
||||
if root, err = root.RemoveTables(ctx, oldName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return root.PutTable(ctx, doltDb, newName, tbl), nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package alterschema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/doltdb"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/dtestutils"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/row"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/schema"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRenameTable(t *testing.T) {
|
||||
otherTable := "other"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tableName string
|
||||
newTableName string
|
||||
expectedSchema schema.Schema
|
||||
expectedRows []row.Row
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "rename table",
|
||||
tableName: "people",
|
||||
newTableName: "newPeople",
|
||||
expectedSchema: dtestutils.TypedSchema,
|
||||
expectedRows: dtestutils.TypedRows,
|
||||
},
|
||||
{
|
||||
name: "table not found",
|
||||
tableName: "notFound",
|
||||
newTableName: "newNotfound",
|
||||
expectedErr: doltdb.ErrTableNotFound.Error(),
|
||||
},
|
||||
{
|
||||
name: "name already in use",
|
||||
tableName: "people",
|
||||
newTableName: otherTable,
|
||||
expectedErr: doltdb.ErrTableExists.Error(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dEnv := createEnvWithSeedData(t)
|
||||
ctx := context.Background()
|
||||
|
||||
dtestutils.CreateTestTable(t, dEnv, otherTable, dtestutils.UntypedSchema)
|
||||
|
||||
root, err := dEnv.WorkingRoot(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
updatedRoot, err := RenameTable(ctx, dEnv.DoltDB, root, tt.tableName, tt.newTableName)
|
||||
if len(tt.expectedErr) > 0 {
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.expectedErr)
|
||||
return
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.False(t, updatedRoot.HasTable(ctx, tt.tableName))
|
||||
newTable, ok := updatedRoot.GetTable(ctx, tt.newTableName)
|
||||
require.True(t, ok)
|
||||
|
||||
require.Equal(t, tt.expectedSchema, newTable.GetSchema(ctx))
|
||||
|
||||
rowData := newTable.GetRowData(ctx)
|
||||
var foundRows []row.Row
|
||||
rowData.Iter(ctx, func(key, value types.Value) (stop bool) {
|
||||
foundRows = append(foundRows, row.FromNoms(tt.expectedSchema, key.(types.Tuple), value.(types.Tuple)))
|
||||
return false
|
||||
})
|
||||
|
||||
assert.Equal(t, tt.expectedRows, foundRows)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -960,8 +960,8 @@ func TestCaseSensitivity(t *testing.T) {
|
||||
tableName: "tableName",
|
||||
tableSchema: newSchema("test", types.StringKind),
|
||||
additionalSetup: func(t *testing.T, dEnv *env.DoltEnv) {
|
||||
createTestTable(dEnv, t, "TABLENAME", newSchema("test", types.StringKind))
|
||||
createTestTable(dEnv, t, "tablename", newSchema("test", types.StringKind))
|
||||
dtestutils.CreateTestTable(t, dEnv, "TABLENAME", newSchema("test", types.StringKind))
|
||||
dtestutils.CreateTestTable(t, dEnv, "tablename", newSchema("test", types.StringKind))
|
||||
},
|
||||
initialRows: rs(newRow(types.String("1"))),
|
||||
query: "select test from tableName",
|
||||
@@ -973,7 +973,7 @@ func TestCaseSensitivity(t *testing.T) {
|
||||
tableName: "tableName",
|
||||
tableSchema: newSchema("test", types.StringKind),
|
||||
additionalSetup: func(t *testing.T, dEnv *env.DoltEnv) {
|
||||
createTestTable(dEnv, t, "TABLENAME", newSchema("test", types.StringKind))
|
||||
dtestutils.CreateTestTable(t, dEnv, "TABLENAME", newSchema("test", types.StringKind))
|
||||
},
|
||||
initialRows: rs(newRow(types.String("1"))),
|
||||
query: "select test from tablename",
|
||||
@@ -984,7 +984,7 @@ func TestCaseSensitivity(t *testing.T) {
|
||||
tableName: "tableName",
|
||||
tableSchema: newSchema("test", types.StringKind),
|
||||
additionalSetup: func(t *testing.T, dEnv *env.DoltEnv) {
|
||||
createTestTable(dEnv, t, "other", newSchema("othercol", types.StringKind))
|
||||
dtestutils.CreateTestTable(t, dEnv, "other", newSchema("othercol", types.StringKind))
|
||||
},
|
||||
initialRows: rs(newRow(types.String("1"))),
|
||||
query: "select other.test from tablename as other, other",
|
||||
@@ -1133,7 +1133,7 @@ func TestCaseSensitivity(t *testing.T) {
|
||||
createTestDatabase(dEnv, t)
|
||||
|
||||
if tt.tableName != "" {
|
||||
createTestTable(dEnv, t, tt.tableName, tt.tableSchema, tt.initialRows...)
|
||||
dtestutils.CreateTestTable(t, dEnv, tt.tableName, tt.tableSchema, tt.initialRows)
|
||||
}
|
||||
if tt.additionalSetup != nil {
|
||||
tt.additionalSetup(t, dEnv)
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
"github.com/google/uuid"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/dtestutils"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/env"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/row"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/schema"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/table"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/table/typed/noms"
|
||||
"github.com/liquidata-inc/ld/dolt/go/libraries/doltcore/table/untyped"
|
||||
"github.com/stretchr/testify/require"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -270,29 +267,9 @@ func newResultSetRow(colVals ...types.Value) row.Row {
|
||||
return row.New(sch, taggedVals)
|
||||
}
|
||||
|
||||
func createTestTable(dEnv *env.DoltEnv, t *testing.T, tableName string, sch schema.Schema, rs ...row.Row) {
|
||||
imt := table.NewInMemTable(sch)
|
||||
|
||||
for _, r := range rs {
|
||||
imt.AppendRow(r)
|
||||
}
|
||||
|
||||
rd := table.NewInMemTableReader(imt)
|
||||
wr := noms.NewNomsMapCreator(context.Background(), dEnv.DoltDB.ValueReadWriter(), sch)
|
||||
|
||||
_, _, err := table.PipeRows(context.Background(), rd, wr, false)
|
||||
rd.Close(context.Background())
|
||||
wr.Close(context.Background())
|
||||
|
||||
require.Nil(t, err, "Failed to seed initial data")
|
||||
|
||||
err = dEnv.PutTableToWorking(context.Background(), *wr.GetMap(), wr.GetSchema(), tableName)
|
||||
require.Nil(t, err, "Unable to put initial value of table in in mem noms db")
|
||||
}
|
||||
|
||||
// Creates a test database with the test data set in it
|
||||
func createTestDatabase(dEnv *env.DoltEnv, t *testing.T) {
|
||||
createTestTable(dEnv, t, peopleTableName, peopleTestSchema, allPeopleRows...)
|
||||
createTestTable(dEnv, t, episodesTableName, episodesTestSchema, allEpsRows...)
|
||||
createTestTable(dEnv, t, appearancesTableName, appearancesTestSchema, allAppsRows...)
|
||||
dtestutils.CreateTestTable(t, dEnv, peopleTableName, peopleTestSchema, allPeopleRows)
|
||||
dtestutils.CreateTestTable(t, dEnv, episodesTableName, episodesTestSchema, allEpsRows)
|
||||
dtestutils.CreateTestTable(t, dEnv, appearancesTableName, appearancesTestSchema, allAppsRows)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user