migrations: add cascading deletes for tables interleaved under versions

This CL adds ON DELETE CASCADE to foreign keys
referencing the versions table, so that deleting a version also deletes
all corresponding data.

Updates b/132710180
Updates b/131789052

Change-Id: I793d2b8b1769cda8b667d3d32c28728e79ac4b4a
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/466436
Reviewed-by: Andrew Bonventre <andybons@google.com>
This commit is contained in:
Rob Findley 2019-05-14 16:09:00 -04:00 коммит произвёл Julie Qiu
Родитель 871d28a305
Коммит 0f5655d627
2 изменённых файлов: 89 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,42 @@
-- Copyright 2019 The Go Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style
-- license that can be found in the LICENSE file.
BEGIN;
ALTER TABLE licenses
DROP CONSTRAINT "licenses_module_path_fkey",
ADD CONSTRAINT "licenses_module_path_fkey"
FOREIGN KEY (module_path, version)
REFERENCES versions(module_path, version);
ALTER TABLE packages
DROP CONSTRAINT "packages_module_path_fkey",
ADD CONSTRAINT "packages_module_path_fkey"
FOREIGN KEY (module_path, version)
REFERENCES versions(module_path, version);
ALTER TABLE package_licenses
DROP CONSTRAINT "package_licenses_module_path_fkey",
ADD CONSTRAINT "package_licenses_module_path_fkey"
FOREIGN KEY (module_path, version, package_path)
REFERENCES packages(module_path, version, path),
DROP CONSTRAINT "package_licenses_module_path_fkey1",
ADD CONSTRAINT "package_licenses_module_path_fkey1"
FOREIGN KEY (module_path, version, file_path)
REFERENCES licenses(module_path, version, file_path);
ALTER TABLE documents
DROP CONSTRAINT "documents_package_path_fkey",
ADD CONSTRAINT "documents_package_path_fkey"
FOREIGN KEY (package_path, module_path, version)
REFERENCES packages(path, module_path, version);
ALTER TABLE imports
DROP CONSTRAINT "imports_from_path_fkey",
ADD CONSTRAINT "imports_from_path_fkey"
FOREIGN KEY (from_path, from_module_path, from_version)
REFERENCES packages(path, module_path, version);
END;

Просмотреть файл

@ -0,0 +1,47 @@
-- Copyright 2019 The Go Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style
-- license that can be found in the LICENSE file.
BEGIN;
ALTER TABLE licenses
DROP CONSTRAINT "licenses_module_path_fkey",
ADD CONSTRAINT "licenses_module_path_fkey"
FOREIGN KEY (module_path, version)
REFERENCES versions(module_path, version)
ON DELETE CASCADE;
ALTER TABLE packages
DROP CONSTRAINT "packages_module_path_fkey",
ADD CONSTRAINT "packages_module_path_fkey"
FOREIGN KEY (module_path, version)
REFERENCES versions(module_path, version)
ON DELETE CASCADE;
ALTER TABLE package_licenses
DROP CONSTRAINT "package_licenses_module_path_fkey",
ADD CONSTRAINT "package_licenses_module_path_fkey"
FOREIGN KEY (module_path, version, package_path)
REFERENCES packages(module_path, version, path)
ON DELETE CASCADE,
DROP CONSTRAINT "package_licenses_module_path_fkey1",
ADD CONSTRAINT "package_licenses_module_path_fkey1"
FOREIGN KEY (module_path, version, file_path)
REFERENCES licenses(module_path, version, file_path)
ON DELETE CASCADE;
ALTER TABLE documents
DROP CONSTRAINT "documents_package_path_fkey",
ADD CONSTRAINT "documents_package_path_fkey"
FOREIGN KEY (package_path, module_path, version)
REFERENCES packages(path, module_path, version)
ON DELETE CASCADE;
ALTER TABLE imports
DROP CONSTRAINT "imports_from_path_fkey",
ADD CONSTRAINT "imports_from_path_fkey"
FOREIGN KEY (from_path, from_module_path, from_version)
REFERENCES packages(path, module_path, version)
ON DELETE CASCADE;
END;