Skip to content

Commit

Permalink
MF-270 - Revert group relations table name
Browse files Browse the repository at this point in the history
Signed-off-by: zzokki81 <zoran.rebic@outlook.com>
  • Loading branch information
zzokki81 committed Aug 16, 2023
1 parent 070745c commit 591c1c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
28 changes: 14 additions & 14 deletions things/postgres/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/jackc/pgx/v5/pgconn"
)

var groupIDFkeyy = "thing_relations_group_id_fkey"
var groupIDFkeyy = "group_relations_group_id_fkey"

var _ things.GroupRepository = (*groupRepository)(nil)

Expand Down Expand Up @@ -243,16 +243,16 @@ func (gr groupRepository) RetrieveGroupThings(ctx context.Context, groupID strin
case true:
q = fmt.Sprintf(`SELECT t.id, t.owner, t.name, t.metadata, t.key
FROM things t
WHERE t.id NOT IN (SELECT gr.thing_id FROM thing_relations gr WHERE gr.group_id = :group_id)
WHERE t.id NOT IN (SELECT gr.member_id FROM group_relations gr WHERE gr.group_id = :group_id)
%s %s;`, mq, olq)
qc = fmt.Sprintf(`SELECT COUNT(*) FROM things t
WHERE t.id NOT IN (SELECT gr.thing_id FROM thing_relations gr WHERE gr.group_id = :group_id) %s;`, mq)
WHERE t.id NOT IN (SELECT gr.member_id FROM group_relations gr WHERE gr.group_id = :group_id) %s;`, mq)
default:
q = fmt.Sprintf(`SELECT t.id, t.owner, t.name, t.metadata, t.key
FROM thing_relations gr, things t
WHERE gr.group_id = :group_id and gr.thing_id = t.id
FROM group_relations gr, things t
WHERE gr.group_id = :group_id and gr.member_id = t.id
%s %s;`, mq, olq)
qc = fmt.Sprintf(`SELECT COUNT(*) FROM thing_relations gr WHERE gr.group_id = :group_id %s;`, mq)
qc = fmt.Sprintf(`SELECT COUNT(*) FROM group_relations gr WHERE gr.group_id = :group_id %s;`, mq)
}

params, err := toDBGroupThingsPage("", groupID, pm)
Expand Down Expand Up @@ -367,9 +367,9 @@ func (gr groupRepository) RetrieveGroupChannels(ctx context.Context, groupID str
}

func (gr groupRepository) RetrieveThingMembership(ctx context.Context, thingID string) (string, error) {
q := `SELECT group_id FROM thing_relations WHERE thing_id = :thing_id;`
q := `SELECT group_id FROM group_relations WHERE member_id = :member_id;`

params := map[string]interface{}{"thing_id": thingID}
params := map[string]interface{}{"member_id": thingID}

rows, err := gr.db.NamedQueryContext(ctx, q, params)
if err != nil {
Expand Down Expand Up @@ -409,7 +409,7 @@ func (gr groupRepository) RetrieveChannelMembership(ctx context.Context, channel
}

func (gr groupRepository) RetrieveAllThingRelations(ctx context.Context) ([]things.GroupThingRelation, error) {
q := `SELECT group_id, thing_id, created_at, updated_at FROM thing_relations`
q := `SELECT group_id, member_id, created_at, updated_at FROM group_relations`

rows, err := gr.db.NamedQueryContext(ctx, q, map[string]interface{}{})
if err != nil {
Expand Down Expand Up @@ -440,8 +440,8 @@ func (gr groupRepository) AssignThing(ctx context.Context, groupID string, ids .
return errors.Wrap(things.ErrAssignGroupThing, err)
}

qIns := `INSERT INTO thing_relations (group_id, thing_id, created_at, updated_at)
VALUES(:group_id, :thing_id, :created_at, :updated_at)`
qIns := `INSERT INTO group_relations (group_id, member_id, created_at, updated_at)
VALUES(:group_id, :member_id, :created_at, :updated_at)`

for _, id := range ids {
created := time.Now()
Expand Down Expand Up @@ -484,7 +484,7 @@ func (gr groupRepository) UnassignThing(ctx context.Context, groupID string, ids
return errors.Wrap(things.ErrUnassignGroupThing, err)
}

qDel := `DELETE from thing_relations WHERE group_id = :group_id AND thing_id = :thing_id`
qDel := `DELETE from group_relations WHERE group_id = :group_id AND member_id = :member_id`

for _, id := range ids {
dbt, err := toDBThingRelation(id, groupID)
Expand Down Expand Up @@ -694,7 +694,7 @@ type dbGroup struct {

type dbGroupThingsPage struct {
GroupID string `db:"group_id"`
ThingID string `db:"thing_id"`
ThingID string `db:"member_id"`
Metadata dbMetadata `db:"metadata"`
Limit uint64 `db:"limit"`
Offset uint64 `db:"offset"`
Expand Down Expand Up @@ -736,7 +736,7 @@ func toGroup(dbu dbGroup) (things.Group, error) {

type dbThingRelation struct {
GroupID sql.NullString `db:"group_id"`
ThingID sql.NullString `db:"thing_id"`
ThingID sql.NullString `db:"member_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Expand Down
2 changes: 1 addition & 1 deletion things/postgres/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ func TestRetrieveAllGroupRelations(t *testing.T) {
}

func cleanUp(t *testing.T) {
_, err := db.Exec("delete from thing_relations")
_, err := db.Exec("delete from group_relations")
require.Nil(t, err, fmt.Sprintf("clean relations unexpected error: %s", err))
_, err = db.Exec("delete from channel_relations")
require.Nil(t, err, fmt.Sprintf("clean relations unexpected error: %s", err))
Expand Down
10 changes: 5 additions & 5 deletions things/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ func migrateDB(db *sqlx.DB) error {
updated_at TIMESTAMPTZ,
UNIQUE (owner_id, name)
)`,
`CREATE TABLE IF NOT EXISTS thing_relations (
thing_id UUID NOT NULL,
`CREATE TABLE IF NOT EXISTS group_relations (
member_id UUID NOT NULL,
group_id UUID NOT NULL,
created_at TIMESTAMPTZ,
updated_at TIMESTAMPTZ,
FOREIGN KEY (group_id) REFERENCES groups (id),
PRIMARY KEY (thing_id, group_id)
PRIMARY KEY (member_id, group_id)
)`,
`CREATE TABLE IF NOT EXISTS channel_relations (
channel_id UUID UNIQUE NOT NULL,
Expand All @@ -131,14 +131,14 @@ func migrateDB(db *sqlx.DB) error {
},
Down: []string{
"DROP TABLE groups",
"DROP TABLE thing_relations",
"DROP TABLE group_relations",
"DROP TABLE channel_relations",
},
},
{
Id: "things_6",
Up: []string{
`ALTER TABLE thing_relations ADD CONSTRAINT thing_id_unique UNIQUE (thing_id);`,
`ALTER TABLE group_relations ADD CONSTRAINT member_id_unique UNIQUE (member_id);`,
},
},
},
Expand Down

0 comments on commit 591c1c9

Please sign in to comment.