mirror of
https://github.com/chaisql/chai.git
synced 2025-10-14 19:57:25 +08:00

Previously, expressions and params were evaluated during the planning phase. This change builds the query plan without evaluating params and expressions which are then evaluated only during the execution phase.
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package statement_test
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/genjidb/genji"
|
|
"github.com/genjidb/genji/document"
|
|
errs "github.com/genjidb/genji/errors"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAlterTable(t *testing.T) {
|
|
db, err := genji.Open(":memory:")
|
|
require.NoError(t, err)
|
|
defer db.Close()
|
|
|
|
err = db.Exec("CREATE TABLE foo")
|
|
require.NoError(t, err)
|
|
|
|
// Insert some data into foo
|
|
err = db.Exec(`INSERT INTO foo VALUES {name: "John Doe", age: 99}`)
|
|
require.NoError(t, err)
|
|
|
|
// Renaming the table to the same name should fail.
|
|
err = db.Exec("ALTER TABLE foo RENAME TO foo")
|
|
require.EqualError(t, err, errs.ErrTableAlreadyExists.Error())
|
|
|
|
err = db.Exec("ALTER TABLE foo RENAME TO bar")
|
|
require.NoError(t, err)
|
|
|
|
// Selecting from the old name should fail.
|
|
err = db.Exec("SELECT * FROM foo")
|
|
if !errors.Is(err, errs.ErrTableNotFound) {
|
|
require.Equal(t, errs.ErrTableNotFound, err)
|
|
}
|
|
|
|
d, err := db.QueryDocument("SELECT * FROM bar")
|
|
require.NoError(t, err)
|
|
data, err := document.MarshalJSON(d)
|
|
require.NoError(t, err)
|
|
require.JSONEq(t, `{"name": "John Doe", "age": 99}`, string(data))
|
|
|
|
// Renaming a read-only table should fail
|
|
err = db.Exec("ALTER TABLE __genji_tables RENAME TO bar")
|
|
require.Error(t, err)
|
|
}
|