Skip to content

Commit 5e82c04

Browse files
authored
Improve migration copy in CLI (#109)
* refactor: improve migration timestamp handling and copying logic - Added helper functions to extract and generate incremental timestamps for migrations - Updated migration copying to assign new timestamps while preserving order - Modified existing migration detection to prevent duplicates based on filename content - Enhanced logging to display styled filenames and migration status more clearly - Removed redundant code and streamlined the migration installation process * chore: add scripts for testing pgflow CLI install and duplicate prevention Update project.json to include new test:e2e:install:duplicates target. Add test-install and test-install-duplicates scripts for automated testing of installation process and migration duplication prevention, including optional timestamp-based duplicate detection. These scripts facilitate end-to-end testing of the CLI's install functionality and ensure no duplicate migrations are created. * fix: improve migration copy logic with detailed status messages Enhance user feedback when copying migrations by displaying detailed information about already installed migrations, including their installation method and timestamps. * fix: improve migration copy logic in CLI for better uniqueness and compatibility Enhance migration copying by always generating a new timestamp-based ID, prepending it to the original filename, and improving source migration matching to ensure backward compatibility with installed migrations. * chore(docs): improve migration instructions and prevent duplication during pgflow setup - Updated documentation to clarify how migration files are copied with timestamps - Added notes on preventing duplicate migrations when rerunning the installer - Enhanced explanation of migration timestamp prefixes and their purpose - Minor formatting adjustments for clarity in the installation guide * refactor: improve timestamp generation logic in copy-migrations script - Simplify function parameter default assignment - Enhance timestamp parsing robustness for migration file handling * chore: update supabase init command to use --force flag in test scripts - Added --force flag to supabase init commands in test-install and test-install-duplicates scripts to ensure clean initialization - Minor formatting adjustments for consistency in test scripts * chore: update test dependency order and add test-install-duplicates script Refactors test task dependencies to include compile step and updates test-install commands to run both install and install-duplicates scripts. * fix: validate timestamp format more strictly in copy-migrations script Enhance the timestamp validation to ensure it matches a 14-digit numeric pattern, preventing invalid timestamps from being processed. This improves robustness when generating new timestamps based on filenames. * fix: improve timestamp generation and migration copying logic - Adjusted generateNewTimestamp to ensure new timestamps are always at least one greater than the reference - Corrected existing files reading to handle undefined or missing files safely - Enhanced migration filename handling to accurately identify already installed migrations - Updated progress and summary messages for clarity and consistency - Minor formatting and code style improvements for better maintainability * refactor: enhance timestamp parsing and generation functions for migration scripts - Added helper functions to format Date objects into timestamp strings - Implemented robust parsing of timestamp strings into Date objects with validation - Updated generateNewTimestamp to produce incremented timestamps that are always forward-moving - Improved handling of invalid or unparseable timestamps to default to current time - Overall, these changes improve reliability and correctness of migration timestamp management * fix: improve migration filename parsing and timestamp validation Refactors getTimestampFromFilename to ensure correct length and format validation. Enhances timestamp comparison logic to consider only validated timestamps when updating latestTimestamp.
1 parent d2968de commit 5e82c04

File tree

6 files changed

+342
-40
lines changed

6 files changed

+342
-40
lines changed

.changeset/short-zebras-wish.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'pgflow': patch
3+
---
4+
5+
Improve migration copy functionality in CLI
6+
7+
- always generate a new timestamp-id for the copied migration
8+
- prepend the timestamp to the original migration name
9+
- when comparing installed migrations to source migrations, search for source migration filename
10+
11+
This change should be backwards compatible with already installed migrations,
12+
because the matching logic will just match the source filename in destination folder.

pkgs/cli/project.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,22 @@
3131
},
3232
"test": {
3333
"executor": "nx:noop",
34-
"dependsOn": ["test:e2e:install", "test:e2e:compile"]
34+
"dependsOn": [
35+
"test:e2e:install",
36+
"test:e2e:install:duplicates",
37+
"test:e2e:compile"
38+
],
39+
"options": {
40+
"parallel": false
41+
}
3542
},
3643
"test:e2e:install": {
3744
"executor": "nx:run-commands",
3845
"dependsOn": ["build"],
3946
"options": {
4047
"commands": [
41-
"rm -rf supabase/",
42-
"npx -y supabase@latest init --with-vscode-settings --with-intellij-settings",
43-
"node dist/index.js install --supabase-path supabase/ --yes",
44-
"./scripts/assert-pgflow-installed"
48+
"./scripts/test-install",
49+
"./scripts/test-install-duplicates"
4550
],
4651
"cwd": "{projectRoot}",
4752
"parallel": false

pkgs/cli/scripts/test-install

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Script to test pgflow CLI install functionality
5+
# This simulates the test:e2e:install target from project.json
6+
7+
cd "$(dirname "$0")/.."
8+
ROOT_DIR=$(pwd)
9+
10+
echo "🧪 Testing pgflow install functionality"
11+
12+
# Clean up any existing supabase directory
13+
echo "🧹 Cleaning up old test directory"
14+
rm -rf supabase/
15+
16+
# Initialize a fresh Supabase project
17+
echo "🏗️ Creating new Supabase project"
18+
npx -y supabase@latest init --force --with-vscode-settings --with-intellij-settings
19+
20+
# Install pgflow with our CLI
21+
echo "📦 Installing pgflow with CLI"
22+
node dist/index.js install --supabase-path supabase/ --yes
23+
24+
# Verify installation succeeded
25+
echo "✅ Verifying pgflow installation"
26+
"$ROOT_DIR/scripts/assert-pgflow-installed"
27+
28+
# Show success message
29+
echo "✨ Installation test complete"
30+
31+
# Optional: Test for duplicates by running install again
32+
if [ "$1" == "--test-duplicates" ]; then
33+
echo "🔄 Testing duplicate installation prevention"
34+
node dist/index.js install --supabase-path supabase/ --yes
35+
fi
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Script to test pgflow CLI install duplicate prevention
5+
# This verifies that we don't create duplicate migrations
6+
7+
cd "$(dirname "$0")/.."
8+
ROOT_DIR=$(pwd)
9+
10+
echo "🧪 Testing pgflow migration duplicate prevention"
11+
12+
# Clean up any existing supabase directory
13+
echo "🧹 Cleaning up old test directory"
14+
rm -rf supabase/
15+
16+
# Initialize a fresh Supabase project
17+
echo "🏗️ Creating new Supabase project"
18+
npx -y supabase@latest init --force --with-vscode-settings --with-intellij-settings
19+
20+
# First installation with pgflow CLI
21+
echo "📦 First pgflow installation"
22+
node dist/index.js install --supabase-path supabase/ --yes
23+
24+
# Count number of migrations after first install
25+
FIRST_COUNT=$(find supabase/migrations -name "*.sql" | wc -l)
26+
echo "🔢 Found $FIRST_COUNT migrations after first install"
27+
28+
# Second installation with pgflow CLI
29+
echo "🔄 Running second pgflow installation"
30+
node dist/index.js install --supabase-path supabase/ --yes
31+
32+
# Count number of migrations after second install
33+
SECOND_COUNT=$(find supabase/migrations -name "*.sql" | wc -l)
34+
echo "🔢 Found $SECOND_COUNT migrations after second install"
35+
36+
# Verify no duplicates were created
37+
if [ "$FIRST_COUNT" -eq "$SECOND_COUNT" ]; then
38+
echo "✅ Success: No duplicate migrations were created"
39+
else
40+
echo "❌ Error: Duplicate migrations detected ($SECOND_COUNT - $FIRST_COUNT = $((SECOND_COUNT - FIRST_COUNT)) new files)"
41+
exit 1
42+
fi
43+
44+
# Optional: Run a third time with different timestamps
45+
if [ "$1" == "--test-third-install" ]; then
46+
# Modify a migration file timestamp to simulate a user renaming it
47+
RANDOM_MIGRATION=$(find supabase/migrations -name "*.sql" | head -1)
48+
NEW_NAME=$(echo "$RANDOM_MIGRATION" | sed 's/[0-9]\{14\}_/99999999999999_/')
49+
50+
echo "🔄 Renaming $RANDOM_MIGRATION to $NEW_NAME"
51+
mv "$RANDOM_MIGRATION" "$NEW_NAME"
52+
53+
echo "🔄 Running third pgflow installation"
54+
node dist/index.js install --supabase-path supabase/ --yes
55+
56+
# Count number of migrations after third install
57+
THIRD_COUNT=$(find supabase/migrations -name "*.sql" | wc -l)
58+
echo "🔢 Found $THIRD_COUNT migrations after third install"
59+
60+
if [ "$SECOND_COUNT" -eq "$THIRD_COUNT" ]; then
61+
echo "✅ Success: No duplicate migrations were created (even with timestamp changes)"
62+
else
63+
echo "❌ Error: Duplicate migrations detected after timestamp changes"
64+
exit 1
65+
fi
66+
fi

0 commit comments

Comments
 (0)