Skip to content
Open
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
4 changes: 4 additions & 0 deletions base/database/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func getQueryFromTags(v reflect.Type) (AttrMap, []AttrName, error) {
info.OrderQuery = info.DataQuery
}
res[columnName] = info
// Allow sort/filter by API name "id" when column is "inventory_id"
if columnName == "inventory_id" {
res["id"] = info
}

// Result HAS to contain all columns, because gorm loads by index, not by name
resNames = append(resNames, columnName)
Expand Down
29 changes: 0 additions & 29 deletions base/database/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,35 +369,6 @@ func GetPackageIDs(nevras ...string) []int64 {
return ids
}

func CreateSystem(t *testing.T, system *models.SystemPlatform) {
ts := time.Now()
err := DB.Create(&system).Error
assert.Nil(t, err)
err = DB.Table("inventory.hosts").
Create(map[string]any{
"id": system.InventoryID,
"account": fmt.Sprint(system.RhAccountID),
"display_name": system.DisplayName,
"tags": "{}",
"updated": ts,
"created": ts,
"stale_timestamp": ts,
"system_profile": "{}",
"reporter": "puptoo",
"per_reporter_staleness": "{}",
"org_id": "dummy_org",
"groups": "[]",
}).Error
assert.Nil(t, err)
}

func DeleteSystem(t *testing.T, inventoryID string) {
err := DB.Delete(models.SystemPlatform{}, "inventory_id = ?", inventoryID).Error
assert.Nil(t, err)
err = DB.Exec("DELETE FROM inventory.hosts WHERE id = ?", inventoryID).Error
assert.Nil(t, err)
}

func CreateTemplate(t *testing.T, account int, uuid string, inventoryIDs []string) {
template := &models.Template{
TemplateBase: models.TemplateBase{
Expand Down
8 changes: 4 additions & 4 deletions base/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,19 @@ func ReadReplicaConfigured() bool {
}

func InventoryHostsJoin(tx *gorm.DB, groups map[string]string) *gorm.DB {
tx = tx.Joins("JOIN inventory.hosts ih ON ih.id = sp.inventory_id")
tx = tx.Joins("JOIN system_inventory si ON si.inventory_id = sp.inventory_id")
if _, ok := groups[utils.KeyGrouped]; !ok {
if _, ok := groups[utils.KeyUngrouped]; ok {
// show only systems with '[]' group
return tx.Where("ih.groups = '[]'")
return tx.Where("si.workspaces = '[]'")
}
// return query without WHERE if there are no groups
return tx
}

db := DB.Where("ih.groups @> ANY (?::jsonb[])", groups[utils.KeyGrouped])
db := DB.Where("si.workspaces @> ANY (?::jsonb[])", groups[utils.KeyGrouped])
if _, ok := groups[utils.KeyUngrouped]; ok {
db = db.Or("ih.groups = '[]'")
db = db.Or("si.workspaces = '[]'")
}
return tx.Where(db)
}
Expand Down
36 changes: 35 additions & 1 deletion base/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package inventory

import (
"app/base/types"
"database/sql/driver"
"encoding/json"
"errors"

"github.com/google/uuid"
)

type SystemProfile struct {
Expand All @@ -15,7 +20,7 @@ type SystemProfile struct {
Releasever *string `json:"releasever,omitempty"`
SatelliteManaged bool `json:"satellite_managed,omitempty"`
BootcStatus Bootc `json:"bootc_status,omitempty"`
ConsumerID string `json:"owner_id,omitempty"`
OwnerID uuid.UUID `json:"owner_id,omitempty"`
Workloads Workloads `json:"workloads,omitempty"`
}

Expand Down Expand Up @@ -81,6 +86,35 @@ type Group struct {
Name string `json:"name"`
}

// Groups is a slice of Group that implements driver.Valuer and sql.Scanner
// for storing/loading as JSONB in the database (e.g. system_inventory.workspaces).
type Groups []Group

// Value implements driver.Valuer for GORM: marshal to JSON for DB write.
func (g *Groups) Value() (driver.Value, error) {
if g == nil {
return nil, nil
}
return json.Marshal(g)
}

// Scan implements sql.Scanner for GORM: unmarshal from JSON on DB read.
func (g *Groups) Scan(value interface{}) error {
if value == nil {
*g = nil
return nil
}
b, ok := value.([]byte)
if !ok {
return errors.New("inventory.Groups: type assertion to []byte failed")
}
if len(b) == 0 {
*g = nil
return nil
}
return json.Unmarshal(b, g)
}

type Workloads struct {
Sap SapWorkload `json:"sap,omitempty"`
Ansible AnsibleWorkload `json:"ansible,omitempty"`
Expand Down
10 changes: 6 additions & 4 deletions base/models/models.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package models

import (
"app/base/inventory"
"time"

"github.com/google/uuid"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -116,17 +118,17 @@ type SystemInventory struct {
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
Arch *string
Bootc bool
Tags []byte `gorm:"column:tags"`
Created time.Time // set by trigger system_platform_insert_trigger
Workspaces pq.StringArray `gorm:"type:text[]"`
Tags []byte `gorm:"column:tags"`
Created time.Time // set by trigger system_platform_insert_trigger
Workspaces *inventory.Groups `gorm:"column:workspaces"`
StaleTimestamp *time.Time
StaleWarningTimestamp *time.Time
CulledTimestamp *time.Time
OSName *string
OSMajor *int16
OSMinor *int16
RhsmVersion *string
SubscriptionManagerID *string
SubscriptionManagerID *uuid.UUID
SapWorkload bool
SapWorkloadSIDs pq.StringArray `gorm:"type:text[];column:sap_workload_sids"`
AnsibleWorkload bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- index
DROP INDEX IF EXISTS system_inventory_workspaces_index;

-- drop the new workspaces field
ALTER TABLE system_inventory DROP COLUMN workspaces;

-- rename the old workspaces field to groups
ALTER TABLE system_inventory ADD COLUMN workspaces TEXT ARRAY CHECK (array_length(workspaces,1) > 0 or workspaces is null);

UPDATE system_inventory si
SET workspaces = ARRAY(SELECT jsonb_array_elements(ih.groups)->>'id')
FROM inventory.hosts ih
WHERE ih.id = si.inventory_id;

-- create index on new workspaces field
CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- index
DROP INDEX IF EXISTS system_inventory_workspaces_index;

-- rename the old workspaces field to groups
ALTER TABLE system_inventory DROP COLUMN workspaces;

-- add the new workspaces field
ALTER TABLE system_inventory ADD COLUMN workspaces JSONB;

-- copy the data from old inventory_hosts.groups to new workspaces field
UPDATE system_inventory si
SET workspaces = ih.groups
FROM inventory.hosts ih
WHERE ih.id = si.inventory_id;

-- create index on new workspaces field
CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);
4 changes: 2 additions & 2 deletions database_admin/schema/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations


INSERT INTO schema_migrations
VALUES (145, false);
VALUES (146, false);

-- ---------------------------------------------------------------------------
-- Functions
Expand Down Expand Up @@ -672,7 +672,6 @@ CREATE TABLE IF NOT EXISTS system_inventory
bootc BOOLEAN NOT NULL DEFAULT false,
tags JSONB NOT NULL,
created TIMESTAMPTZ NOT NULL,
workspaces TEXT ARRAY CHECK (array_length(workspaces,1) > 0 or workspaces is null), -- group IDs from system_platform.groups
stale_timestamp TIMESTAMPTZ,
stale_warning_timestamp TIMESTAMPTZ,
culled_timestamp TIMESTAMPTZ,
Expand All @@ -687,6 +686,7 @@ CREATE TABLE IF NOT EXISTS system_inventory
ansible_workload_controller_version TEXT CHECK (NOT empty(ansible_workload_controller_version)),
mssql_workload BOOLEAN NOT NULL DEFAULT false,
mssql_workload_version TEXT CHECK (NOT empty(mssql_workload_version)),
workspaces JSONB,
PRIMARY KEY (rh_account_id, id),
UNIQUE (rh_account_id, inventory_id)
) PARTITION BY HASH (rh_account_id);
Expand Down
2 changes: 1 addition & 1 deletion database_admin/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestSchemaCompatiblity(t *testing.T) {
writeTemp("/tmp", "schema-1-migrated.*.dump", migrated)
writeTemp("/tmp", "schema-2-fromscratch.*.dump", fromScratch)
}
assert.Equal(t, len(diff), 0)
assert.Equal(t, 0, len(diff))
}

func TestSchemaEmptyText(t *testing.T) {
Expand Down
Loading
Loading