Files
threshold/internal/host/static/query.go
T
Michael Gaffney 9ca8a4ec20 Host set members (#301)
* Update host set member test

* Add ListSetMembers method signature and doc

* Move repository methods for host set members to new file

* Add TestSetMembers

* Implement ListSetMembers

* Return hosts from ListSetMembers

* Implement AddSetMembers

* Implement DeleteSetMembers

* Use sql.QueryContext not sql.Query

* Implement SetSetMembers

* Refactor: extract getHosts method

* Refactor: extract createMembers method

* Refactor: extract common functions

* Return correct count of changed

In SetSetMembers, retrieving the current list of hosts in the set
happens after the transaction to modify the set has completed. This
retrieval can result in an error but the number of modified rows is not
effected.

* Make list set members an unexported method

* Get hosts within the same transaction as mutations of the set

* Return hosts of set from LookupSet method

* Return hosts of set from UpdateSet method

* Remove listSetMembers

* Fix merge conflict

* Add NOTE and TODOs about using new pattern in "SetMembers" methods
2020-08-26 12:31:18 -04:00

47 lines
932 B
Go

package static
const (
setChangesQuery = `
with
final_hosts (host_id) as (
-- returns the SET list
select public_id
from static_host
where public_id in (%s)
),
current_hosts (host_id) as (
-- returns the current list
select host_id
from static_host_set_member
where set_id = $1
),
keep_hosts (host_id) as (
-- returns the KEEP list
select host_id
from current_hosts
where host_id in (select * from final_hosts)
),
delete_hosts (host_id) as (
-- returns the DELETE list
select host_id
from current_hosts
where host_id not in (select * from final_hosts)
),
insert_hosts (host_id) as (
-- returns the ADD list
select host_id
from final_hosts
where host_id not in (select * from keep_hosts)
),
final (action, host_id) as (
select 'delete', host_id
from delete_hosts
union
select 'add', host_id
from insert_hosts
)
select * from final
order by action, host_id;
`
)