Skip to content

Commit

Permalink
Implement resourceInUse error for dataset deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaibbq committed Apr 14, 2024
1 parent 76f8e9e commit fa9d0a3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions internal/metadata/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var ErrDuplicatedDataset = errors.New("dataset is already created")
var ErrDatasetInUse = errors.New("dataset is in use, empty the dataset before deleting it")

type Project struct {
ID string
Expand Down Expand Up @@ -92,6 +93,9 @@ func (p *Project) DeleteDataset(ctx context.Context, tx *sql.Tx, id string) erro
if !exists {
return fmt.Errorf("dataset '%s' is not found in project '%s'", id, p.ID)
}
if len(dataset.TableIDs()) > 0 {
return fmt.Errorf("dataset %s: %w", id, ErrDatasetInUse)
}
if err := dataset.Delete(ctx, tx); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ func (h *datasetsDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
dataset: dataset,
deleteContents: isDeleteContents(r),
}); err != nil {
if errors.Is(err, metadata.ErrDatasetInUse) {
errorResponse(ctx, w, errResourceInUse(err.Error()))
return
}

errorResponse(ctx, w, errInternalError(err.Error()))
return
}
Expand Down
66 changes: 59 additions & 7 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,30 @@ import (
"google.golang.org/api/option"
)

func buildClient(ctx context.Context, project *types.Project, server *server.TestServer) (*bigquery.Client, error) {
client, err := bigquery.NewClient(
ctx,
project.ID,
option.WithEndpoint(server.URL),
option.WithoutAuthentication(),
)

if err != nil {
return nil, err
}
return client, nil

}

func TestSimpleQuery(t *testing.T) {
ctx := context.Background()

bqServer, err := server.New(server.TempStorage)
if err != nil {
t.Fatal(err)
}
if err := bqServer.Load(server.StructSource(types.NewProject("test"))); err != nil {
project := types.NewProject("test")
if err := bqServer.Load(server.StructSource()); err != nil {
t.Fatal(err)
}
testServer := bqServer.TestServer()
Expand All @@ -46,12 +62,7 @@ func TestSimpleQuery(t *testing.T) {
bqServer.Stop(ctx)
}()

client, err := bigquery.NewClient(
ctx,
"test",
option.WithEndpoint(testServer.URL),
option.WithoutAuthentication(),
)
client, err := buildClient(ctx, project, testServer)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -893,6 +904,47 @@ func TestDuplicateDataset(t *testing.T) {
}
}

func TestDeleteDatasetInUseJob(t *testing.T) {
const (
projectName = "test"
datasetName = "dataset1"
)
ctx := context.Background()

bqServer, err := server.New(server.TempStorage)
if err != nil {
t.Fatal(err)
}

project := types.NewProject(projectName, types.NewDataset(datasetName,
types.NewTable("table1", []*types.Column{
types.NewColumn("id", types.STRING),
}, nil),
))

if err := bqServer.Load(server.StructSource(project)); err != nil {
t.Fatal(err)
}

testServer := bqServer.TestServer()
defer func() {
testServer.Close()
bqServer.Stop(ctx)
}()

client, err := buildClient(ctx, project, testServer)
if err != nil {
t.Fatal(err)
}
defer client.Close()

dataset := client.Dataset(datasetName)
err = dataset.Delete(ctx)
if err == nil || !strings.HasSuffix(err.Error(), "resourceInUse") {
t.Fatalf("expected resource in use error; got %s", err)
}
}

func TestDataFromStruct(t *testing.T) {
ctx := context.Background()

Expand Down

0 comments on commit fa9d0a3

Please sign in to comment.