Unified Storage: create kind_version table migration, add SQL and fix db (#87977)
* fix database interfaces * add queries * fix queries * fix linters * add owner to imported go library * remove unused funcs * run go work sync * improve critical section fix in data race fix * fix linters * remove sync * fix typo * improve data embedding * fix linters * fix migration * remove unnecessary comments * fix linters * improve SQL templates readability * remove group_version from kind_version for consistency in History method * add created_at and updated_at columns to kind_version table
This commit is contained in:
committed by
GitHub
parent
6744dae806
commit
8b02b6b76a
@@ -0,0 +1,7 @@
|
||||
DELETE FROM {{ .Ident "entity" }}
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "namespace" }} = {{ .Arg .Key.Namespace }}
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Key.Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Key.Resource }}
|
||||
AND {{ .Ident "name" }} = {{ .Arg .Key.Name }}
|
||||
;
|
||||
@@ -0,0 +1,35 @@
|
||||
INSERT INTO {{ .Ident "entity_folder" }}
|
||||
(
|
||||
{{ .Ident "guid" }},
|
||||
{{ .Ident "namespace" }},
|
||||
{{ .Ident "name" }},
|
||||
{{ .Ident "slug_path" }},
|
||||
{{ .Ident "tree" }},
|
||||
{{ .Ident "depth" }},
|
||||
{{ .Ident "lft" }},
|
||||
{{ .Ident "rgt" }},
|
||||
{{ .Ident "detached" }}
|
||||
)
|
||||
|
||||
VALUES
|
||||
{{ $this := . }}
|
||||
{{ $addComma := false }}
|
||||
{{ 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 }}
|
||||
)
|
||||
{{ end }}
|
||||
;
|
||||
@@ -0,0 +1,93 @@
|
||||
INSERT INTO
|
||||
|
||||
{{/* Determine which table to insert into */}}
|
||||
{{ if .TableEntity }} {{ .Ident "entity" }}
|
||||
{{ else }} {{ .Ident "entity_history" }}
|
||||
{{ end }}
|
||||
|
||||
{{/* Explicitly specify fields that will be set */}}
|
||||
(
|
||||
{{ .Ident "guid" }},
|
||||
{{ .Ident "resource_version" }},
|
||||
|
||||
{{ .Ident "key" }},
|
||||
|
||||
{{ .Ident "group" }},
|
||||
{{ .Ident "group_version" }},
|
||||
{{ .Ident "resource" }},
|
||||
{{ .Ident "namespace" }},
|
||||
{{ .Ident "name" }},
|
||||
|
||||
{{ .Ident "folder" }},
|
||||
|
||||
{{ .Ident "meta" }},
|
||||
{{ .Ident "body" }},
|
||||
{{ .Ident "status" }},
|
||||
|
||||
{{ .Ident "size" }},
|
||||
{{ .Ident "etag" }},
|
||||
|
||||
{{ .Ident "created_at" }},
|
||||
{{ .Ident "created_by" }},
|
||||
{{ .Ident "updated_at" }},
|
||||
{{ .Ident "updated_by" }},
|
||||
|
||||
{{ .Ident "origin" }},
|
||||
{{ .Ident "origin_key" }},
|
||||
{{ .Ident "origin_ts" }},
|
||||
|
||||
{{ .Ident "title" }},
|
||||
{{ .Ident "slug" }},
|
||||
{{ .Ident "description" }},
|
||||
|
||||
{{ .Ident "message" }},
|
||||
{{ .Ident "labels" }},
|
||||
{{ .Ident "fields" }},
|
||||
{{ .Ident "errors" }},
|
||||
|
||||
{{ .Ident "action" }}
|
||||
)
|
||||
|
||||
{{/* Provide the values */}}
|
||||
VALUES (
|
||||
{{ .Arg .Entity.Guid }},
|
||||
{{ .Arg .Entity.ResourceVersion }},
|
||||
|
||||
{{ .Arg .Entity.Key }},
|
||||
|
||||
{{ .Arg .Entity.Group }},
|
||||
{{ .Arg .Entity.GroupVersion }},
|
||||
{{ .Arg .Entity.Resource }},
|
||||
{{ .Arg .Entity.Namespace }},
|
||||
{{ .Arg .Entity.Name }},
|
||||
|
||||
{{ .Arg .Entity.Folder }},
|
||||
|
||||
{{ .Arg .Entity.Meta }},
|
||||
{{ .Arg .Entity.Body }},
|
||||
{{ .Arg .Entity.Status }},
|
||||
|
||||
{{ .Arg .Entity.Size }},
|
||||
{{ .Arg .Entity.ETag }},
|
||||
|
||||
{{ .Arg .Entity.CreatedAt }},
|
||||
{{ .Arg .Entity.CreatedBy }},
|
||||
{{ .Arg .Entity.UpdatedAt }},
|
||||
{{ .Arg .Entity.UpdatedBy }},
|
||||
|
||||
{{ .Arg .Entity.Origin.Source }},
|
||||
{{ .Arg .Entity.Origin.Key }},
|
||||
{{ .Arg .Entity.Origin.Time }},
|
||||
|
||||
{{ .Arg .Entity.Title }},
|
||||
{{ .Arg .Entity.Slug }},
|
||||
{{ .Arg .Entity.Description }},
|
||||
|
||||
{{ .Arg .Entity.Message }},
|
||||
{{ .Arg .Entity.Labels }},
|
||||
{{ .Arg .Entity.Fields }},
|
||||
{{ .Arg .Entity.Errors }},
|
||||
|
||||
{{ .Arg .Entity.Action }}
|
||||
)
|
||||
;
|
||||
@@ -0,0 +1,18 @@
|
||||
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 }}
|
||||
)
|
||||
{{ end }}
|
||||
;
|
||||
@@ -0,0 +1,29 @@
|
||||
INSERT INTO {{ .Ident "entity_labels" }}
|
||||
(
|
||||
{{ .Ident "guid" }},
|
||||
{{ .Ident "label" }},
|
||||
{{ .Ident "value" }}
|
||||
)
|
||||
|
||||
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 }}
|
||||
{{ range $name, $value := .Labels }}
|
||||
{{ if $addComma }}
|
||||
,
|
||||
{{ end }}
|
||||
{{ $addComma = true }}
|
||||
|
||||
(
|
||||
{{ $this.Arg $guid }},
|
||||
{{ $this.Arg $name }},
|
||||
{{ $this.Arg $value }}
|
||||
)
|
||||
{{ end }}
|
||||
;
|
||||
@@ -0,0 +1,14 @@
|
||||
SELECT
|
||||
{{ .Ident "guid" | .Into .FolderInfo.GUID }},
|
||||
{{ .Ident "name" | .Into .FolderInfo.UID }},
|
||||
{{ .Ident "folder" | .Into .FolderInfo.ParentUID }},
|
||||
{{ .Ident "name" | .Into .FolderInfo.Name }},
|
||||
{{ .Ident "slug" | .Into .FolderInfo.Slug }}
|
||||
|
||||
FROM {{ .Ident "entity" }}
|
||||
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
|
||||
AND {{ .Ident "namespace" }} = {{ .Arg .Namespace }}
|
||||
;
|
||||
@@ -0,0 +1,78 @@
|
||||
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 }}
|
||||
|
||||
FROM
|
||||
{{ if gt .ResourceVersion 0 }}
|
||||
{{ .Ident "entity_history" }}
|
||||
{{ else }}
|
||||
{{ .Ident "entity" }}
|
||||
{{ end }}
|
||||
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "namespace" }} = {{ .Arg .Key.Namespace }}
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Key.Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Key.Resource }}
|
||||
AND {{ .Ident "name" }} = {{ .Arg .Key.Name }}
|
||||
|
||||
{{/*
|
||||
Resource versions work like snapshots at the kind level. Thus, a request
|
||||
to retrieve a specific resource version should be interpreted as asking
|
||||
for a resource as of how it existed at that point in time. This is why we
|
||||
request matching entities with at most the provided resource version, and
|
||||
return only the one with the highest resource version. In the case of not
|
||||
specifying a resource version (i.e. resource version zero), it is
|
||||
interpreted as the latest version of the given entity, thus we instead
|
||||
query the "entity" table (which holds only the latest version of
|
||||
non-deleted entities) and we don't need to specify anything else. The
|
||||
"entity" table has a unique constraint on (namespace, group, resource,
|
||||
name), so we're guaranteed to have at most one matching row.
|
||||
*/}}
|
||||
{{ if gt .ResourceVersion 0 }}
|
||||
AND {{ .Ident "resource_version" }} <= {{ .Arg .ResourceVersion }}
|
||||
|
||||
ORDER BY {{ .Ident "resource_version" }} DESC
|
||||
LIMIT 1
|
||||
{{ end }}
|
||||
|
||||
{{ if .SelectForUpdate }}
|
||||
{{ .SelectFor "UPDATE" }}
|
||||
{{ end }}
|
||||
;
|
||||
@@ -0,0 +1,53 @@
|
||||
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 }}
|
||||
|
||||
FROM
|
||||
{{ .Ident "entity_ref" }} AS r
|
||||
INNER JOIN
|
||||
{{ .Ident "entity" }} AS e
|
||||
ON r.{{ .Ident "guid" }} = e.{{ .Ident "guid" }}
|
||||
|
||||
WHERE 1 = 1
|
||||
AND r.{{ .Ident "namespace" }} = {{ .Arg .Request.Namespace }}
|
||||
AND r.{{ .Ident "group" }} = {{ .Arg .Request.Group }}
|
||||
AND r.{{ .Ident "resource" }} = {{ .Arg .Request.Resource }}
|
||||
AND r.{{ .Ident "resolved_to" }} = {{ .Arg .Request.Name }}
|
||||
;
|
||||
@@ -0,0 +1,34 @@
|
||||
UPDATE {{ .Ident "entity" }} SET
|
||||
{{ .Ident "resource_version" }} = {{ .Arg .Entity.ResourceVersion }},
|
||||
|
||||
{{ .Ident "group_version" }} = {{ .Arg .Entity.GroupVersion }},
|
||||
|
||||
{{ .Ident "folder" }} = {{ .Arg .Entity.Folder }},
|
||||
|
||||
{{ .Ident "meta" }} = {{ .Arg .Entity.Meta }},
|
||||
{{ .Ident "body" }} = {{ .Arg .Entity.Body }},
|
||||
{{ .Ident "status" }} = {{ .Arg .Entity.Status }},
|
||||
|
||||
{{ .Ident "size" }} = {{ .Arg .Entity.Size }},
|
||||
{{ .Ident "etag" }} = {{ .Arg .Entity.ETag }},
|
||||
|
||||
{{ .Ident "updated_at" }} = {{ .Arg .Entity.UpdatedAt }},
|
||||
{{ .Ident "updated_by" }} = {{ .Arg .Entity.UpdatedBy }},
|
||||
|
||||
{{ .Ident "origin" }} = {{ .Arg .Entity.Origin.Source }},
|
||||
{{ .Ident "origin_key" }} = {{ .Arg .Entity.Origin.Key }},
|
||||
{{ .Ident "origin_ts" }} = {{ .Arg .Entity.Origin.Time }},
|
||||
|
||||
{{ .Ident "title" }} = {{ .Arg .Entity.Title }},
|
||||
{{ .Ident "slug" }} = {{ .Arg .Entity.Slug }},
|
||||
{{ .Ident "description" }} = {{ .Arg .Entity.Description }},
|
||||
|
||||
{{ .Ident "message" }} = {{ .Arg .Entity.Message }},
|
||||
{{ .Ident "labels" }} = {{ .Arg .Entity.Labels }},
|
||||
{{ .Ident "fields" }} = {{ .Arg .Entity.Fields }},
|
||||
{{ .Ident "errors" }} = {{ .Arg .Entity.Errors }},
|
||||
|
||||
{{ .Ident "action" }} = {{ .Arg .Entity.Action }}
|
||||
|
||||
WHERE {{ .Ident "guid" }} = {{ .Arg .Entity.Guid }}
|
||||
;
|
||||
@@ -0,0 +1,7 @@
|
||||
UPDATE {{ .Ident "kind_version" }}
|
||||
SET {{ .Ident "resource_version" }} = {{ .Arg .ResourceVersion }} + 1
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
|
||||
AND {{ .Ident "resource_version" }} = {{ .Arg .ResourceVersion }}
|
||||
;
|
||||
@@ -0,0 +1,13 @@
|
||||
INSERT INTO {{ .Ident "kind_version" }}
|
||||
(
|
||||
{{ .Ident "group" }},
|
||||
{{ .Ident "resource" }},
|
||||
{{ .Ident "resource_version" }}
|
||||
)
|
||||
|
||||
VALUES (
|
||||
{{ .Arg .Group }},
|
||||
{{ .Arg .Resource }},
|
||||
1
|
||||
)
|
||||
;
|
||||
@@ -0,0 +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 }}
|
||||
{{ .SelectFor "UPDATE" }}
|
||||
;
|
||||
Reference in New Issue
Block a user