-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce postgres dbstore with migrations (#198)
* feat: add postgres to docker compose Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * feat: add postgres dbstore migrations/schemas and basic queries Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * feat: add postgres dbstore code generated with sqlc Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * feat: add repositories with generated mocks Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * feat: add postgres config Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * feat: add repositories to ingestion processor Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * feat: reconciler cmd - run postgres migrations on service startup Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: add gen-mocks and gen-dbstore makefile recipes Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: regenerate mocks with latest mockery Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: add install-dev-tools makefile recipe Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: mockery config update ref: https://vektra.github.io/mockery/v2.49/deprecations/#issue-845-fix Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: tidyup migrations Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: tidyup mockery config Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> * chore: update diode-server README to use docker compose from release branch Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com> --------- Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com>
- Loading branch information
1 parent
8015871
commit c10c080
Showing
29 changed files
with
782 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
diode-server/dbstore/postgres/migrations/00001_ingestion_logs.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-- +goose Up | ||
|
||
-- Create the ingestion_logs table | ||
CREATE TABLE IF NOT EXISTS ingestion_logs | ||
( | ||
id SERIAL PRIMARY KEY, | ||
ingestion_log_ksuid CHAR(27) NOT NULL, | ||
data_type VARCHAR(255), | ||
state INTEGER, | ||
request_id VARCHAR(255), | ||
ingestion_ts BIGINT, | ||
producer_app_name VARCHAR(255), | ||
producer_app_version VARCHAR(255), | ||
sdk_name VARCHAR(255), | ||
sdk_version VARCHAR(255), | ||
entity JSONB, | ||
error JSONB, | ||
source_metadata JSONB, | ||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- Create indices | ||
CREATE INDEX IF NOT EXISTS idx_ingestion_logs_ingestion_log_ksuid ON ingestion_logs(ingestion_log_ksuid); | ||
CREATE INDEX IF NOT EXISTS idx_ingestion_logs_data_type ON ingestion_logs(data_type); | ||
CREATE INDEX IF NOT EXISTS idx_ingestion_logs_state ON ingestion_logs(state); | ||
CREATE INDEX IF NOT EXISTS idx_ingestion_logs_request_id ON ingestion_logs(request_id); | ||
|
||
-- +goose Down | ||
|
||
-- Drop the ingestion_logs table | ||
DROP TABLE ingestion_logs; |
49 changes: 49 additions & 0 deletions
49
diode-server/dbstore/postgres/migrations/00002_change_sets.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- +goose Up | ||
|
||
-- Create the change_sets table | ||
CREATE TABLE IF NOT EXISTS change_sets | ||
( | ||
id SERIAL PRIMARY KEY, | ||
change_set_ksuid CHAR(27) NOT NULL, | ||
ingestion_log_id INTEGER NOT NULL, | ||
branch_name VARCHAR(255), | ||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- Create indices | ||
CREATE INDEX IF NOT EXISTS idx_change_sets_change_set_ksuid ON change_sets(change_set_ksuid); | ||
|
||
-- Create the changes table | ||
CREATE TABLE IF NOT EXISTS changes | ||
( | ||
id SERIAL PRIMARY KEY, | ||
change_ksuid CHAR(27) NOT NULL, | ||
change_set_id INTEGER NOT NULL, | ||
change_type VARCHAR(50) NOT NULL, | ||
object_type VARCHAR(100) NOT NULL, | ||
object_id INTEGER, | ||
object_version INTEGER, | ||
data JSONB, | ||
sequence_number INTEGER, | ||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- Create indices | ||
CREATE INDEX IF NOT EXISTS idx_changes_change_ksuid ON changes(change_ksuid); | ||
CREATE INDEX IF NOT EXISTS idx_changes_change_set_id ON changes(change_set_id); | ||
CREATE INDEX IF NOT EXISTS idx_changes_change_type ON changes(change_type); | ||
CREATE INDEX IF NOT EXISTS idx_changes_object_type ON changes(object_type); | ||
|
||
-- Add foreign key constraints | ||
ALTER TABLE change_sets ADD CONSTRAINT fk_change_sets_ingestion_logs FOREIGN KEY (ingestion_log_id) REFERENCES ingestion_logs(id); | ||
ALTER TABLE changes ADD CONSTRAINT fk_changes_change_sets FOREIGN KEY (change_set_id) REFERENCES change_sets(id); | ||
|
||
-- +goose Down | ||
|
||
-- Drop the changes table | ||
DROP TABLE changes; | ||
|
||
-- Drop the change_sets table | ||
DROP TABLE change_sets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- name: CreateChangeSet :one | ||
|
||
INSERT INTO change_sets (change_set_ksuid, ingestion_log_id, branch_name) | ||
VALUES ($1, $2, $3) | ||
RETURNING *; | ||
|
||
-- name: CreateChange :one | ||
|
||
INSERT INTO changes (change_ksuid, change_set_id, change_type, object_type, object_id, object_version, data, | ||
sequence_number) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) | ||
RETURNING *; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- name: CreateIngestionLog :one | ||
INSERT INTO ingestion_logs (ingestion_log_ksuid, data_type, state, request_id, ingestion_ts, producer_app_name, | ||
producer_app_version, sdk_name, sdk_version, entity, source_metadata) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/netboxlabs/diode/diode-server/gen/dbstore/postgres" | ||
"github.com/netboxlabs/diode/diode-server/gen/diode/v1/reconcilerpb" | ||
) | ||
|
||
// IngestionLogRepository allows interacting with ingestion logs. | ||
type IngestionLogRepository struct { | ||
queries *postgres.Queries | ||
} | ||
|
||
// NewIngestionLogRepository creates a new IngestionLogRepository. | ||
func NewIngestionLogRepository(db postgres.DBTX) *IngestionLogRepository { | ||
return &IngestionLogRepository{ | ||
queries: postgres.New(db), | ||
} | ||
} | ||
|
||
// CreateIngestionLog creates a new ingestion log. | ||
func (r *IngestionLogRepository) CreateIngestionLog(_ context.Context, _ *reconcilerpb.IngestionLog, _ []byte) error { | ||
return errors.New("not implemented") | ||
} | ||
|
||
// ChangeSetRepository allows interacting with change sets. | ||
type ChangeSetRepository struct { | ||
queries *postgres.Queries | ||
} | ||
|
||
// NewChangeSetRepository creates a new ChangeSetRepository. | ||
func NewChangeSetRepository(db postgres.DBTX) *ChangeSetRepository { | ||
return &ChangeSetRepository{ | ||
queries: postgres.New(db), | ||
} | ||
} | ||
|
||
// CreateChangeSet creates a new change set. | ||
func (r *ChangeSetRepository) CreateChangeSet(_ context.Context, _ *reconcilerpb.ChangeSet) error { | ||
return errors.New("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.