Unified Storage: Fix Create, Update and Delete wrt Resource Versions (#88183)

* add sqltemplate utilities, improve tests and documentation

* bunch of things

* remove unnecessary message

* add queries

* add queries

* add queries

* add folders support

* fix diff

* fix linters

* fix diff

* fix linters

* fix linters

* fix typo

* fix linters

* fix linters

* fix linters

* several fixes

* several fixes

* temporarily disable k8s integration tests for Entity Server

* postpone some tests

* postpone documentation changes

* Fix bug in create

* improve error reporting

* fix PostgeSQL parameters

* fix MySQL sqlmode

* fix MySQL-5.7

* reduce but document the number of database connection options

* remove unused code and improve docs
This commit is contained in:
Diego Augusto Molina
2024-06-05 14:23:32 -03:00
committed by GitHub
parent cbe7521a56
commit 6fcd7d9e03
43 changed files with 1867 additions and 1298 deletions
@@ -0,0 +1,61 @@
{{/*
This is the list of all the fields in *entity.Entity, in a way that is
suitable to be imported by other templates that need to select these fields
from either the "entity" or the "entity_history" tables.
Example usage:
SELECT {{ template "common_entity_select_into" . }}
FROM {{ .Ident "entity" }} AS e
*/}}
{{ define "common_entity_select_into" }}
e.{{ .Ident "guid" | .Into .Entity.Guid }},
e.{{ .Ident "resource_version" | .Into .Entity.ResourceVersion }},
e.{{ .Ident "key" | .Into .Entity.Key }},
e.{{ .Ident "group" | .Into .Entity.Group }},
e.{{ .Ident "group_version" | .Into .Entity.GroupVersion }},
e.{{ .Ident "resource" | .Into .Entity.Resource }},
e.{{ .Ident "namespace" | .Into .Entity.Namespace }},
e.{{ .Ident "name" | .Into .Entity.Name }},
e.{{ .Ident "folder" | .Into .Entity.Folder }},
e.{{ .Ident "meta" | .Into .Entity.Meta }},
e.{{ .Ident "body" | .Into .Entity.Body }},
e.{{ .Ident "status" | .Into .Entity.Status }},
e.{{ .Ident "size" | .Into .Entity.Size }},
e.{{ .Ident "etag" | .Into .Entity.ETag }},
e.{{ .Ident "created_at" | .Into .Entity.CreatedAt }},
e.{{ .Ident "created_by" | .Into .Entity.CreatedBy }},
e.{{ .Ident "updated_at" | .Into .Entity.UpdatedAt }},
e.{{ .Ident "updated_by" | .Into .Entity.UpdatedBy }},
e.{{ .Ident "origin" | .Into .Entity.Origin.Source }},
e.{{ .Ident "origin_key" | .Into .Entity.Origin.Key }},
e.{{ .Ident "origin_ts" | .Into .Entity.Origin.Time }},
e.{{ .Ident "title" | .Into .Entity.Title }},
e.{{ .Ident "slug" | .Into .Entity.Slug }},
e.{{ .Ident "description" | .Into .Entity.Description }},
e.{{ .Ident "message" | .Into .Entity.Message }},
e.{{ .Ident "labels" | .Into .Entity.Labels }},
e.{{ .Ident "fields" | .Into .Entity.Fields }},
e.{{ .Ident "errors" | .Into .Entity.Errors }},
e.{{ .Ident "action" | .Into .Entity.Action }}
{{ end }}
{{/* Build an ORDER BY clause from a []SortBy contained in a .Sort field */}}
{{ define "common_order_by" }}
{{ $comma := listSep ", " }}
{{ range .Sort }}
{{- call $comma -}} {{ $.Ident .Field }} {{ .Direction.String }}
{{ end }}
{{ end }}
@@ -12,24 +12,18 @@ INSERT INTO {{ .Ident "entity_folder" }}
)
VALUES
{{ $this := . }}
{{ $addComma := false }}
{{ $comma := listSep ", " }}
{{ range .Items }}
{{ if $addComma }}
,
{{ end }}
{{ $addComma = true }}
(
{{ $this.Arg .GUID }},
{{ $this.Arg .Namespace }},
{{ $this.Arg .UID }},
{{ $this.Arg .SlugPath }},
{{ $this.Arg .JS }},
{{ $this.Arg .Depth }},
{{ $this.Arg .Left }},
{{ $this.Arg .Right }},
{{ $this.Arg .Detached }}
{{- call $comma -}} (
{{ $.Arg .GUID }},
{{ $.Arg .Namespace }},
{{ $.Arg .UID }},
{{ $.Arg .SlugPath }},
{{ $.Arg .JS }},
{{ $.Arg .Depth }},
{{ $.Arg .Left }},
{{ $.Arg .Right }},
{{ $.Arg .Detached }}
)
{{ end }}
;
@@ -0,0 +1,30 @@
SELECT {{ template "common_entity_select_into" . }}
FROM {{ .Ident "entity_history" }} AS e
WHERE 1 = 1
{{ if gt .Before 0 }}
AND {{ .Ident "resource_version" }} < {{ .Arg .Before }}
{{ end }}
{{/* There are two mutually exclusive search modes: by GUID and by Key */}}
{{ if ne .Query.GUID "" }}
AND {{ .Ident "guid" }} = {{ .Arg .Query.GUID }}
{{ else }}
AND {{ .Ident "group" }} = {{ .Arg .Query.Key.Group }}
AND {{ .Ident "resource" }} = {{ .Arg .Query.Key.Resource }}
AND {{ .Ident "name" }} = {{ .Arg .Query.Key.Name }}
{{ if ne .Query.Key.Namespace "" }}
AND {{ .Ident "namespace" }} = {{ .Arg .Query.Key.Namespace }}
{{ end }}
{{ end }}
ORDER BY {{ template "common_order_by" . }}
LIMIT {{ .Limit }}
OFFSET {{ .Offset }}
;
@@ -2,17 +2,6 @@ DELETE FROM {{ .Ident "entity_labels" }}
WHERE 1 = 1
AND {{ .Ident "guid" }} = {{ .Arg .GUID }}
{{ if gt (len .KeepLabels) 0 }}
AND {{ .Ident "label" }} NOT IN (
{{ $this := . }}
{{ $addComma := false }}
{{ range .KeepLabels }}
{{ if $addComma }}
,
{{ end }}
{{ $addComma = true }}
{{ $this.Arg . }}
{{ end }}
)
AND {{ .Ident "label" }} NOT IN ( {{ .ArgList .KeepLabels }} )
{{ end }}
;
@@ -6,24 +6,12 @@ INSERT INTO {{ .Ident "entity_labels" }}
)
VALUES
{{/*
When we enter the "range" loop the "." will be changed, so we need to
store the current ".GUID" in a variable to be able to use its value
*/}}
{{ $guid := .GUID }}
{{ $this := . }}
{{ $addComma := false }}
{{ $comma := listSep ", " }}
{{ range $name, $value := .Labels }}
{{ if $addComma }}
,
{{ end }}
{{ $addComma = true }}
(
{{ $this.Arg $guid }},
{{ $this.Arg $name }},
{{ $this.Arg $value }}
{{- call $comma -}} (
{{ $.Arg $.GUID }},
{{ $.Arg $name }},
{{ $.Arg $value }}
)
{{ end }}
;
@@ -1,49 +1,10 @@
SELECT
{{ .Ident "guid" | .Into .Entity.Guid }},
{{ .Ident "resource_version" | .Into .Entity.ResourceVersion }},
{{ .Ident "key" | .Into .Entity.Key }},
{{ .Ident "group" | .Into .Entity.Group }},
{{ .Ident "group_version" | .Into .Entity.GroupVersion }},
{{ .Ident "resource" | .Into .Entity.Resource }},
{{ .Ident "namespace" | .Into .Entity.Namespace }},
{{ .Ident "name" | .Into .Entity.Name }},
{{ .Ident "folder" | .Into .Entity.Folder }},
{{ .Ident "meta" | .Into .Entity.Meta }},
{{ .Ident "body" | .Into .Entity.Body }},
{{ .Ident "status" | .Into .Entity.Status }},
{{ .Ident "size" | .Into .Entity.Size }},
{{ .Ident "etag" | .Into .Entity.ETag }},
{{ .Ident "created_at" | .Into .Entity.CreatedAt }},
{{ .Ident "created_by" | .Into .Entity.CreatedBy }},
{{ .Ident "updated_at" | .Into .Entity.UpdatedAt }},
{{ .Ident "updated_by" | .Into .Entity.UpdatedBy }},
{{ .Ident "origin" | .Into .Entity.Origin.Source }},
{{ .Ident "origin_key" | .Into .Entity.Origin.Key }},
{{ .Ident "origin_ts" | .Into .Entity.Origin.Time }},
{{ .Ident "title" | .Into .Entity.Title }},
{{ .Ident "slug" | .Into .Entity.Slug }},
{{ .Ident "description" | .Into .Entity.Description }},
{{ .Ident "message" | .Into .Entity.Message }},
{{ .Ident "labels" | .Into .Entity.Labels }},
{{ .Ident "fields" | .Into .Entity.Fields }},
{{ .Ident "errors" | .Into .Entity.Errors }},
{{ .Ident "action" | .Into .Entity.Action }}
SELECT {{ template "common_entity_select_into" . }}
FROM
{{ if gt .ResourceVersion 0 }}
{{ .Ident "entity_history" }}
{{ .Ident "entity_history" }} AS e
{{ else }}
{{ .Ident "entity" }}
{{ .Ident "entity" }} AS e
{{ end }}
WHERE 1 = 1
@@ -73,6 +34,6 @@ SELECT
{{ end }}
{{ if .SelectForUpdate }}
{{ .SelectFor "UPDATE" }}
{{ .SelectFor "UPDATE NOWAIT" }}
{{ end }}
;
@@ -1,43 +1,4 @@
SELECT
e.{{ .Ident "guid" | .Into .Entity.Guid }},
e.{{ .Ident "resource_version" | .Into .Entity.ResourceVersion }},
e.{{ .Ident "key" | .Into .Entity.Key }},
e.{{ .Ident "group" | .Into .Entity.Group }},
e.{{ .Ident "group_version" | .Into .Entity.GroupVersion }},
e.{{ .Ident "resource" | .Into .Entity.Resource }},
e.{{ .Ident "namespace" | .Into .Entity.Namespace }},
e.{{ .Ident "name" | .Into .Entity.Name }},
e.{{ .Ident "folder" | .Into .Entity.Folder }},
e.{{ .Ident "meta" | .Into .Entity.Meta }},
e.{{ .Ident "body" | .Into .Entity.Body }},
e.{{ .Ident "status" | .Into .Entity.Status }},
e.{{ .Ident "size" | .Into .Entity.Size }},
e.{{ .Ident "etag" | .Into .Entity.ETag }},
e.{{ .Ident "created_at" | .Into .Entity.CreatedAt }},
e.{{ .Ident "created_by" | .Into .Entity.CreatedBy }},
e.{{ .Ident "updated_at" | .Into .Entity.UpdatedAt }},
e.{{ .Ident "updated_by" | .Into .Entity.UpdatedBy }},
e.{{ .Ident "origin" | .Into .Entity.Origin.Source }},
e.{{ .Ident "origin_key" | .Into .Entity.Origin.Key }},
e.{{ .Ident "origin_ts" | .Into .Entity.Origin.Time }},
e.{{ .Ident "title" | .Into .Entity.Title }},
e.{{ .Ident "slug" | .Into .Entity.Slug }},
e.{{ .Ident "description" | .Into .Entity.Description }},
e.{{ .Ident "message" | .Into .Entity.Message }},
e.{{ .Ident "labels" | .Into .Entity.Labels }},
e.{{ .Ident "fields" | .Into .Entity.Fields }},
e.{{ .Ident "errors" | .Into .Entity.Errors }},
e.{{ .Ident "action" | .Into .Entity.Action }}
SELECT {{ template "common_entity_select_into" . }}
FROM
{{ .Ident "entity_ref" }} AS r
@@ -0,0 +1,10 @@
SELECT
{{ .Ident "resource_version" | .Into .ResourceVersion }},
{{ .Ident "created_at" | .Into .ResourceVersion }},
{{ .Ident "updated_at" | .Into .ResourceVersion }}
FROM {{ .Ident "kind_version" }}
WHERE 1 = 1
AND {{ .Ident "group" }} = {{ .Arg .Group }}
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
;
@@ -1,5 +1,8 @@
UPDATE {{ .Ident "kind_version" }}
SET {{ .Ident "resource_version" }} = {{ .Arg .ResourceVersion }} + 1
SET
{{ .Ident "resource_version" }} = {{ .Arg .ResourceVersion }} + 1,
{{ .Ident "updated_at" }} = {{ .Arg .UpdatedAt }}
WHERE 1 = 1
AND {{ .Ident "group" }} = {{ .Arg .Group }}
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
@@ -2,12 +2,16 @@ INSERT INTO {{ .Ident "kind_version" }}
(
{{ .Ident "group" }},
{{ .Ident "resource" }},
{{ .Ident "resource_version" }}
{{ .Ident "resource_version" }},
{{ .Ident "created_at" }},
{{ .Ident "updated_at" }}
)
VALUES (
{{ .Arg .Group }},
{{ .Arg .Resource }},
1
1,
{{ .Arg .CreatedAt }},
{{ .Arg .UpdatedAt }}
)
;
@@ -1,7 +1,7 @@
SELECT {{ .Ident "resource_version" | .Into .ResourceVersion }}
FROM {{ .Ident "kind_version" }}
WHERE 1 = 1
AND {{ .Ident "group" }} = {{ .Arg .Group }}
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
AND {{ .Ident "group" }} = {{ .Arg .Group }}
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
{{ .SelectFor "UPDATE" }}
;