set role postgres;
--
CREATE SCHEMA IF NOT EXISTS gpu;
--ALTER SCHEMA gpu OWNER TO dbo;
GRANT USAGE ON SCHEMA gpu TO nifi;

GRANT ALL ON ALL TABLES IN SCHEMA gpu TO nifi;
GRANT ALL ON ALL SEQUENCES IN SCHEMA gpu TO nifi;

--ALTER DEFAULT PRIVILEGES IN SCHEMA gpu GRANT ALL ON TABLES TO nifi;
--ALTER DEFAULT PRIVILEGES IN SCHEMA gpu GRANT ALL ON SEQUENCES TO nifi;

ALTER DEFAULT PRIVILEGES FOR ROLE dbo, postgres IN SCHEMA gpu GRANT ALL ON TABLES TO nifi;
ALTER DEFAULT PRIVILEGES FOR ROLE dbo, postgres IN SCHEMA gpu GRANT ALL ON SEQUENCES TO nifi;

--SET ROLE dbo;

DROP TABLE IF EXISTS gpu.vm;
CREATE TABLE gpu.vm ( 
	 objUUID uuid NOT NULL PRIMARY KEY 
	,groupUUID uuid NULL 
	,objId varchar(255) NULL
	,ClientID varchar(255) NULL	-- WZ	
	,VCD varchar(255) NULL		
	,Org varchar(255) NULL		
	,OrgVDC varchar(255) NULL		
	,vCenter varchar(255) NULL		
	,GPU varchar(255) NULL	
	,dt_load timestamptz NOT NULL 	
);
ALTER TABLE gpu.vm OWNER TO dbo;
/*
{
"type": "record",
"name": "gpu.vm",
"namespace": "org.apache.nifi",
"fields": [ 
	{"name":"objUUID", "type":{"type": "string", "logicalType": "uuid"}},	
	{"name":"groupUUID", "type":[{"type": "string", "logicalType": "uuid"} ,"null"]},
	{"name":"objId", "type":["string" ,"null"]},
	{"name":"ClientID", "type":["int" ,"null"]},
	{"name":"VCD", "type":["string" ,"null"]},
	{"name":"Org", "type":["string" ,"null"]},
	{"name":"OrgVDC", "type":["string" ,"null"]},
	{"name":"vCenter", "type":["string" ,"null"]},
	{"name":"GPU", "type":["string" ,"null"]},
	{"name":"VCD", "type":["string" ,"null"]},
	{"name":"dt_load","type":{"type": "long", "logicalType": "local-timestamp-millis"}}
]
}
*/

DROP TABLE IF EXISTS gpu.vm_power_state;
CREATE TABLE gpu.vm_power_state (
	 objId varchar(255) NULL
	,ts timestamptz NOT NULL -- без таймштампа загрузки	
	,value int NULL		
	,CONSTRAINT PK_vm_power_state PRIMARY KEY(objId, ts)
);
ALTER TABLE gpu.vm_power_state OWNER TO dbo;
/*
{
"type": "record",
"name": "gpu.vm_power_state,
"namespace": "org.apache.nifi",
"fields": [ 
	{"name":"objId", "type":["string" ,"null"]},
	{"name":"ts","type":{"type": "long", "logicalType": "local-timestamp-millis"}},
	{"name":"value", "type":["int" ,"null"]}
]
}
*/