Skip to content

Commit

Permalink
convert generated column to static and embed server token SQL functio…
Browse files Browse the repository at this point in the history
…ns into statements
  • Loading branch information
jessepeterson committed Aug 22, 2023
1 parent 40e7c16 commit f78cb24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
16 changes: 12 additions & 4 deletions storage/mysql/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ func (s *MySQLStorage) StoreDeclaration(ctx context.Context, d *ddm.Declaration)
ctx,
`
INSERT INTO declarations
(identifier, type, payload)
(identifier, type, payload, server_token)
VALUES
(?, ?, ?) AS new
(?, ?, ?, SHA1(CONCAT(identifier, type, payload, CURRENT_TIMESTAMP, 0))) AS new
ON DUPLICATE KEY
UPDATE
type = new.type,
payload = new.payload;`,
payload = new.payload,
server_token = SHA1(CONCAT(new.identifier, new.type, new.payload, created_at, touched_ct));`,
d.Identifier,
d.Type,
d.PayloadJSON,
Expand Down Expand Up @@ -178,7 +179,14 @@ func (s *MySQLStorage) RetrieveDeclarations(ctx context.Context) ([]string, erro
func (s *MySQLStorage) TouchDeclaration(ctx context.Context, declarationID string) error {
result, err := s.db.ExecContext(
ctx,
`UPDATE declarations SET touched_ct = touched_ct + 1 WHERE identifier = ?;`,
`
UPDATE
declarations
SET
touched_ct = touched_ct + 1,
server_token = SHA1(CONCAT(identifier, type, payload, created_at, touched_ct))
WHERE
identifier = ?;`,
declarationID,
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions storage/mysql/schema.00003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE declarations MODIFY COLUMN server_token CHAR(40) NOT NULL;
6 changes: 5 additions & 1 deletion storage/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ CREATE TABLE declarations (
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
touched_ct INT DEFAULT 0 NOT NULL,

server_token CHAR(40) AS (SHA1(CONCAT(identifier, type, payload, created_at, touched_ct))) STORED NOT NULL,
-- previously: AS (SHA1(CONCAT(identifier, type, payload, created_at, touched_ct))) STORED
-- due to a schema management system this type of column isn't used
-- despite MySQL supporting it just fine. instead we manually inject the
-- stored SHA-1 in the SQL in declarations.go.
server_token CHAR(40) NOT NULL,

PRIMARY KEY (identifier),

Expand Down

0 comments on commit f78cb24

Please sign in to comment.