[server] Change entity_data.id from uuid to string

This commit is contained in:
Neeraj Gupta
2024-08-16 10:30:18 +05:30
parent 555f23a519
commit 9f043eba24
6 changed files with 28 additions and 21 deletions

View File

@@ -1,9 +1,5 @@
package userentity
import (
"github.com/google/uuid"
)
type EntityType string
const (
@@ -23,7 +19,7 @@ type EntityKey struct {
// EntityData represents a single UserEntity
type EntityData struct {
ID uuid.UUID `json:"id" binding:"required"`
ID string `json:"id" binding:"required"`
UserID int64 `json:"userID" binding:"required"`
Type EntityType `json:"type" binding:"required"`
EncryptedData *string `json:"encryptedData" binding:"required"`
@@ -54,7 +50,7 @@ type EntityDataRequest struct {
// UpdateEntityDataRequest updates the current entity
type UpdateEntityDataRequest struct {
ID uuid.UUID `json:"id" binding:"required"`
ID string `json:"id" binding:"required"`
Type EntityType `json:"type" binding:"required"`
EncryptedData string `json:"encryptedData" binding:"required"`
Header string `json:"header" binding:"required"`

View File

@@ -0,0 +1,9 @@
BEGIN;
ALTER TABLE entity_data ADD COLUMN id_uuid UUID;
UPDATE entity_data SET id_uuid = id::UUID;
ALTER TABLE entity_data ALTER COLUMN id_uuid SET NOT NULL;
ALTER TABLE entity_data DROP CONSTRAINT entity_data_pkey;
ALTER TABLE entity_data DROP COLUMN IF EXISTS id;
ALTER TABLE entity_data RENAME COLUMN id_uuid TO id;
ALTER TABLE entity_data ADD PRIMARY KEY (id);
COMMIT;

View File

@@ -0,0 +1,9 @@
BEGIN;
ALTER TABLE entity_data ADD COLUMN id_text TEXT;
UPDATE entity_data SET id_text = id::TEXT;
ALTER TABLE entity_data ALTER COLUMN id_text SET NOT NULL;
ALTER TABLE entity_data DROP CONSTRAINT entity_data_pkey;
ALTER TABLE entity_data DROP COLUMN IF EXISTS id;
ALTER TABLE entity_data RENAME COLUMN id_text TO id;
ALTER TABLE entity_data ADD PRIMARY KEY (id);
END;

View File

@@ -10,7 +10,6 @@ import (
"github.com/ente-io/museum/pkg/utils/handler"
"github.com/ente-io/stacktrace"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// UserEntityHandler expose request handlers for various operations on user entity
@@ -84,12 +83,8 @@ func (h *UserEntityHandler) UpdateEntity(c *gin.Context) {
// DeleteEntity...
func (h *UserEntityHandler) DeleteEntity(c *gin.Context) {
id, err := uuid.Parse(c.Query("id"))
if err != nil {
handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, "failed to find id"))
return
}
_, err = h.Controller.Delete(c, id)
id := c.Query("id")
_, err := h.Controller.Delete(c, id)
if err != nil {
handler.Error(c, stacktrace.Propagate(err, "Failed to delete DeleteEntity"))
return

View File

@@ -5,8 +5,6 @@ import (
"github.com/ente-io/museum/pkg/repo/userentity"
"github.com/ente-io/museum/pkg/utils/auth"
"github.com/ente-io/stacktrace"
"github.com/google/uuid"
"github.com/gin-gonic/gin"
)
@@ -52,7 +50,7 @@ func (c *Controller) UpdateEntity(ctx *gin.Context, req model.UpdateEntityDataRe
}
// Delete...
func (c *Controller) Delete(ctx *gin.Context, entityID uuid.UUID) (bool, error) {
func (c *Controller) Delete(ctx *gin.Context, entityID string) (bool, error) {
userID := auth.GetUserID(ctx.Request.Header)
return c.Repo.Delete(ctx, userID, entityID)
}

View File

@@ -13,7 +13,7 @@ import (
)
// Create inserts a new entry
func (r *Repository) Create(ctx context.Context, userID int64, entry model.EntityDataRequest) (uuid.UUID, error) {
func (r *Repository) Create(ctx context.Context, userID int64, entry model.EntityDataRequest) (string, error) {
id := uuid.New()
err := r.DB.QueryRow(`INSERT into entity_data(
id,
@@ -28,12 +28,12 @@ func (r *Repository) Create(ctx context.Context, userID int64, entry model.Entit
entry.Header). // $5 header
Scan(&id)
if err != nil {
return id, stacktrace.Propagate(err, "failed to create enity data")
return id.String(), stacktrace.Propagate(err, "failed to create enity data")
}
return id, nil
return id.String(), nil
}
func (r *Repository) Get(ctx context.Context, userID int64, id uuid.UUID) (*model.EntityData, error) {
func (r *Repository) Get(ctx context.Context, userID int64, id string) (*model.EntityData, error) {
res := model.EntityData{}
row := r.DB.QueryRowContext(ctx, `SELECT
id, user_id, type, encrypted_data, header, is_deleted, created_at, updated_at
@@ -50,7 +50,7 @@ func (r *Repository) Get(ctx context.Context, userID int64, id uuid.UUID) (*mode
return &res, nil
}
func (r *Repository) Delete(ctx context.Context, userID int64, id uuid.UUID) (bool, error) {
func (r *Repository) Delete(ctx context.Context, userID int64, id string) (bool, error) {
_, err := r.DB.ExecContext(ctx,
`UPDATE entity_data SET is_deleted = true, encrypted_data = NULL, header = NULL where id=$1 and user_id = $2`,
id, userID)