Skip to content

Commit

Permalink
feat(embedded/sql): table renaming
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>
  • Loading branch information
jeroiraz committed Oct 18, 2023
1 parent 09841ef commit 4b1d3b1
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 305 deletions.
25 changes: 24 additions & 1 deletion embedded/sql/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,36 @@ func (t *Table) newColumn(spec *ColSpec) (*Column, error) {
return col, nil
}

func (ctlg *Catalog) renameTable(oldName, newName string) (*Table, error) {
if oldName == newName {
return nil, fmt.Errorf("%w (%s)", ErrSameOldAndNewNames, oldName)
}

t, err := ctlg.GetTableByName(oldName)
if err != nil {
return nil, err
}

_, err = ctlg.GetTableByName(newName)
if err == nil {
return nil, fmt.Errorf("%w (%s)", ErrTableAlreadyExists, newName)
}

t.name = newName

delete(ctlg.tablesByName, oldName)
ctlg.tablesByName[newName] = t

return t, nil
}

func (t *Table) renameColumn(oldName, newName string) (*Column, error) {
if newName == revCol {
return nil, fmt.Errorf("%w(%s)", ErrReservedWord, revCol)
}

if oldName == newName {
return nil, fmt.Errorf("%w (%s)", ErrSameOldAndNewColumnName, oldName)
return nil, fmt.Errorf("%w (%s)", ErrSameOldAndNewNames, oldName)
}

col, exists := t.colsByName[oldName]
Expand Down
2 changes: 1 addition & 1 deletion embedded/sql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var ErrTableDoesNotExist = errors.New("table does not exist")
var ErrColumnDoesNotExist = errors.New("column does not exist")
var ErrColumnAlreadyExists = errors.New("column already exists")
var ErrCantDropIndexedColumn = errors.New("can not drop indexed column")
var ErrSameOldAndNewColumnName = errors.New("same old and new column names")
var ErrSameOldAndNewNames = errors.New("same old and new names")
var ErrColumnNotIndexed = errors.New("column is not indexed")
var ErrFunctionDoesNotExist = errors.New("function does not exist")
var ErrLimitedKeyType = errors.New("indexed key of unsupported type or exceeded length")
Expand Down
25 changes: 20 additions & 5 deletions embedded/sql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ func TestAddColumn(t *testing.T) {
})
}

func TestRenameColumn(t *testing.T) {
func TestRenaming(t *testing.T) {
dir := t.TempDir()

t.Run("create-store", func(t *testing.T) {
Expand All @@ -1108,17 +1108,23 @@ func TestRenameColumn(t *testing.T) {
engine, err := NewEngine(st, DefaultOptions().WithPrefix(sqlPrefix))
require.NoError(t, err)

_, _, err = engine.Exec(context.Background(), nil, "CREATE TABLE table1 (id INTEGER AUTO_INCREMENT, name VARCHAR[50], PRIMARY KEY id)", nil)
_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table1 RENAME TO table11", nil)
require.ErrorIs(t, err, ErrTableDoesNotExist)

_, _, err = engine.Exec(context.Background(), nil, "CREATE TABLE table11 (id INTEGER AUTO_INCREMENT, name VARCHAR[50], PRIMARY KEY id)", nil)
require.NoError(t, err)

_, _, err = engine.Exec(context.Background(), nil, "CREATE INDEX ON table1(name)", nil)
_, _, err = engine.Exec(context.Background(), nil, "CREATE INDEX ON table11(name)", nil)
require.NoError(t, err)

_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table11 RENAME TO table1", nil)
require.NoError(t, err)

_, _, err = engine.Exec(context.Background(), nil, "INSERT INTO table1(name) VALUES('John'), ('Sylvia'), ('Robocop') ", nil)
require.NoError(t, err)

_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table1 RENAME COLUMN name TO name", nil)
require.ErrorIs(t, err, ErrSameOldAndNewColumnName)
require.ErrorIs(t, err, ErrSameOldAndNewNames)

_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table1 RENAME COLUMN name TO id", nil)
require.ErrorIs(t, err, ErrColumnAlreadyExists)
Expand Down Expand Up @@ -1168,7 +1174,13 @@ func TestRenameColumn(t *testing.T) {
engine, err := NewEngine(st, DefaultOptions().WithPrefix(sqlPrefix))
require.NoError(t, err)

res, err := engine.Query(context.Background(), nil, "SELECT id, surname FROM table1 ORDER BY surname", nil)
_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table1 RENAME TO table1", nil)
require.ErrorIs(t, err, ErrSameOldAndNewNames)

_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table1 RENAME TO table11", nil)
require.NoError(t, err)

res, err := engine.Query(context.Background(), nil, "SELECT id, surname FROM table11 ORDER BY surname", nil)
require.NoError(t, err)

row, err := res.Read(context.Background())
Expand Down Expand Up @@ -4188,6 +4200,9 @@ func TestJoins(t *testing.T) {
_, _, err = engine.Exec(context.Background(), nil, "CREATE TABLE table3 (id INTEGER, age INTEGER, PRIMARY KEY id)", nil)
require.NoError(t, err)

_, _, err = engine.Exec(context.Background(), nil, "ALTER TABLE table1 RENAME TO table3", nil)
require.ErrorIs(t, err, ErrTableAlreadyExists)

rowCount := 10

for i := 0; i < rowCount; i++ {
Expand Down
5 changes: 5 additions & 0 deletions embedded/sql/sql_grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ ddlstmt:
{
$$ = &AddColumnStmt{table: $3, colSpec: $6}
}
|
ALTER TABLE IDENTIFIER RENAME TO IDENTIFIER
{
$$ = &RenameTableStmt{oldName: $3, newName: $6}
}
|
ALTER TABLE IDENTIFIER RENAME COLUMN IDENTIFIER TO IDENTIFIER
{
Expand Down
Loading

0 comments on commit 4b1d3b1

Please sign in to comment.