Skip to content

Commit

Permalink
Update create queries to only createdAt when record is deleted (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyphu authored Oct 9, 2023
1 parent 4e73db3 commit 74d98f3
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/authz/objecttype/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (repo MySQLRepository) Create(ctx context.Context, model Model) (int64, err
) VALUES (?, ?)
ON DUPLICATE KEY UPDATE
definition = ?,
createdAt = CURRENT_TIMESTAMP(6),
createdAt = IF(objectType.deletedAt IS NULL, objectType.createdAt, CURRENT_TIMESTAMP(6)),
updatedAt = CURRENT_TIMESTAMP(6),
deletedAt = NULL
`,
Expand Down
5 changes: 4 additions & 1 deletion pkg/authz/objecttype/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func (repo PostgresRepository) Create(ctx context.Context, model Model) (int64,
) VALUES (?, ?)
ON CONFLICT (type_id) DO UPDATE SET
definition = ?,
created_at = CURRENT_TIMESTAMP(6),
created_at = CASE
WHEN object_type.deleted_at IS NULL THEN object_type.created_at
ELSE CURRENT_TIMESTAMP(6)
END,
updated_at = CURRENT_TIMESTAMP(6),
deleted_at = NULL
RETURNING id
Expand Down
2 changes: 1 addition & 1 deletion pkg/authz/objecttype/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (repo SQLiteRepository) Create(ctx context.Context, model Model) (int64, er
) VALUES (?, ?, ?, ?)
ON CONFLICT (typeId) DO UPDATE SET
definition = ?,
createdAt = ?,
createdAt = IIF(objectType.deletedAt IS NULL, objectType.createdAt, ?),
updatedAt = ?,
deletedAt = NULL
RETURNING id
Expand Down
2 changes: 1 addition & 1 deletion pkg/authz/warrant/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (repo MySQLRepository) Create(ctx context.Context, model Model) (int64, err
policyHash
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
createdAt = CURRENT_TIMESTAMP(6),
createdAt = IF(warrant.deletedAt IS NULL, warrant.createdAt, CURRENT_TIMESTAMP(6)),
updatedAt = CURRENT_TIMESTAMP(6),
deletedAt = NULL
`,
Expand Down
5 changes: 4 additions & 1 deletion pkg/authz/warrant/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func (repo PostgresRepository) Create(ctx context.Context, model Model) (int64,
policy_hash
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (object_type, object_id, relation, subject_type, subject_id, subject_relation, policy_hash) DO UPDATE SET
created_at = CURRENT_TIMESTAMP(6),
created_at = CASE
WHEN warrant.deleted_at IS NULL THEN warrant.created_at
ELSE CURRENT_TIMESTAMP(6)
END,
updated_at = CURRENT_TIMESTAMP(6),
deleted_at = NULL
RETURNING id
Expand Down
2 changes: 1 addition & 1 deletion pkg/authz/warrant/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (repo SQLiteRepository) Create(ctx context.Context, model Model) (int64, er
updatedAt
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (objectType, objectId, relation, subjectType, subjectId, subjectRelation, policyHash) DO UPDATE SET
createdAt = ?,
createdAt = IIF(warrant.deletedAt IS NULL, warrant.createdAt, ?),
updatedAt = ?,
deletedAt = NULL
RETURNING id
Expand Down
2 changes: 1 addition & 1 deletion pkg/object/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (repo MySQLRepository) Create(ctx context.Context, model Model) (int64, err
) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
meta = ?,
createdAt = CURRENT_TIMESTAMP(6),
createdAt = IF(object.deletedAt IS NULL, object.createdAt, CURRENT_TIMESTAMP(6)),
updatedAt = CURRENT_TIMESTAMP(6),
deletedAt = NULL
`,
Expand Down
5 changes: 4 additions & 1 deletion pkg/object/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func (repo PostgresRepository) Create(ctx context.Context, model Model) (int64,
) VALUES (?, ?, ?)
ON CONFLICT (object_type, object_id) DO UPDATE SET
meta = ?,
created_at = CURRENT_TIMESTAMP(6),
created_at = CASE
WHEN object.deleted_at IS NULL THEN object.created_at
ELSE CURRENT_TIMESTAMP(6)
END,
updated_at = CURRENT_TIMESTAMP(6),
deleted_at = NULL
RETURNING id
Expand Down
2 changes: 1 addition & 1 deletion pkg/object/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (repo SQLiteRepository) Create(ctx context.Context, model Model) (int64, er
) VALUES (?, ?, ?, ?, ?)
ON CONFLICT (objectType, objectId) DO UPDATE SET
meta = ?,
createdAt = ?,
createdAt = IIF(object.deletedAt IS NULL, object.createdAt, ?),
updatedAt = ?,
deletedAt = NULL
RETURNING id
Expand Down
8 changes: 4 additions & 4 deletions tests/objects-crud.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@
"expectedResponse": {
"statusCode": 200,
"body": [
{
"objectType": "test",
"objectId": "second-object"
},
{
"objectType": "test",
"objectId": "third-object",
Expand All @@ -174,6 +170,10 @@
"objectType": "test",
"objectId": "{{ createObjectWithGeneratedId.objectId }}"
},
{
"objectType": "test",
"objectId": "second-object"
},
{
"objectType": "test",
"objectId": "first-object"
Expand Down

0 comments on commit 74d98f3

Please sign in to comment.