Skip to content

Commit b4762d3

Browse files
committed
RHINENG-26529: Revert RHINENG-25147 workspace changes (v3.8.260)
1 parent 3fcab0a commit b4762d3

49 files changed

Lines changed: 315 additions & 362 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base/core/gintesting.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ type ContextKV struct {
1717

1818
var V3APICtx = ContextKV{Key: utils.KeyApiver, Value: 3}
1919

20-
var WorkspacesTestCtx = ContextKV{
21-
Key: utils.KeyInventoryWorkspaces,
22-
Value: []string{
23-
"00000000-0000-0000-0000-000000000001",
24-
"00000000-0000-0000-0000-000000000002",
25-
"00000000-0000-0000-0000-999999999999",
26-
},
27-
}
28-
2920
func InitRouter(handler gin.HandlerFunc, contextKVs ...ContextKV) *gin.Engine {
3021
return InitRouterWithPath(handler, "/", contextKVs...)
3122
}
@@ -41,7 +32,6 @@ func InitRouterWithParams(handler gin.HandlerFunc, account int, method, path str
4132
router.Use(func(c *gin.Context) {
4233
// set default api version for tests to latest
4334
c.Set(utils.KeyApiver, LatestAPIVersion)
44-
c.Set(utils.KeyInventoryWorkspaces, WorkspacesTestCtx.Value)
4535
for _, kv := range contextKVs {
4636
c.Set(kv.Key, kv.Value)
4737
}

base/database/utils.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ func (j joinsT) apply(tx *gorm.DB) *gorm.DB {
2626
return tx
2727
}
2828

29-
func Systems(tx *gorm.DB, accountID int, workspaceIDs []string, joins ...join) *gorm.DB {
29+
func Systems(tx *gorm.DB, accountID int, groups map[string]string, joins ...join) *gorm.DB {
3030
tx = tx.Table("system_inventory si").
3131
Joins("JOIN system_patch spatch ON si.id = spatch.system_id AND si.rh_account_id = spatch.rh_account_id").
3232
Where("si.rh_account_id = ?", accountID)
3333
tx = (joinsT)(joins).apply(tx)
34-
return ApplyInventoryWorkspaceFilter(tx, workspaceIDs)
34+
return ApplyInventoryWorkspaceFilter(tx, groups)
3535
}
3636

37-
func SystemAdvisories(tx *gorm.DB, accountID int, workspaceIDs []string, joins ...join) *gorm.DB {
38-
tx = Systems(tx, accountID, workspaceIDs).
37+
func SystemAdvisories(tx *gorm.DB, accountID int, groups map[string]string, joins ...join) *gorm.DB {
38+
tx = Systems(tx, accountID, groups).
3939
Joins("JOIN system_advisories sa on sa.system_id = si.id AND sa.rh_account_id = ?", accountID)
4040
return (joinsT)(joins).apply(tx)
4141
}
@@ -46,8 +46,8 @@ func SystemPackagesShort(tx *gorm.DB, accountID int, joins ...join) *gorm.DB {
4646
return (joinsT)(joins).apply(tx)
4747
}
4848

49-
func SystemPackages(tx *gorm.DB, accountID int, workspaceIDs []string, joins ...join) *gorm.DB {
50-
tx = Systems(tx, accountID, workspaceIDs).
49+
func SystemPackages(tx *gorm.DB, accountID int, groups map[string]string, joins ...join) *gorm.DB {
50+
tx = Systems(tx, accountID, groups).
5151
Joins("JOIN system_package2 spkg on spkg.system_id = si.id AND spkg.rh_account_id = ?", accountID).
5252
Joins("JOIN package p on p.id = spkg.package_id").
5353
Joins("JOIN package_name pn on pn.id = spkg.name_id")
@@ -65,9 +65,9 @@ func PackageByName(tx *gorm.DB, pkgName string, joins ...join) *gorm.DB {
6565
return (joinsT)(joins).apply(tx)
6666
}
6767

68-
func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, workspaceIDs []string, inventoryID string,
68+
func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, groups map[string]string, inventoryID string,
6969
joins ...join) *gorm.DB {
70-
tx = SystemAdvisories(tx, accountID, workspaceIDs).Where("si.inventory_id = ?::uuid", inventoryID)
70+
tx = SystemAdvisories(tx, accountID, groups).Where("si.inventory_id = ?::uuid", inventoryID)
7171
return (joinsT)(joins).apply(tx)
7272
}
7373

@@ -240,11 +240,21 @@ func ReadReplicaConfigured() bool {
240240
return len(utils.CoreCfg.DBReadReplicaHost) > 0 && utils.CoreCfg.DBReadReplicaPort != 0
241241
}
242242

243-
func ApplyInventoryWorkspaceFilter(tx *gorm.DB, workspaceIDs []string) *gorm.DB {
244-
if len(workspaceIDs) == 0 {
245-
utils.LogWarn("there should always be some workspaces, at least root workspace")
243+
func ApplyInventoryWorkspaceFilter(tx *gorm.DB, groups map[string]string) *gorm.DB {
244+
if _, ok := groups[utils.KeyGrouped]; !ok {
245+
if _, ok := groups[utils.KeyUngrouped]; ok {
246+
// show only systems with '[]' group
247+
return tx.Where("si.workspaces = '[]'")
248+
}
249+
// return query without WHERE if there are no groups
250+
return tx
251+
}
252+
253+
db := DB.Where("si.workspaces @> ANY (?::jsonb[])", groups[utils.KeyGrouped])
254+
if _, ok := groups[utils.KeyUngrouped]; ok {
255+
db = db.Or("si.workspaces = '[]'")
246256
}
247-
return tx.Where("si.workspace_id IN (?)", workspaceIDs)
257+
return tx.Where(db)
248258
}
249259

250260
// LEFT JOIN templates to spatch (system_patch)

base/database/utils_test.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,38 @@ var (
1111
// counts of systems from system_inventory (+ system_patch join in Systems())
1212
nGroup1 int64 = 7
1313
nGroup2 int64 = 2
14-
nUngrouped int64 = 9
14+
nUngrouped int64 = 7
1515
nAll int64 = 18
1616
)
17-
var nonExisting = "00000000-0000-0000-3333-000000000000"
1817

19-
var testCases = []map[int64][]string{
20-
{nGroup1: {"00000000-0000-0000-0000-000000000001"}},
21-
{nGroup2: {"00000000-0000-0000-0000-000000000002"}},
22-
{nGroup1 + nGroup2: {"00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002"}},
23-
{nGroup1 + nUngrouped: {"00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-999999999999"}},
24-
{nUngrouped: {nonExisting, "00000000-0000-0000-0000-999999999999"}},
25-
{0: {nonExisting}},
26-
{nUngrouped: {"00000000-0000-0000-0000-999999999999"}},
27-
{nAll: {"00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002",
28-
"00000000-0000-0000-0000-999999999999"}},
18+
var testCases = []map[int64]map[string]string{
19+
{nGroup1: {utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]"}`}},
20+
{nGroup2: {utils.KeyGrouped: `{"[{\"id\":\"inventory-group-2\"}]"}`}},
21+
{nGroup1 + nGroup2: {utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`}},
22+
{nGroup1 + nUngrouped: {
23+
utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]"}`,
24+
utils.KeyUngrouped: "[]",
25+
}},
26+
{nUngrouped: {
27+
utils.KeyGrouped: `{"[{\"id\":\"non-existing-group\"}]"}`,
28+
utils.KeyUngrouped: "[]",
29+
}},
30+
{0: {utils.KeyGrouped: `{"[{\"id\":\"non-existing-group\"}]"}`}},
31+
{nUngrouped: {utils.KeyUngrouped: "[]"}},
32+
{nAll: {}},
33+
{nAll: nil},
2934
}
3035

3136
func TestApplyInventoryWorkspaceFilter(t *testing.T) {
3237
utils.SkipWithoutDB(t)
3338
Configure()
3439

3540
for _, tc := range testCases {
36-
for expectedCount, workspaceIDs := range tc {
41+
for expectedCount, groups := range tc {
3742
var count int64
3843
ApplyInventoryWorkspaceFilter(DB.Table("system_inventory si").
3944
Joins("JOIN system_patch spatch ON si.id = spatch.system_id AND si.rh_account_id = spatch.rh_account_id"),
40-
workspaceIDs).Count(&count)
45+
groups).Count(&count)
4146
assert.Equal(t, expectedCount, count)
4247
}
4348
}

base/inventory/inventory.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package inventory
22

33
import (
44
"app/base/types"
5+
"database/sql/driver"
6+
"encoding/json"
7+
"errors"
58

69
"github.com/google/uuid"
710
)
@@ -83,6 +86,35 @@ type Group struct {
8386
Name string `json:"name"`
8487
}
8588

89+
// Groups is a slice of Group that implements driver.Valuer and sql.Scanner
90+
// for storing/loading as JSONB in the database (e.g. system_inventory.workspaces).
91+
type Groups []Group
92+
93+
// Value implements driver.Valuer for GORM: marshal to JSON for DB write.
94+
func (g *Groups) Value() (driver.Value, error) {
95+
if g == nil {
96+
return nil, nil
97+
}
98+
return json.Marshal(g)
99+
}
100+
101+
// Scan implements sql.Scanner for GORM: unmarshal from JSON on DB read.
102+
func (g *Groups) Scan(value interface{}) error {
103+
if value == nil {
104+
*g = nil
105+
return nil
106+
}
107+
b, ok := value.([]byte)
108+
if !ok {
109+
return errors.New("inventory.Groups: type assertion to []byte failed")
110+
}
111+
if len(b) == 0 {
112+
*g = nil
113+
return nil
114+
}
115+
return json.Unmarshal(b, g)
116+
}
117+
86118
type Workloads struct {
87119
Sap SapWorkload `json:"sap,omitempty"`
88120
Ansible AnsibleWorkload `json:"ansible,omitempty"`

base/models/models.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"app/base/inventory"
45
"time"
56

67
"github.com/google/uuid"
@@ -70,10 +71,9 @@ type SystemInventory struct {
7071
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
7172
Arch *string
7273
Bootc bool
73-
Tags []byte `gorm:"column:tags"`
74-
Created time.Time // set by trigger system_platform_insert_trigger
75-
WorkspaceID *uuid.UUID `gorm:"column:workspace_id"`
76-
WorkspaceName *string `gorm:"column:workspace_name"`
74+
Tags []byte `gorm:"column:tags"`
75+
Created time.Time // set by trigger system_platform_insert_trigger
76+
Workspaces *inventory.Groups `gorm:"column:workspaces"`
7777
StaleTimestamp *time.Time
7878
StaleWarningTimestamp *time.Time
7979
CulledTimestamp *time.Time

base/utils/gin.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313
)
1414

1515
const (
16-
KeyApiver = "apiver"
17-
KeyAccount = "account"
18-
KeyOrgID = "org_id"
19-
KeyUser = "user"
20-
KeySystem = "system_cn"
21-
KeyInventoryWorkspaces = "workspaceIDs"
16+
KeyApiver = "apiver"
17+
KeyAccount = "account"
18+
KeyOrgID = "org_id"
19+
KeyUser = "user"
20+
KeySystem = "system_cn"
21+
KeyInventoryGroups = "inventoryGroups"
22+
KeyGrouped = "grouped"
23+
KeyUngrouped = "ungrouped"
2224
// ReadHeaderTimeout same as nginx default
2325
ReadHeaderTimeout = 60 * time.Second
2426
)

database_admin/migrations/153_simplify_workspaces.up.sql

Lines changed: 0 additions & 13 deletions
This file was deleted.

database_admin/schema/create_schema.sql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations
77

88

99
INSERT INTO schema_migrations
10-
VALUES (153, false);
10+
VALUES (152, false);
1111

1212
-- ---------------------------------------------------------------------------
1313
-- Functions
@@ -592,8 +592,7 @@ CREATE TABLE IF NOT EXISTS system_inventory
592592
ansible_workload_controller_version TEXT CHECK (NOT empty(ansible_workload_controller_version)),
593593
mssql_workload BOOLEAN NOT NULL DEFAULT false,
594594
mssql_workload_version TEXT CHECK (NOT empty(mssql_workload_version)),
595-
workspace_id UUID,
596-
workspace_name TEXT CHECK (NOT empty(workspace_name)),
595+
workspaces JSONB,
597596
PRIMARY KEY (rh_account_id, id),
598597
UNIQUE (rh_account_id, inventory_id)
599598
) PARTITION BY HASH (rh_account_id);
@@ -625,8 +624,7 @@ SELECT create_table_partition_triggers('system_inventory_on_update',
625624
CREATE INDEX IF NOT EXISTS system_inventory_inventory_id_idx ON system_inventory (inventory_id);
626625
CREATE INDEX IF NOT EXISTS system_inventory_tags_index ON system_inventory USING GIN (tags JSONB_PATH_OPS);
627626
CREATE INDEX IF NOT EXISTS system_inventory_stale_timestamp_index ON system_inventory (stale_timestamp);
628-
CREATE INDEX IF NOT EXISTS system_inventory_workspace_id_index ON system_inventory (workspace_id);
629-
CREATE INDEX IF NOT EXISTS system_inventory_workspace_name_index ON system_inventory (workspace_name);
627+
CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);
630628

631629
CREATE TABLE IF NOT EXISTS deleted_system
632630
(

0 commit comments

Comments
 (0)