forked from nephio-project/porch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c53afaf
commit 65e1735
Showing
9 changed files
with
359 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,67 @@ | ||
DROP TABLE IF EXISTS resources; | ||
DROP TABLE IF EXISTS package_revisions; | ||
DROP TABLE IF EXISTS packages; | ||
DROP TABLE IF EXISTS repositories; | ||
DROP TABLE IF EXISTS resources; | ||
|
||
DROP TYPE IF EXISTS package_rev_lifecycle; | ||
|
||
CREATE TABLE IF NOT EXISTS repositories ( | ||
namespace TEXT NOT NULL, | ||
name_space TEXT NOT NULL, | ||
repo_name TEXT NOT NULL, | ||
meta JSONB NOT NULL, | ||
spec JSONB NOT NULL, | ||
meta TEXT NOT NULL, | ||
spec TEXT NOT NULL, | ||
updated TIMESTAMPTZ, | ||
updatedby TEXT NOT NULL, | ||
deployment BOOLEAN, | ||
PRIMARY KEY (namespace, repo_name) | ||
PRIMARY KEY (name_space, repo_name) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS packages ( | ||
namespace TEXT NOT NULL, | ||
name_space TEXT NOT NULL, | ||
repo_name TEXT NOT NULL, | ||
package_name TEXT NOT NULL, | ||
meta JSONB NOT NULL, | ||
spec JSONB NOT NULL, | ||
meta TEXT NOT NULL, | ||
spec TEXT NOT NULL, | ||
updated TIMESTAMPTZ NOT NULL, | ||
updatedby TEXT NOT NULL, | ||
PRIMARY KEY (namespace, repo_name, package_name), | ||
PRIMARY KEY (name_space, repo_name, package_name), | ||
CONSTRAINT fk_repository | ||
FOREIGN KEY (namespace, repo_name) | ||
REFERENCES repositories (namespace, repo_name) | ||
FOREIGN KEY (name_space, repo_name) | ||
REFERENCES repositories (name_space, repo_name) | ||
ON DELETE CASCADE | ||
); | ||
|
||
CREATE TYPE package_rev_lifecycle AS ENUM ('Draft', 'Proposed', 'Published', 'DeletionProposed'); | ||
|
||
CREATE TABLE IF NOT EXISTS package_revisions ( | ||
namespace TEXT NOT NULL, | ||
name_space TEXT NOT NULL, | ||
repo_name TEXT NOT NULL, | ||
package_name TEXT NOT NULL, | ||
package_rev TEXT NOT NULL, | ||
workspace_name TEXT NOT NULL, | ||
meta JSONB NOT NULL, | ||
spec JSONB NOT NULL, | ||
meta TEXT NOT NULL, | ||
spec TEXT NOT NULL, | ||
updated TIMESTAMPTZ NOT NULL, | ||
updatedby TEXT NOT NULL, | ||
lifecycle package_rev_lifecycle NOT NULL, | ||
PRIMARY KEY (namespace, repo_name, package_name, package_rev), | ||
PRIMARY KEY (name_space, repo_name, package_name, package_rev, workspace_name), | ||
CONSTRAINT fk_package | ||
FOREIGN KEY (namespace, repo_name, package_name) | ||
REFERENCES packages (namespace, repo_name, package_name) | ||
FOREIGN KEY (name_space, repo_name, package_name) | ||
REFERENCES packages (name_space, repo_name, package_name) | ||
ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS resources ( | ||
namespace TEXT NOT NULL, | ||
name_space TEXT NOT NULL, | ||
repo_name TEXT NOT NULL, | ||
package_name TEXT NOT NULL, | ||
package_rev TEXT NOT NULL, | ||
workspace_name TEXT NOT NULL, | ||
resource_key TEXT NOT NULL, | ||
resource_value JSONB NOT NULL, | ||
lifecycle package_rev_lifecycle NOT NULL, | ||
resources BYTEA, | ||
PRIMARY KEY (namespace, repo_name, package_name, package_rev), | ||
resource_value TEXT NOT NULL, | ||
PRIMARY KEY (name_space, repo_name, package_name, package_rev, workspace_name, resource_key), | ||
CONSTRAINT fk_package | ||
FOREIGN KEY (namespace, repo_name, package_name) | ||
REFERENCES packages (namespace, repo_name, package_name) | ||
FOREIGN KEY (name_space, repo_name, package_name, package_rev, workspace_name) | ||
REFERENCES package_revisions (name_space, repo_name, package_name, package_rev, workspace_name) | ||
ON DELETE CASCADE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright 2024 The Nephio Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dbcache | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/nephio-project/porch/pkg/repository" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func pkgRevResourceReadFromDB(prk repository.PackageRevisionKey, resKey string) (string, string, error) { | ||
klog.Infof("pkgRevResourceReadFromDB: reading package revision resource %q:%s", prk, resKey) | ||
|
||
sqlStatement := `SELECT resource_value FROM resources | ||
WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5 AND key=$6` | ||
|
||
var resVal string | ||
|
||
err := GetDBConnection().db.QueryRow( | ||
sqlStatement, prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName).Scan( | ||
&resVal) | ||
|
||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
klog.Infof("pkgRevResourceReadFromDB: package revision resource not found in db %q:%s", prk, resKey) | ||
} else { | ||
klog.Infof("pkgRevResourceReadFromDB: reading package revision resource %q:%s returned err: %q", prk, resKey, err) | ||
} | ||
return resKey, "", err | ||
} | ||
|
||
klog.Infof("pkgRevResourceReadFromDB: reading package succeeded %q:%s", prk, resKey) | ||
|
||
return resKey, resVal, err | ||
} | ||
|
||
func pkgRevResourcesReadFromDB(prk repository.PackageRevisionKey) (map[string]string, error) { | ||
sqlStatement := `SELECT * FROM resources WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5` | ||
|
||
resources := make(map[string]string) | ||
|
||
klog.Infof("pkgRevResourcesReadFromDB: running query [%q] on %q", sqlStatement, prk) | ||
|
||
rows, err := GetDBConnection().db.Query(sqlStatement, prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName) | ||
if err != nil { | ||
klog.Infof("pkgRevResourcesReadFromDB: query failed for %q: %q", prk, err) | ||
return nil, err | ||
} | ||
|
||
klog.Infof("pkgRevResourcesReadFromDB: query succeeded for %q", prk) | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var resKey, resVal string | ||
|
||
rows.Scan( | ||
&resKey, | ||
&resVal) | ||
|
||
resources[resKey] = resVal | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func pkgRevResourcesWriteToDB(pr *dbPackageRevision) error { | ||
klog.Infof("pkgRevResourcesWriteToDB: writing package revision resources for %q", pr.Key()) | ||
|
||
for k, v := range pr.resources { | ||
if err := pkgRevResourceWriteToDB(pr, k, v); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
klog.Infof("pkgRevResourcesWriteToDB: succeeded, row created") | ||
return nil | ||
} | ||
|
||
func pkgRevResourceWriteToDB(pr *dbPackageRevision, resKey string, resVal string) error { | ||
klog.Infof("pkgRevResourceWriteToDB: writing package revision resource %q:%s", pr.Key(), resKey) | ||
|
||
sqlStatement := ` | ||
INSERT INTO resources (name_space, repo_name, package_name, package_rev, workspace_name, resource_key, resource_value) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7) | ||
ON CONFLICT (name_space, repo_name, package_name, package_rev, workspace_name, resource_key) | ||
DO UPDATE SET resource_value = EXCLUDED.resource_value` | ||
|
||
klog.Infof("pkgRevResourceWriteToDB: running query [%q] on repository (%#v)", sqlStatement, pr) | ||
|
||
prk := pr.Key() | ||
if returnedVal := GetDBConnection().db.QueryRow( | ||
sqlStatement, | ||
prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName, | ||
resKey, resVal); returnedVal.Err() == nil { | ||
klog.Infof("pkgRevResourceWriteToDB: query succeeded, row created") | ||
return nil | ||
} else { | ||
klog.Infof("pkgRevResourceWriteToDB: query failed %q", returnedVal.Err()) | ||
return returnedVal.Err() | ||
} | ||
} | ||
|
||
func pkgRevResourcesDeleteFromDB(prk repository.PackageRevisionKey) error { | ||
klog.Infof("pkgRevResourcesDeleteFromDB: deleting package revision %q", prk) | ||
|
||
sqlStatement := `DELETE FROM resources WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5` | ||
|
||
returnedVal := GetDBConnection().db.QueryRow(sqlStatement, | ||
prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName) | ||
|
||
if returnedVal.Err() == nil { | ||
klog.Infof("pkgRevResourcesDeleteFromDB: deleted package revision %q", prk) | ||
} else { | ||
klog.Infof("pkgRevResourcesDeleteFromDB: deletion of package revision %q failed: %q", prk, returnedVal.Err()) | ||
} | ||
|
||
return returnedVal.Err() | ||
} | ||
|
||
func pkgRevResourceDeleteFromDB(prk repository.PackageRevisionKey, resKey string) error { | ||
klog.Infof("pkgRevResourceDeleteFromDB: deleting package revision %q", prk) | ||
|
||
sqlStatement := `DELETE FROM resources WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5 AND presource_key=$6` | ||
|
||
returnedVal := GetDBConnection().db.QueryRow(sqlStatement, | ||
prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName, resKey) | ||
|
||
if returnedVal.Err() == nil { | ||
klog.Infof("pkgRevResourceDeleteFromDB: deleted package revision %q", prk) | ||
} else { | ||
klog.Infof("pkgRevResourceDeleteFromDB: deletion of package revision %q failed: %q", prk, returnedVal.Err()) | ||
} | ||
|
||
return returnedVal.Err() | ||
} |
Oops, something went wrong.