MCPcopy Create free account
hub / github.com/bytebase/bytebase / CreateSheets

Method CreateSheets

backend/store/sheet.go:111–143  ·  view source on GitHub ↗

CreateSheets creates sheet blobs using content-addressed storage. Each sheet is identified by the SHA256 hash of its statement. Duplicate statements share the same blob (ON CONFLICT DO NOTHING).

(ctx context.Context, creates ...*SheetMessage)

Source from the content-addressed store, hash-verified

109// Each sheet is identified by the SHA256 hash of its statement.
110// Duplicate statements share the same blob (ON CONFLICT DO NOTHING).
111func (s *Store) CreateSheets(ctx context.Context, creates ...*SheetMessage) ([]*SheetMessage, error) {
112 var statements []string
113 var sha256s [][]byte
114
115 for _, c := range creates {
116 statements = append(statements, c.Statement)
117 h := sha256.Sum256([]byte(c.Statement))
118 c.Sha256 = hex.EncodeToString(h[:])
119 sha256s = append(sha256s, h[:])
120 c.Size = int64(len(c.Statement))
121 }
122
123 q := qb.Q().Space(`
124 INSERT INTO sheet_blob (
125 sha256,
126 content
127 ) SELECT
128 unnest(CAST(? AS BYTEA[])),
129 unnest(CAST(? AS TEXT[]))
130 ON CONFLICT DO NOTHING
131 `, sha256s, statements)
132
133 query, args, err := q.ToSQL()
134 if err != nil {
135 return nil, errors.Wrapf(err, "failed to build sql")
136 }
137
138 if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {
139 return nil, errors.Wrapf(err, "failed to exec")
140 }
141
142 return creates, nil
143}
144
145// HasSheets checks if all sheets exist by SHA256 hashes.
146func (s *Store) HasSheets(ctx context.Context, sha256Hexes ...string) (bool, error) {

Callers 4

CreateSheetMethod · 0.80
BatchCreateSheetsMethod · 0.80
CreateReleaseMethod · 0.80

Calls 4

GetDBMethod · 0.95
QFunction · 0.92
SpaceMethod · 0.80
ToSQLMethod · 0.80

Tested by

no test coverage detected