Skip to content

Commit 19dca39

Browse files
committed
feat: Add migration fixer script (for branches)
1 parent f05609b commit 19dca39

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER TABLE workspace_agents DROP COLUMN startup_script_timeout_seconds;
2+
ALTER TABLE workspace_agents DROP COLUMN delay_login_until_ready;
3+
4+
ALTER TABLE workspace_agents DROP COLUMN lifecycle_state;
5+
6+
DROP TYPE workspace_agent_lifecycle_state;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE TYPE workspace_agent_lifecycle_state AS ENUM ('created', 'starting', 'start_timeout', 'start_error', 'ready');
2+
3+
-- Set all existing workspace agents to 'ready' so that only newly created agents will be in the 'created' state.
4+
ALTER TABLE workspace_agents ADD COLUMN lifecycle_state workspace_agent_lifecycle_state NOT NULL DEFAULT 'ready';
5+
-- Change the default for newly created agents.
6+
ALTER TABLE workspace_agents ALTER COLUMN lifecycle_state SET DEFAULT 'created';
7+
8+
COMMENT ON COLUMN workspace_agents.lifecycle_state IS 'The current lifecycle state reported by the workspace agent.';
9+
10+
-- Set default values that conform to current behavior.
11+
-- Allow logins immediately after agent connect.
12+
ALTER TABLE workspace_agents ADD COLUMN delay_login_until_ready boolean NOT NULL DEFAULT false;
13+
14+
COMMENT ON COLUMN workspace_agents.delay_login_until_ready IS 'If true, the agent will delay logins until it is ready (e.g. executing startup script has ended).';
15+
16+
-- Disable startup script timeouts by default.
17+
ALTER TABLE workspace_agents ADD COLUMN startup_script_timeout_seconds int4 NOT NULL DEFAULT 0;
18+
19+
COMMENT ON COLUMN workspace_agents.startup_script_timeout_seconds IS 'The number of seconds to wait for the startup script to complete. If the script does not complete within this time, the agent lifecycle will be marked as start_timeout.';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE template_version_parameters DROP CONSTRAINT validation_monotonic_order;
2+
3+
ALTER TABLE template_version_parameters DROP COLUMN validation_monotonic;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER TABLE template_version_parameters ADD COLUMN validation_monotonic text NOT NULL DEFAULT '';
2+
3+
ALTER TABLE template_version_parameters ADD CONSTRAINT validation_monotonic_order CHECK (validation_monotonic IN ('increasing', 'decreasing', ''));
4+
5+
COMMENT ON COLUMN template_version_parameters.validation_monotonic
6+
IS 'Validation: consecutive values preserve the monotonic order';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
5+
6+
list_migrations() {
7+
branch=$1
8+
git ls-tree -r --name-only "${branch}" -- . | grep -E '[0-9]{6}.*' | sort -n
9+
}
10+
11+
main() {
12+
cd "${SCRIPT_DIR}"
13+
14+
curr_num=$(
15+
set -e
16+
list_migrations main | grep '^[0-9]' | tail -n1
17+
)
18+
echo "Last migration (main): ${curr_num}"
19+
next_num=$(("1${curr_num:0:6}" - 1000000 + 1))
20+
curr_num=$(printf "%06d" "${next_num}")
21+
echo "Next migration number: ${curr_num}"
22+
23+
main_files="$(
24+
set -e
25+
list_migrations main
26+
)"
27+
head_files="$(
28+
set -e
29+
list_migrations HEAD
30+
)"
31+
32+
declare -A prefix_map=()
33+
declare -a git_add_files=()
34+
35+
# Renumber migrations part of this branch (as compared to main)
36+
diff_files="$(diff -u <(echo "${main_files}") <(echo "${head_files}") | sed -E -ne 's;^\+(0.*);\1;p' | sort -n || true)"
37+
if [[ -z "${diff_files}" ]]; then
38+
echo "No migrations to rename, exiting."
39+
return
40+
fi
41+
while read -r file; do
42+
old_file="${file}"
43+
dir=$(dirname "${file}")
44+
file=$(basename "${file}")
45+
num="${file:0:6}"
46+
set +u
47+
new_num="${prefix_map["${num}"]}"
48+
set -u
49+
if [[ -z "${new_num}" ]]; then
50+
new_num="${curr_num}"
51+
prefix_map["${num}"]="${new_num}"
52+
next_num=$((next_num + 1))
53+
curr_num=$(printf "%06d" "${next_num}")
54+
fi
55+
name="${file:7:-4}"
56+
new_file="${new_num}_${name}.sql"
57+
echo "Renaming ${old_file} to ${new_file}"
58+
mv "${old_file}" "${new_file}"
59+
git_add_files+=("${new_file}" "${old_file}")
60+
done <<<"${diff_files}"
61+
62+
# Renumber fixtures if there's a matching migration in this branch (as compared to main).
63+
diff_files="$(diff -u <(echo "${main_files}") <(echo "${head_files}") | sed -E -ne 's;^\+(testdata/[^/]*/0.*);\1;p' | sort -n || true)"
64+
if [[ -z "${diff_files}" ]]; then
65+
echo "No testdata fixtures to rename, skipping."
66+
return
67+
fi
68+
while read -r file; do
69+
old_file="${file}"
70+
dir=$(dirname "${file}")
71+
file=$(basename "${file}")
72+
num="${file:0:6}"
73+
set +u
74+
new_num="${prefix_map["${num}"]}"
75+
set -u
76+
if [[ -z "${new_num}" ]]; then
77+
echo "Skipping ${old_file}, no matching migration in ${SCRIPT_DIR}"
78+
continue
79+
fi
80+
name="${file:7:-4}"
81+
new_file="${dir}/${new_num}_${name}.sql"
82+
echo "Renaming ${old_file} to ${new_file}"
83+
mv "${old_file}" "${new_file}"
84+
git_add_files+=("${new_file}" "${old_file}")
85+
done <<<"${diff_files}"
86+
87+
git add "${git_add_files[@]}"
88+
git status
89+
echo "Run 'git commit' to commit the changes."
90+
}
91+
92+
(main "$@")

0 commit comments

Comments
 (0)