diff --git a/packages/postgresql/postgresql.go b/packages/postgresql/postgresql.go index 600adc0..6d99dd6 100644 --- a/packages/postgresql/postgresql.go +++ b/packages/postgresql/postgresql.go @@ -71,6 +71,69 @@ func Init(ctx context.Context) { if err != nil { panic(err) } + + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS hubs () + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + creator_id UUID NOT NULL REFERENCES users(id), + created_at TIMESTAMP NOT NULL DEFAULT NOW() + `) + if err != nil { + panic(err) + } + + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS hub_channel_groups () + hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE, + channel_group_id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + name TEXT NOT NULL, + rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295), + `) + if err != nil { + panic(err) + } + + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS hub_channels () + hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE, + channel_id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + name TEXT NOT NULL, + parent_group_id UUID REFERENCES hub_channel_groups(id) ON DELETE CASCADE, + `) + if err != nil { + panic(err) + } + + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS hub_users () + hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE, + user_id UUID PRIMARY KEY NOT NULL REFERENCES users(id) ON DELETE CASCADE, + name TEXT NOT NULL, + rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295), + created_at TIMESTAMP NOT NULL DEFAULT NOW() + `) + if err != nil { + panic(err) + } + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS hub_user_roles () + user_id UUID PRIMARY KEY NOT NULL REFERENCES users(id) ON DELETE CASCADE, + role_id SMALLINT NOT NULL REFERENCES hub_roles(role_id) + `) + if err != nil { + panic(err) + } + + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS hub_roles () + hub_id UUID PRIMARY KEY NOT NULL REFERENCES hubs(id) ON DELETE CASCADE, + role_id SMALLINT NOT NULL DEFAULT 0, + name TEXT NOT NULL, + rgba BIGINT NOT NULL DEFAULT 0 CHECK (rgba BETWEEN 0 AND 4294967295), + `) + if err != nil { + panic(err) + } } func UserSave(ctx context.Context, user *types.User) error { diff --git a/packages/types/types.go b/packages/types/types.go index 73ea0be..33e6723 100644 --- a/packages/types/types.go +++ b/packages/types/types.go @@ -167,6 +167,18 @@ type Hub struct { Creator uuid.UUID `json:"creator"` } +type HubChannelGroup struct { + Id uuid.UUID `json:"id"` + Name string `json:"name"` + Color string `json:"color"` +} + +type HubChannel struct { + Id uuid.UUID `json:"id"` + Name uuid.UUID `json:"name"` + ParentGroupId uuid.UUID `json:"parentGroupId"` +} + type HubUser struct { Id uuid.UUID `json:"id"` Username string `json:"username"`