Skip to content

Commit 464a345

Browse files
committed
RHINENG-25867: update old migrations that depend on cyndi
1 parent 958b812 commit 464a345

6 files changed

Lines changed: 71 additions & 12 deletions

database_admin/migrations/100_create_schema.up.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,4 +1088,12 @@ GRANT cyndi_reader to evaluator;
10881088
GRANT cyndi_reader to manager;
10891089
GRANT cyndi_reader TO vmaas_sync;
10901090

1091-
GRANT cyndi_admin to cyndi;
1091+
1092+
-- Grant cyndi the cyndi_admin role if cyndi exists
1093+
DO $$
1094+
BEGIN
1095+
IF EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'cyndi') THEN
1096+
GRANT cyndi_admin to cyndi;
1097+
END IF;
1098+
END
1099+
$$;

database_admin/migrations/146_system_inventory_new_workspaces_field.down.sql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ ALTER TABLE system_inventory DROP COLUMN workspaces;
77
-- rename the old workspaces field to groups
88
ALTER TABLE system_inventory ADD COLUMN workspaces TEXT ARRAY CHECK (array_length(workspaces,1) > 0 or workspaces is null);
99

10-
UPDATE system_inventory si
11-
SET workspaces = ARRAY(SELECT jsonb_array_elements(ih.groups)->>'id')
12-
FROM inventory.hosts ih
13-
WHERE ih.id = si.inventory_id;
10+
-- copy data from old inventory.hosts table if it exists
11+
DO $$
12+
BEGIN
13+
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'inventory' AND table_name = 'hosts') THEN
14+
UPDATE system_inventory si
15+
SET workspaces = ARRAY(SELECT jsonb_array_elements(ih.groups)->>'id')
16+
FROM inventory.hosts ih
17+
WHERE ih.id = si.inventory_id;
18+
END IF;
19+
END
20+
$$;
1421

1522
-- create index on new workspaces field
1623
CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);

database_admin/migrations/146_system_inventory_new_workspaces_field.up.sql

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ ALTER TABLE system_inventory DROP COLUMN workspaces;
77
-- add the new workspaces field
88
ALTER TABLE system_inventory ADD COLUMN workspaces JSONB;
99

10-
-- copy the data from old inventory_hosts.groups to new workspaces field
11-
UPDATE system_inventory si
12-
SET workspaces = ih.groups
13-
FROM inventory.hosts ih
14-
WHERE ih.id = si.inventory_id;
10+
-- copy the data from old inventory_hosts.groups (if it exists) to new workspaces field
11+
DO $$
12+
BEGIN
13+
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'inventory' AND table_name = 'hosts') THEN
14+
UPDATE system_inventory si
15+
SET workspaces = ih.groups
16+
FROM inventory.hosts ih
17+
WHERE ih.id = si.inventory_id;
18+
END IF;
19+
END
20+
$$;
1521

1622
-- create index on new workspaces field
1723
CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
SELECT 1 FROM inventory.hosts LIMIT 1;
1+
DO $$
2+
BEGIN
3+
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'inventory' AND table_name = 'hosts') THEN
4+
PERFORM 1 FROM inventory.hosts LIMIT 1;
5+
END IF;
6+
END
7+
$$;
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
-- just to trigger the migration
2-
SELECT 1 FROM inventory.hosts LIMIT 1;
2+
DO $$
3+
BEGIN
4+
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'inventory' AND table_name = 'hosts') THEN
5+
PERFORM 1 FROM inventory.hosts LIMIT 1;
6+
END IF;
7+
END
8+
$$;

database_admin/schema_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ func TestSchemaCompatiblity(t *testing.T) {
9494
assert.Equal(t, 0, len(diff))
9595
}
9696

97+
// checks that all migrations succeed without creating cyndi user or inventory.hosts table
98+
func TestMigrationsWithoutCyndi(t *testing.T) {
99+
utils.SkipWithoutDB(t)
100+
cfg := postgres.Config{
101+
DatabaseName: "patchman",
102+
SchemaName: "public",
103+
MigrationsTable: "schema_migrations",
104+
}
105+
database.Configure()
106+
107+
err := database.ExecFile("./schema/clear_db.sql")
108+
assert.NoError(t, err)
109+
110+
sqlDB, err := database.DB.DB()
111+
assert.NoError(t, err)
112+
driver, err := postgres.WithInstance(sqlDB, &cfg)
113+
assert.NoError(t, err)
114+
115+
m, err := migrate.NewWithDatabaseInstance("file://migrations",
116+
utils.FailIfEmpty(utils.CoreCfg.DBName, "DB_NAME"), driver)
117+
assert.NoError(t, err)
118+
119+
err = m.Up()
120+
assert.NoError(t, err)
121+
}
122+
97123
func TestSchemaEmptyText(t *testing.T) {
98124
utils.SkipWithoutDB(t)
99125
database.Configure()

0 commit comments

Comments
 (0)