Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions home-manager/services/dolt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ let
inherit (inputs.host) isKyber isGalactica;
homeDir = config.home.homeDirectory;
repoDir = "${homeDir}/dotfiles";
beadsDir = "${repoDir}/.beads";
legacyBeadsDir = "${repoDir}/.beads";
sharedServerDir = "${homeDir}/.beads/shared-server";
beadsDir = "${sharedServerDir}/dolt";
Comment thread
shunkakinoki marked this conversation as resolved.
doltManifest = "${beadsDir}/beads_global/.dolt/noms/manifest";
mirrorDir = "${homeDir}/.cache/beads-jsonl-mirror";
remoteUrl = "https://github.com/shunkakinoki/beads";
Expand All @@ -18,7 +20,7 @@ let
# 1.86+ adds the git+https:// remote scheme used by the beads_global GitHub backup.
doltMinVersion = "1.86";
startScript = pkgs.replaceVars ./start.sh {
inherit beadsDir;
inherit beadsDir legacyBeadsDir;
inherit (pkgs) dolt;
};
backupScript = pkgs.replaceVars ./backup-dolt-main.sh {
Expand All @@ -37,6 +39,7 @@ lib.mkIf enabled {
home.sessionVariables = {
BEADS_DOLT_SHARED_SERVER = "1";
BEADS_DOLT_SERVER_PORT = "3307";
BEADS_SHARED_SERVER_DIR = sharedServerDir;
DOLT_CLI_USER = "root";
DOLT_CLI_PASSWORD = "";
};
Expand Down
28 changes: 21 additions & 7 deletions home-manager/services/dolt/start.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
#!/usr/bin/env bash
# @beadsDir@ and @dolt@ are substituted by pkgs.replaceVars.
# @beadsDir@, @legacyBeadsDir@, and @dolt@ are substituted by pkgs.replaceVars.
set -euo pipefail

mkdir -p "@beadsDir@"

# Legacy migration: a `dolt` directory predates the rename to `df`.
# Only migrate when `df` does not exist yet; once `df` is in place the
# `dolt` directory is treated as its own (possibly external) database.
if [ -d "@beadsDir@/dolt" ] && [ ! -L "@beadsDir@/dolt" ] && [ ! -e "@beadsDir@/df" ]; then
mv -f "@beadsDir@/dolt" "@beadsDir@/df"
fi
# Move databases served by the pre-shared-server configuration into the
# canonical root. Existing destinations win, so activation never overwrites a
# newer clone. A legacy database named `dolt` was the old name for `df`.
for legacy_db in "@legacyBeadsDir@"/*; do
if [ ! -d "$legacy_db/.dolt" ] || [ -L "$legacy_db" ]; then
continue
fi

db_name="${legacy_db##*/}"
target_name="$db_name"
if [ "$db_name" = "dolt" ] && [ ! -e "@beadsDir@/df" ]; then
target_name="df"
fi

if [ -e "@beadsDir@/$target_name" ]; then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A dangling symlink at a canonical database path makes Dolt fail to start instead of preserving the existing destination. Treat symlinks as occupied targets before invoking mv.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At home-manager/services/dolt/start.sh, line 21:

<comment>A dangling symlink at a canonical database path makes Dolt fail to start instead of preserving the existing destination. Treat symlinks as occupied targets before invoking `mv`.</comment>

<file context>
@@ -1,15 +1,29 @@
+    target_name="df"
+  fi
+
+  if [ -e "@beadsDir@/$target_name" ]; then
+    continue
+  fi
</file context>
Suggested change
if [ -e "@beadsDir@/$target_name" ]; then
if [ -e "@beadsDir@/$target_name" ] || [ -L "@beadsDir@/$target_name" ]; then

continue
fi

mv -f -- "$legacy_db" "@beadsDir@/$target_name"
done

# Additional databases (e.g. data shared with another repo) are managed by
# the user as real directories under @beadsDir@/<dbname>. dolt sql-server
Expand Down
85 changes: 75 additions & 10 deletions spec/dolt_start_spec.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# shellcheck disable=SC2329
# shellcheck disable=SC2016,SC2329

Describe 'home-manager/services/dolt/start.sh'
SCRIPT="$PWD/home-manager/services/dolt/start.sh"
Expand Down Expand Up @@ -27,26 +27,72 @@ When run bash -c "grep '@beadsDir@' '$SCRIPT'"
The output should include '@beadsDir@'
End

It 'references @legacyBeadsDir@'
When run bash -c "grep '@legacyBeadsDir@' '$SCRIPT'"
The output should include '@legacyBeadsDir@'
End

It 'references @dolt@'
When run bash -c "grep '@dolt@' '$SCRIPT'"
The output should include '@dolt@'
End
End

Describe 'dolt migration behavior'
It 'migrates legacy dolt directory to df'
When run bash -c "grep 'mv -f' '$SCRIPT'"
The output should include 'mv -f'
setup_migration() {
TEST_ROOT=$(mktemp -d)
LEGACY_DIR="$TEST_ROOT/legacy"
SHARED_DIR="$TEST_ROOT/shared/dolt"
FAKE_DOLT="$TEST_ROOT/fake-dolt"
RENDERED_SCRIPT="$TEST_ROOT/start.sh"
mkdir -p "$LEGACY_DIR" "$SHARED_DIR" "$FAKE_DOLT/bin"
printf '%s\n' '#!/usr/bin/env bash' 'exit 0' >"$FAKE_DOLT/bin/dolt"
chmod +x "$FAKE_DOLT/bin/dolt"
sed \
-e "s|@legacyBeadsDir@|$LEGACY_DIR|g" \
-e "s|@beadsDir@|$SHARED_DIR|g" \
-e "s|@dolt@|$FAKE_DOLT|g" \
"$SCRIPT" >"$RENDERED_SCRIPT"
}

cleanup_migration() {
rm -rf "$TEST_ROOT"
}

Before 'setup_migration'
After 'cleanup_migration'

It 'moves a legacy database into the shared-server root'
mkdir -p "$LEGACY_DIR/beads/.dolt"
When run bash "$RENDERED_SCRIPT"
The status should be success
The path "$SHARED_DIR/beads/.dolt" should be directory
The path "$LEGACY_DIR/beads" should not be exist
End

It 'gates the migration on df not yet existing'
When run bash -c "grep -- '-e \"@beadsDir@/df\"' '$SCRIPT'"
The output should include '-e "@beadsDir@/df"'
It 'does not overwrite an existing shared-server database'
mkdir -p "$LEGACY_DIR/df/.dolt" "$SHARED_DIR/df/.dolt"
touch "$LEGACY_DIR/df/.dolt/legacy-marker"
When run bash "$RENDERED_SCRIPT"
The status should be success
The path "$LEGACY_DIR/df/.dolt/legacy-marker" should be file
The path "$SHARED_DIR/df/.dolt" should be directory
End

It 'does not recreate the legacy dolt -> df symlink'
When run bash -c "grep -c 'ln -sfn df' '$SCRIPT' || true"
The output should equal '0'
It 'maps the old dolt database name to df during legacy-root migration'
mkdir -p "$LEGACY_DIR/dolt/.dolt"
When run bash "$RENDERED_SCRIPT"
The status should be success
The path "$SHARED_DIR/df/.dolt" should be directory
The path "$SHARED_DIR/dolt" should not be exist
End

It 'does not rename a legitimate dolt database in the shared-server root'
mkdir -p "$SHARED_DIR/dolt/.dolt"
When run bash "$RENDERED_SCRIPT"
The status should be success
The path "$SHARED_DIR/dolt/.dolt" should be directory
The path "$SHARED_DIR/df" should not be exist
End
End

Expand All @@ -68,3 +114,22 @@ End
End

End

Describe 'home-manager/services/dolt/default.nix'
Comment thread
shunkakinoki marked this conversation as resolved.
MODULE="$PWD/home-manager/services/dolt/default.nix"

It 'uses the canonical Beads shared-server directory'
When run grep -F 'sharedServerDir = "${homeDir}/.beads/shared-server";' "$MODULE"
The output should include 'sharedServerDir = "${homeDir}/.beads/shared-server";'
End

It 'serves databases from the shared-server dolt directory'
When run grep -F 'beadsDir = "${sharedServerDir}/dolt";' "$MODULE"
The output should include 'beadsDir = "${sharedServerDir}/dolt";'
End

It 'exports the same shared-server directory to bd'
When run grep -F 'BEADS_SHARED_SERVER_DIR = sharedServerDir;' "$MODULE"
The output should include 'BEADS_SHARED_SERVER_DIR = sharedServerDir;'
End
End
Loading