Commit 1a2402b6 authored by Mo Firouz's avatar Mo Firouz
Browse files

Improve group SQL query with type information

parent b8b20846
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -5,6 +5,10 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Fixed

- Improve group SQL query with type information.

## [0.11.0] - 2017-02-09
### Added
- Add `--verbose` flag to enable debug logs in server.
+10 −6
Original line number Diff line number Diff line
@@ -798,7 +798,11 @@ func (p *pipeline) groupUserAdd(l zap.Logger, session *session, envelope *Envelo
	}
	defer func() {
		if err != nil {
			if _, ok := err.(*pq.Error); ok {
				logger.Error("Could not add user to group", zap.Error(err))
			} else {
				logger.Warn("Could not add user to group", zap.Error(err))
			}
			err = tx.Rollback()
			if err != nil {
				logger.Error("Could not rollback transaction", zap.Error(err))
@@ -828,16 +832,16 @@ func (p *pipeline) groupUserAdd(l zap.Logger, session *session, envelope *Envelo
INSERT INTO group_edge (source_id, position, updated_at, destination_id, state)
SELECT data.id, data.position, data.updated_at, data.destination, data.state
FROM (
  SELECT $1 AS id, $2 AS position, $2 AS updated_at, $3 AS destination, 1 AS state
  SELECT $1::BYTES AS id, $2::INT AS position, $2::INT AS updated_at, $3::BYTES AS destination, 1 AS state
  UNION ALL
  SELECT $3 AS id, $2 AS position, $2 AS updated_at, $1 AS destination, 1 AS state
  SELECT $3::BYTES AS id, $2::INT AS position, $2::INT AS updated_at, $1::BYTES AS destination, 1 AS state
) AS data
WHERE
	EXISTS (SELECT source_id FROM group_edge WHERE source_id = $1 AND destination_id = $4 AND state = 0)
  EXISTS (SELECT source_id FROM group_edge WHERE source_id = $1::BYTES AND destination_id = $4::BYTES AND state = 0)
AND
	EXISTS (SELECT id FROM groups WHERE id = $1 AND disabled_at = 0)
  EXISTS (SELECT id FROM groups WHERE id = $1::BYTES AND disabled_at = 0)
ON CONFLICT (source_id, destination_id)
DO UPDATE SET state = 1, updated_at = $2`,
DO UPDATE SET state = 1, updated_at = $2::INT`,
		groupID.Bytes(), nowMs(), userID.Bytes(), session.userID.Bytes())

	if err != nil {