Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent creating views with same name as existing table #4548

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,19 @@ func (db Database) AllViews(ctx *sql.Context) ([]sql.ViewDefinition, error) {
// it can exist in a sql session later. Returns sql.ErrExistingView if a view
// with that name already exists.
func (db Database) CreateView(ctx *sql.Context, name string, definition string) error {
err := sql.ErrExistingView.New(db.name, name)
ws, err := db.GetWorkingSet(ctx)
if err != nil {
return err
}
root := ws.WorkingRoot()

if exists, err := root.HasTable(ctx, name); err != nil {
return err
} else if exists {
return sql.ErrTableAlreadyExists.New(name)
}
err = sql.ErrExistingView.New(db.name, name)

return db.addFragToSchemasTable(ctx, "view", name, definition, time.Unix(0, 0).UTC(), err)
}

Expand Down
7 changes: 7 additions & 0 deletions integration-tests/bats/create-views.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ SQL
[[ "${lines[3]}" =~ ' 4 ' ]] || false
}

@test "create-views: create view with same name as table" {
dolt sql -q "create table t (i int)"
run dolt sql -q "create view t as select 1 from dual"
[ "$status" -eq 1 ]
[[ "$output" =~ 'already exists' ]] || false
}

@test "create-views: drop a single view" {
run dolt sql <<SQL
create view four as select 2+2 as res from dual;
Expand Down