Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BED-5080 - remove additional integer types from SQL schema #992

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions packages/go/dawgs/drivers/pg/pg_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"testing"

"github.com/specterops/bloodhound/src/test/integration"

"github.com/specterops/bloodhound/dawgs"
"github.com/specterops/bloodhound/dawgs/drivers/pg"
"github.com/specterops/bloodhound/dawgs/graph"
Expand All @@ -33,25 +35,10 @@ import (
"github.com/stretchr/testify/require"
)

const murderDB = `
do
$$
declare
t text;
begin
for t in select relname from pg_class where oid in (select partrelid from pg_partitioned_table)
loop
execute 'drop table ' || t || ' cascade';
end loop;

for t in select table_name from information_schema.tables where table_schema = current_schema() and not table_name ilike '%pg_stat%'
loop
execute 'drop table ' || t || ' cascade';
end loop;
end
$$;`

func Test_ResetDB(t *testing.T) {
// We don't need the reference to the DB but this will ensure that the canonical DB wipe method is called
integration.SetupDB(t)

ctx, done := context.WithCancel(context.Background())
defer done()

Expand All @@ -63,7 +50,6 @@ func Test_ResetDB(t *testing.T) {
})
require.Nil(t, err)

require.Nil(t, graphDB.Run(ctx, murderDB, nil))
require.Nil(t, graphDB.AssertSchema(ctx, graphschema.DefaultGraphSchema()))

require.Nil(t, graphDB.WriteTransaction(ctx, func(tx graph.Transaction) error {
Expand Down Expand Up @@ -108,3 +94,31 @@ func TestPG(t *testing.T) {
return tx.Query("match p = (s:User)-[*..]->(:Computer) return p", nil).Error()
}))
}

// TestInt64SequenceIDs is a test that validates against a regression in any logic that originally used graph IDs.
// Before this test and its related changes, graph IDs were stored as uint32 values. Scale of graphs necessitated
// a change to an int64 ID space, however, the nuance of this refactor revealed several bugs and assumptions in
// query formatting as well as business logic.
//
// This test represents validating that an instance can insert a node or an edge that has an ID greater than the maximum
// value of an int32 graph ID.
func TestInt64SequenceIDs(t *testing.T) {
var (
// We don't need the reference to the DB but this will ensure that the canonical DB wipe method is called
_ = integration.SetupDB(t)
graphTestContext = integration.NewGraphTestContext(t, graphschema.DefaultGraphSchema())
)

if pg.IsPostgreSQLGraph(graphTestContext.Graph.Database) {
// Move the ID space out past an int32's max value
require.Nil(t, graphTestContext.Graph.Database.Run(context.Background(), "alter sequence node_id_seq restart with 2147483648;", make(map[string]any)))
require.Nil(t, graphTestContext.Graph.Database.Run(context.Background(), "alter sequence edge_id_seq restart with 2147483648;", make(map[string]any)))
}

var (
// Create two users, chuck and steve where chuck has GenericAll on steve
chuckNode = graphTestContext.NewNode(graph.AsProperties(map[string]any{"name": "chuck"}), ad.User, ad.Entity)
steveNode = graphTestContext.NewNode(graph.AsProperties(map[string]any{"name": "steve"}), ad.User, ad.Entity)
_ = graphTestContext.NewRelationship(chuckNode, steveNode, ad.GenericAll)
)
}
8 changes: 4 additions & 4 deletions packages/go/dawgs/drivers/pg/query/sql/schema_up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ $$
begin
create type nodeComposite as
(
id integer,
id bigint,
kind_ids smallint[8],
properties jsonb
);
Expand Down Expand Up @@ -133,9 +133,9 @@ $$
begin
create type edgeComposite as
(
id integer,
start_id integer,
end_id integer,
id bigint,
start_id bigint,
end_id bigint,
kind_id smallint,
properties jsonb
);
Expand Down
Loading