Skip to content

Commit

Permalink
Automigrate on local SQLite backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
andream16 committed Nov 26, 2024
1 parent 5aaa1ea commit 3d9f424
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
25 changes: 0 additions & 25 deletions sdk/component/internal/storer/local/sqlite/export_test.go

This file was deleted.

27 changes: 27 additions & 0 deletions sdk/component/internal/storer/local/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func NewManager(dsn string, opts ...managerOption) (*manager, error) {
}
}

if err := mgr.migrate(); err != nil {
return nil, errors.Errorf("could not apply migrations: %w", err)
}

return mgr, nil
}

Expand Down Expand Up @@ -211,6 +215,29 @@ func (m *manager) Close(ctx context.Context) error {
return nil
}

// TODO: potentially leverage migrations here but this is simple enough for now for local setup.
// Tracked here https://linear.app/smithy/issue/OCU-274/automigrate-on-sqlite-storage.
func (m *manager) migrate() error {
stmt, err := m.db.Prepare(`
CREATE TABLE IF NOT EXISTS finding (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
instance_id UUID NOT NULL UNIQUE,
findings TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`)
if err != nil {
return fmt.Errorf("could not prepare statement for creating table: %w", err)
}

if _, err := stmt.Exec(); err != nil {
return fmt.Errorf("could not create table: %w", err)
}

return stmt.Close()
}

func (m *manager) marshalFindings(findings []*ocsf.VulnerabilityFinding) (string, error) {
var rawFindings []json.RawMessage
for _, finding := range findings {
Expand Down
3 changes: 0 additions & 3 deletions sdk/component/internal/storer/local/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type (
component.Reader
component.Updater
component.Writer

CreateTable() error
}

ManagerTestSuite struct {
Expand All @@ -54,7 +52,6 @@ func (mts *ManagerTestSuite) SetupTest() {

mts.manager, err = sqlite.NewManager("smithy.db", sqlite.ManagerWithClock(clock))
require.NoError(mts.t, err)
require.NoError(mts.T(), mts.manager.CreateTable())
}

func (mts *ManagerTestSuite) TearDownTest() {
Expand Down

0 comments on commit 3d9f424

Please sign in to comment.