Skip to content

Commit

Permalink
add: version column to crawls table
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Nov 20, 2023
1 parent f74507c commit eceaae7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/nebula/cmd_crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func CrawlAction(c *cli.Context) error {
// Inserting a crawl row into the db so that we
// can associate results with this crawl via
// its DB identifier
dbCrawl, err := dbc.InitCrawl(ctx)
dbCrawl, err := dbc.InitCrawl(ctx, cfg.Root.Version())
if err != nil {
return fmt.Errorf("creating crawl in db: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type Client interface {
io.Closer
InitCrawl(ctx context.Context) (*models.Crawl, error)
InitCrawl(ctx context.Context, version string) (*models.Crawl, error)
UpdateCrawl(ctx context.Context, crawl *models.Crawl) error
PersistCrawlProperties(ctx context.Context, crawl *models.Crawl, properties map[string]map[string]int) error
PersistCrawlVisit(ctx context.Context, crawlID int, peerID peer.ID, maddrs []ma.Multiaddr, protocols []string, agentVersion string, connectDuration time.Duration, crawlDuration time.Duration, visitStartedAt time.Time, visitEndedAt time.Time, connectErrorStr string, crawlErrorStr string, properties null.JSON) (*InsertVisitResult, error)
Expand Down
3 changes: 2 additions & 1 deletion pkg/db/client_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ func partitionQuery(table string, lower time.Time, upper time.Time) string {

// InitCrawl inserts a crawl instance into the database in the state `started`.
// This is done to receive a database ID that all subsequent database entities can be linked to.
func (c *DBClient) InitCrawl(ctx context.Context) (*models.Crawl, error) {
func (c *DBClient) InitCrawl(ctx context.Context, version string) (*models.Crawl, error) {
crawl := &models.Crawl{
State: models.CrawlStateStarted,
StartedAt: time.Now(),
Version: version,
}
return crawl, crawl.Insert(ctx, c.dbh, boil.Infer())
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/db/client_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ func InitJSONClient(out string) (Client, error) {
return client, nil
}

func (c *JSONClient) InitCrawl(ctx context.Context) (*models.Crawl, error) {
func (c *JSONClient) InitCrawl(ctx context.Context, version string) (*models.Crawl, error) {
crawl := &models.Crawl{
State: models.CrawlStateStarted,
StartedAt: time.Now(),
Version: version,
UpdatedAt: time.Now(),
CreatedAt: time.Now(),
}

data, err := json.Marshal(crawl)
Expand All @@ -85,6 +88,8 @@ func (c *JSONClient) InitCrawl(ctx context.Context) (*models.Crawl, error) {
}

func (c *JSONClient) UpdateCrawl(ctx context.Context, crawl *models.Crawl) error {
crawl.UpdatedAt = time.Now()

data, err := json.Marshal(crawl)
if err != nil {
return fmt.Errorf("marshal crawl json: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/db/client_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ func InitNoopClient() *NoopClient {
return &NoopClient{}
}

func (n *NoopClient) InitCrawl(ctx context.Context) (*models.Crawl, error) {
func (n *NoopClient) InitCrawl(ctx context.Context, version string) (*models.Crawl, error) {
return &models.Crawl{
ID: 1,
StartedAt: time.Now(),
State: models.CrawlStateStarted,
Version: version,
UpdatedAt: time.Now(),
CreatedAt: time.Now(),
}, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE crawls DROP COLUMN version;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN;

ALTER TABLE crawls ADD COLUMN version TEXT;

UPDATE crawls SET version = 'undefined';

ALTER TABLE crawls ALTER COLUMN version SET NOT NULL;

COMMIT;
11 changes: 9 additions & 2 deletions pkg/models/crawls.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eceaae7

Please sign in to comment.