Skip to content

Conversation

@eh-am
Copy link
Collaborator

@eh-am eh-am commented Sep 15, 2022

Closes #1509

Plus refactors the upsert as per @kolesnikovae's here #1508 (comment)

Here's the sql schema (generated with .schema annotations):

CREATE TABLE `annotations` (`id` integer,`app_name` text NOT NULL DEFAULT null,`timestamp` datetime,`content` text NOT NULL DEFAULT null,`created_at` datetime,`updated_at` datetime,PRIMARY KEY (`id`));
CREATE UNIQUE INDEX `idx_appname_timestamp` ON `annotations`(`app_name`,`timestamp`);

offtopic/self documenting: had to turn on sql log to make sense out of what the orm is doing

diff --git a/pkg/sqlstore/sqlstore.go b/pkg/sqlstore/sqlstore.go
index 642ad7ca..06c7e429 100644
--- a/pkg/sqlstore/sqlstore.go
+++ b/pkg/sqlstore/sqlstore.go
@@ -57,7 +57,8 @@ func (s *SQLStore) openSQLiteDB() (err error) {
 		path = s.config.Database.URL
 	}
 	s.orm, err = gorm.Open(sqlite.Open(path), &gorm.Config{
-		Logger: logger.Discard,
+		//		Logger: logger.Discard,
+		Logger: logger.Default.LogMode(logger.Info),
 	})
 	s.db, err = s.orm.DB()
 	return err

@eh-am eh-am requested a review from kolesnikovae September 15, 2022 20:56
@github-actions
Copy link
Contributor

github-actions bot commented Sep 15, 2022

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
webapp/public/assets/app.js 431.71 KB (0%) 8.7 s (0%) 3.2 s (+1.18% 🔺) 11.9 s
webapp/public/assets/app.css 16.77 KB (0%) 336 ms (0%) 0 ms (+100% 🔺) 336 ms
webapp/public/assets/styles.css 9.49 KB (0%) 190 ms (0%) 0 ms (+100% 🔺) 190 ms
packages/pyroscope-flamegraph/dist/index.js 92.08 KB (0%) 1.9 s (0%) 1.2 s (+29.88% 🔺) 3 s
packages/pyroscope-flamegraph/dist/index.node.js 91.97 KB (0%) 1.9 s (0%) 541 ms (-37.54% 🔽) 2.4 s
packages/pyroscope-flamegraph/dist/index.css 7.32 KB (0%) 147 ms (0%) 0 ms (+100% 🔺) 147 ms

@codecov
Copy link

codecov bot commented Sep 15, 2022

Codecov Report

Base: 66.62% // Head: 66.62% // No change to project coverage 👍

Coverage data is based on head (88fe79e) compared to base (c02c7f6).
Patch has no changes to coverable lines.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1513   +/-   ##
=======================================
  Coverage   66.62%   66.62%           
=======================================
  Files         146      146           
  Lines        4966     4966           
  Branches     1350     1350           
=======================================
  Hits         3308     3308           
  Misses       1653     1653           
  Partials        5        5           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +34 to 45
if err := tx.Clauses(clause.OnConflict{
Columns: []clause.Column{
{Name: "app_name"},
{Name: "timestamp"},
},
// Update fields we care about
DoUpdates: clause.AssignmentColumns([]string{"app_name", "timestamp", "content"}),
// Update updateAt fields
UpdateAll: true,
}).Create(&u).Error; err != nil {
return nil, err
}
Copy link
Collaborator Author

@eh-am eh-am Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is an improvement.

There are a few things happening here:

  1. We need to tell which fields we are interested in conflicting (app_name, timestamp), otherwise it defaults to the primary key. I decided to not use a natural primary key here, since we may want to for eg modify an existing annotation (change appName for example)
  2. DoUpdates since we need to tell which columns we want to update. Otherwise it doesn't do anything.
  3. UpdateAll to tell it to update updateAt field. Here's where it gets confusing: I thought UpdateAll would update everything (duh), but it seems to ignore the other fields.

Copy link
Collaborator

@kolesnikovae kolesnikovae Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting – I was sure that UpdateAll does update all columns but primary keys.

Maybe we should update content and updated_at explicitly – just for clarity:

{
    Columns: ...,
    DoUpdates: clause.AssignmentColumns([]string{"content", "updated_at"}),
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not work, I think it's due to how updated_at is generated dynamically by gorm.

@eh-am eh-am marked this pull request as ready for review September 15, 2022 21:28
func addIndexesUniqueTableMigration() *gormigrate.Migration {
type annotation struct {
AppName string `gorm:"index:idx_appname_timestamp,unique;not null;default:null"`
Timestamp time.Time `gorm:"index:idx_appname_timestamp,unique;not null;default:null"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous declaration has a typo: form -> gorm

Timestamp time.Time `form:"not null;default:null"`

Copy link
Collaborator Author

@eh-am eh-am Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed (and fixed it), but not in migration thought right?

@kolesnikovae
Copy link
Collaborator

Regarding SQL logging – I'd say that we should not enable it by default

@kolesnikovae
Copy link
Collaborator

kolesnikovae commented Sep 16, 2022

Honestly, I don't quite understand the restriction: why can't we have multiple annotations for the same application and the same moment of time?

@eh-am
Copy link
Collaborator Author

eh-am commented Sep 16, 2022

Regarding SQL logging – I'd say that we should not enable it by default

Yeah, I didn't even commit it. I was just documenting for the next person.

Honestly, I don't quite understand the restriction: why can't we have multiple annotations for the same application and the same moment of time?

From a technical perspective? None.

From a practical perspective? Couple reasons (at least):

  • UI is just not ready for it (instead of a simple tooltip we would need to have a more robust component)
  • we may want to implement some kind of threading. eg if i add an annotation on top of an existing annotation, there's some ordering implicit where the original one needs to be displayed first

@eh-am eh-am merged commit 337921e into main Sep 16, 2022
@eh-am eh-am deleted the chore/annotations-index branch September 16, 2022 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make pair (appName, timestamp) be unique

3 participants