Skip to content

Commit 0166ac3

Browse files
committed
wip
1 parent 7fcf3fc commit 0166ac3

File tree

8 files changed

+554
-12
lines changed

8 files changed

+554
-12
lines changed

api/store/pg/entity/device.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Device struct {
3131
Latitude float64 `bun:"latitude,type:numeric"`
3232

3333
Namespace *Namespace `bun:"rel:belongs-to,join:namespace_id=id"`
34+
Tags []*Tag `bun:"rel:has-many,join:id=device_id,join:namespace_id=namespace_id,m2m:device_tags,on_delete:cascade"`
3435
}
3536

3637
func DeviceFromModel(model *models.Device) *Device {
@@ -43,6 +44,7 @@ func DeviceFromModel(model *models.Device) *Device {
4344
Status: string(model.Status),
4445
Name: model.Name,
4546
PublicKey: model.PublicKey,
47+
Tags: []*Tag{},
4648
}
4749

4850
if model.DisconnectedAt != nil {
@@ -66,6 +68,13 @@ func DeviceFromModel(model *models.Device) *Device {
6668
device.Platform = model.Info.Platform
6769
}
6870

71+
if len(model.Tags) > 0 {
72+
device.Tags = make([]*Tag, len(model.Tags))
73+
for i, t := range model.Tags {
74+
device.Tags[i] = TagFromModel(&t)
75+
}
76+
}
77+
6978
return device
7079
}
7180

@@ -83,7 +92,9 @@ func DeviceToModel(entity *Device) *models.Device {
8392
Namespace: entity.Namespace.Name,
8493
DisconnectedAt: nil,
8594
RemoteAddr: "",
86-
Tags: []string{},
95+
Taggable: models.Taggable{
96+
Tags: []models.Tag{},
97+
},
8798
Position: &models.DevicePosition{
8899
Longitude: entity.Longitude,
89100
Latitude: entity.Latitude,
@@ -105,5 +116,12 @@ func DeviceToModel(entity *Device) *models.Device {
105116
device.DisconnectedAt = &disconnectedAt
106117
}
107118

119+
if len(entity.Tags) > 0 {
120+
device.Tags = make([]models.Tag, len(entity.Tags))
121+
for i, t := range entity.Tags {
122+
device.Tags[i] = *TagToModel(t)
123+
}
124+
}
125+
108126
return device
109127
}

api/store/pg/entity/public-key.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,33 @@ type PublicKey struct {
1717
UpdatedAt time.Time `bun:"updated_at"`
1818
Name string `bun:"name"`
1919
Data []byte `bun:"data,type:bytea"`
20+
21+
Tags []*Tag `bun:"rel:has-many,join:id=public_key_id,join:namespace_id=namespace_id,m2m:public_key_tags,on_delete:cascade"`
2022
}
2123

2224
func PublicKeyFromModel(model *models.PublicKey) *PublicKey {
23-
return &PublicKey{
25+
publicKey := &PublicKey{
2426
NamespaceID: model.TenantID,
2527
Fingerprint: model.Fingerprint,
2628
CreatedAt: model.CreatedAt,
2729
UpdatedAt: time.Time{},
2830
Name: model.PublicKeyFields.Name,
2931
Data: model.Data,
32+
Tags: []*Tag{},
33+
}
34+
35+
if len(model.Filter.Tags) > 0 {
36+
publicKey.Tags = make([]*Tag, len(model.Filter.Tags))
37+
for i, t := range model.Filter.Tags {
38+
publicKey.Tags[i] = TagFromModel(&t)
39+
}
3040
}
41+
42+
return publicKey
3143
}
3244

3345
func PublicKeyToModel(entity *PublicKey) *models.PublicKey {
34-
return &models.PublicKey{
46+
publicKey := &models.PublicKey{
3547
TenantID: entity.NamespaceID,
3648
Fingerprint: entity.Fingerprint,
3749
Data: entity.Data,
@@ -41,8 +53,19 @@ func PublicKeyToModel(entity *PublicKey) *models.PublicKey {
4153
Username: "",
4254
Filter: models.PublicKeyFilter{
4355
Hostname: "",
44-
Tags: []string{},
56+
Taggable: models.Taggable{
57+
Tags: []models.Tag{},
58+
},
4559
},
4660
},
4761
}
62+
63+
if len(entity.Tags) > 0 {
64+
publicKey.Filter.Tags = make([]models.Tag, len(entity.Tags))
65+
for i, t := range entity.Tags {
66+
publicKey.Filter.Tags[i] = *TagToModel(t)
67+
}
68+
}
69+
70+
return publicKey
4871
}

api/store/pg/entity/tag.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package entity
2+
3+
import (
4+
"time"
5+
6+
"github.com/shellhub-io/shellhub/pkg/models"
7+
"github.com/uptrace/bun"
8+
)
9+
10+
type Tag struct {
11+
bun.BaseModel `bun:"table:tags"`
12+
13+
ID string `bun:"id,pk"`
14+
NamespaceID string `bun:"namespace_id"`
15+
Name string `bun:"name"`
16+
CreatedAt time.Time `bun:"created_at"`
17+
UpdatedAt time.Time `bun:"updated_at"`
18+
19+
Namespace *Namespace `bun:"rel:belongs-to,join:namespace_id=id"`
20+
}
21+
22+
func TagFromModel(model *models.Tag) *Tag {
23+
return &Tag{
24+
ID: model.ID,
25+
NamespaceID: model.TenantID,
26+
Name: model.Name,
27+
CreatedAt: model.CreatedAt,
28+
UpdatedAt: model.UpdatedAt,
29+
}
30+
}
31+
32+
func TagToModel(entity *Tag) *models.Tag {
33+
return &models.Tag{
34+
ID: entity.ID,
35+
TenantID: entity.NamespaceID,
36+
Name: entity.Name,
37+
CreatedAt: entity.CreatedAt,
38+
UpdatedAt: entity.UpdatedAt,
39+
}
40+
}

api/store/pg/migrations/004_create_devices_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func migration004Up(ctx context.Context, db *bun.DB) error {
4545
_, err = db.NewCreateTable().
4646
Model(deviceTable).
4747
IfNotExists().
48-
ForeignKey(`("namespace_id") REFERENCES namespaces("id")`).
48+
ForeignKey(`("namespace_id") REFERENCES namespaces("id") ON DELETE CASCADE`).
4949
Exec(ctx)
5050
if err != nil {
5151
log.WithError(err).Error("failed to apply migration 004")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/uptrace/bun"
9+
)
10+
11+
func init() {
12+
migrations.MustRegister(migration008Up, migration008Down)
13+
}
14+
15+
func migration008Up(ctx context.Context, db *bun.DB) error {
16+
table := &struct {
17+
bun.BaseModel `bun:"table:tags"`
18+
ID string `bun:"id,type:uuid,pk"`
19+
NamespaceID string `bun:"namespace_id,type:uuid,notnull"`
20+
Name string `bun:"name,type:varchar,notnull"`
21+
CreatedAt time.Time `bun:"created_at,type:timestamptz,notnull"`
22+
UpdatedAt time.Time `bun:"updated_at,type:timestamptz,notnull"`
23+
}{}
24+
25+
if _, err := db.NewCreateTable().
26+
Model(table).
27+
IfNotExists().
28+
ForeignKey(`("namespace_id") REFERENCES namespaces("id") ON DELETE CASCADE`).
29+
Exec(ctx); err != nil {
30+
log.WithError(err).Error("failed to apply migration 008")
31+
32+
return err
33+
}
34+
35+
_, err := db.NewCreateIndex().
36+
Model((*struct {
37+
bun.BaseModel `bun:"table:tags"`
38+
})(nil)).
39+
Index("tags_namespace_id_name_unique").
40+
Column("namespace_id", "name").
41+
Unique().
42+
Exec(ctx)
43+
if err != nil {
44+
log.WithError(err).Error("failed to apply migration 008")
45+
46+
return err
47+
}
48+
49+
return nil
50+
}
51+
52+
func migration008Down(ctx context.Context, db *bun.DB) error {
53+
_, err := db.ExecContext(ctx, "DROP TABLE IF EXISTS tags")
54+
55+
return err
56+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/uptrace/bun"
9+
)
10+
11+
func init() {
12+
migrations.MustRegister(migration009Up, migration009Down)
13+
}
14+
15+
func migration009Up(ctx context.Context, db *bun.DB) error {
16+
deviceTagsTable := &struct {
17+
bun.BaseModel `bun:"table:device_tags"`
18+
DeviceID string `bun:"device_id,type:varchar,pk"`
19+
TagID string `bun:"tag_id,type:uuid,pk"`
20+
CreatedAt time.Time `bun:"created_at,type:timestamptz,notnull"`
21+
}{}
22+
23+
if _, err := db.NewCreateTable().
24+
Model(deviceTagsTable).
25+
IfNotExists().
26+
ForeignKey(`("device_id") REFERENCES devices("id") ON DELETE CASCADE`).
27+
ForeignKey(`("tag_id") REFERENCES tags("id") ON DELETE CASCADE`).
28+
Exec(ctx); err != nil {
29+
log.WithError(err).Error("failed to create device_tags table in migration 009")
30+
31+
return err
32+
}
33+
34+
if _, err := db.NewCreateIndex().
35+
Model(deviceTagsTable).
36+
Index("device_tags_device_id").
37+
Column("device_id").
38+
Exec(ctx); err != nil {
39+
log.WithError(err).Error("failed to create device_id index for device_tags in migration 009")
40+
41+
return err
42+
}
43+
44+
if _, err := db.NewCreateIndex().
45+
Model(deviceTagsTable).
46+
Index("device_tags_tag_id").
47+
Column("tag_id").
48+
Exec(ctx); err != nil {
49+
log.WithError(err).Error("failed to create tag_id index for device_tags in migration 009")
50+
51+
return err
52+
}
53+
54+
return nil
55+
}
56+
57+
func migration009Down(ctx context.Context, db *bun.DB) error {
58+
_, err := db.ExecContext(ctx, `
59+
DROP TABLE IF EXISTS public_key_tags;
60+
DROP TABLE IF EXISTS device_tags;
61+
`)
62+
63+
return err
64+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/uptrace/bun"
9+
)
10+
11+
func init() {
12+
migrations.MustRegister(migration010Up, migration010Down)
13+
}
14+
15+
func migration010Up(ctx context.Context, db *bun.DB) error {
16+
publicKeyTagsTable := &struct {
17+
bun.BaseModel `bun:"table:public_key_tags"`
18+
PublicKeyID string `bun:"public_key_id,type:uuid,pk"`
19+
TagID string `bun:"tag_id,type:uuid,pk"`
20+
CreatedAt time.Time `bun:"created_at,type:timestamptz,notnull"`
21+
}{}
22+
23+
if _, err := db.NewCreateTable().
24+
Model(publicKeyTagsTable).
25+
IfNotExists().
26+
ForeignKey(`("public_key_id") REFERENCES public_keys("id") ON DELETE CASCADE`).
27+
ForeignKey(`("tag_id") REFERENCES tags("id") ON DELETE CASCADE`).
28+
Exec(ctx); err != nil {
29+
log.WithError(err).Error("failed to create public_key_tags table in migration 010")
30+
31+
return err
32+
}
33+
34+
if _, err := db.NewCreateIndex().
35+
Model(publicKeyTagsTable).
36+
Index("public_key_tags_public_key_id").
37+
Column("public_key_id").
38+
Exec(ctx); err != nil {
39+
log.WithError(err).Error("failed to create public_key_id index for public_key_tags in migration 010")
40+
41+
return err
42+
}
43+
44+
if _, err := db.NewCreateIndex().
45+
Model(publicKeyTagsTable).
46+
Index("public_key_tags_tag_id").
47+
Column("tag_id").
48+
Exec(ctx); err != nil {
49+
log.WithError(err).Error("failed to create tag_id index for public_key_tags in migration 010")
50+
51+
return err
52+
}
53+
54+
return nil
55+
}
56+
57+
func migration010Down(ctx context.Context, db *bun.DB) error {
58+
_, err := db.ExecContext(ctx, "DROP TABLE IF EXISTS public_key_tags")
59+
60+
return err
61+
}

0 commit comments

Comments
 (0)