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

fix: prevent database rebuild when server starting #269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions server/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"github.com/goccy/bigquery-emulator/types"
)

func (s *Server) addProjects(ctx context.Context, projects []*types.Project) error {
func (s *Server) addProjects(ctx context.Context, projects []*types.Project, reload bool) error {
for _, project := range projects {
if err := s.addProject(ctx, project); err != nil {
if err := s.addProject(ctx, project, reload); err != nil {
return err
}
}
return nil
}

func (s *Server) addProject(ctx context.Context, project *types.Project) error {
func (s *Server) addProject(ctx context.Context, project *types.Project, reload bool) error {
conn, err := s.connMgr.Connection(ctx, project.ID, "")
if err != nil {
return err
Expand All @@ -40,6 +40,22 @@ func (s *Server) addProject(ctx context.Context, project *types.Project) error {
return err
}
if found != nil {
if !reload {
return nil
}

for _, dataset := range found.Datasets() {
for _, table := range dataset.Tables() {
if err := table.Delete(ctx, tx.Tx()); err != nil {
return err
}
}

if err := dataset.Delete(ctx, tx.Tx()); err != nil {
return err
}
}

if err := s.metaRepo.UpdateProject(ctx, tx.Tx(), p); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions server/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func YAMLSource(path string) Source {
if err := dec.Decode(&v); err != nil {
return errors.New(yaml.FormatError(err, false, true))
}
return s.addProjects(context.Background(), v.Projects)
return s.addProjects(context.Background(), v.Projects, true)
}
}

func StructSource(projects ...*types.Project) Source {
return func(s *Server) error {
return s.addProjects(context.Background(), projects)
return s.addProjects(context.Background(), projects, false)
}
}