Merge pull request #5479 from NixOS/selfref-ca

Fix the removal of ca-induced self-references
This commit is contained in:
Théophane Hufschmitt 2022-04-21 10:30:22 +02:00 committed by GitHub
commit d6efc07f85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 4 deletions

View file

@ -13,12 +13,27 @@ create table if not exists Realisations (
create index if not exists IndexRealisations on Realisations(drvPath, outputName); create index if not exists IndexRealisations on Realisations(drvPath, outputName);
-- We can end-up in a weird edge-case where a path depends on itself because
-- its an output of a CA derivation, that happens to be the same as one of its
-- dependencies.
-- In that case we have a dependency loop (path -> realisation1 -> realisation2
-- -> path) that we need to break by removing the dependencies between the
-- realisations
create trigger if not exists DeleteSelfRefsViaRealisations before delete on ValidPaths
begin
delete from RealisationsRefs where realisationReference in (
select id from Realisations where outputPath = old.id
);
end;
create table if not exists RealisationsRefs ( create table if not exists RealisationsRefs (
referrer integer not null, referrer integer not null,
realisationReference integer, realisationReference integer,
foreign key (referrer) references Realisations(id) on delete cascade, foreign key (referrer) references Realisations(id) on delete cascade,
foreign key (realisationReference) references Realisations(id) on delete restrict foreign key (realisationReference) references Realisations(id) on delete restrict
); );
-- used by deletion trigger
create index if not exists IndexRealisationsRefsRealisationReference on RealisationsRefs(realisationReference);
-- used by QueryRealisationReferences -- used by QueryRealisationReferences
create index if not exists IndexRealisationsRefs on RealisationsRefs(referrer); create index if not exists IndexRealisationsRefs on RealisationsRefs(referrer);

View file

@ -81,7 +81,7 @@ int getSchema(Path schemaPath)
void migrateCASchema(SQLite& db, Path schemaPath, AutoCloseFD& lockFd) void migrateCASchema(SQLite& db, Path schemaPath, AutoCloseFD& lockFd)
{ {
const int nixCASchemaVersion = 3; const int nixCASchemaVersion = 4;
int curCASchema = getSchema(schemaPath); int curCASchema = getSchema(schemaPath);
if (curCASchema != nixCASchemaVersion) { if (curCASchema != nixCASchemaVersion) {
if (curCASchema > nixCASchemaVersion) { if (curCASchema > nixCASchemaVersion) {
@ -143,6 +143,21 @@ void migrateCASchema(SQLite& db, Path schemaPath, AutoCloseFD& lockFd)
)"); )");
txn.commit(); txn.commit();
} }
if (curCASchema < 4) {
SQLiteTxn txn(db);
db.exec(R"(
create trigger if not exists DeleteSelfRefsViaRealisations before delete on ValidPaths
begin
delete from RealisationsRefs where realisationReference in (
select id from Realisations where outputPath = old.id
);
end;
-- used by deletion trigger
create index if not exists IndexRealisationsRefsRealisationReference on RealisationsRefs(realisationReference);
)");
txn.commit();
}
writeFile(schemaPath, fmt("%d", nixCASchemaVersion)); writeFile(schemaPath, fmt("%d", nixCASchemaVersion));
lockFile(lockFd.get(), ltRead, true); lockFile(lockFd.get(), ltRead, true);
} }

View file

@ -2,7 +2,7 @@ source common.sh
file=build-hook-ca-floating.nix file=build-hook-ca-floating.nix
enableFeatures "ca-derivations ca-references" enableFeatures "ca-derivations"
CONTENT_ADDRESSED=true CONTENT_ADDRESSED=true

View file

@ -1,5 +1,5 @@
source ../common.sh source ../common.sh
enableFeatures "ca-derivations ca-references" enableFeatures "ca-derivations"
restartDaemon restartDaemon

11
tests/ca/selfref-gc.sh Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env bash
source common.sh
requireDaemonNewerThan "2.4pre20210626"
enableFeatures "ca-derivations nix-command flakes"
export NIX_TESTS_CA_BY_DEFAULT=1
cd ..
source ./selfref-gc.sh

View file

@ -92,6 +92,7 @@ nix_tests = \
plugins.sh \ plugins.sh \
build.sh \ build.sh \
ca/nix-run.sh \ ca/nix-run.sh \
selfref-gc.sh ca/selfref-gc.sh \
db-migration.sh \ db-migration.sh \
bash-profile.sh \ bash-profile.sh \
pass-as-file.sh \ pass-as-file.sh \

View file

@ -3,7 +3,7 @@ source common.sh
clearStore clearStore
clearProfiles clearProfiles
enableFeatures "ca-derivations ca-references" enableFeatures "ca-derivations"
restartDaemon restartDaemon
# Make a flake. # Make a flake.

30
tests/selfref-gc.sh Normal file
View file

@ -0,0 +1,30 @@
source common.sh
requireDaemonNewerThan "2.6.0pre20211215"
clearStore
nix-build --no-out-link -E '
with import ./config.nix;
let d1 = mkDerivation {
name = "selfref-gc";
outputs = [ "out" ];
buildCommand = "
echo SELF_REF: $out > $out
";
}; in
# the only change from d1 is d1 as an (unused) build input
# to get identical store path in CA.
mkDerivation {
name = "selfref-gc";
outputs = [ "out" ];
buildCommand = "
echo UNUSED: ${d1}
echo SELF_REF: $out > $out
";
}
'
nix-collect-garbage