-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_migration.sh
More file actions
executable file
·144 lines (111 loc) · 4.1 KB
/
db_migration.sh
File metadata and controls
executable file
·144 lines (111 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# CodePatchwork Database Migration Script
# This script exports database schema and data for local PostgreSQL import
set -e # Exit immediately if a command exits with a non-zero status
echo "🚀 Starting CodePatchwork database migration..."
# Check if DATABASE_URL environment variable is set
if [ -z "$DATABASE_URL" ]; then
echo "❌ Error: DATABASE_URL environment variable is not set."
echo "Please set the DATABASE_URL variable before running this script."
exit 1
fi
# Create an export directory if it doesn't exist
EXPORT_DIR="./db-export"
mkdir -p "$EXPORT_DIR"
echo "📂 Created export directory at $EXPORT_DIR"
# Extract database connection details from DATABASE_URL
# Format: postgres://username:password@host:port/database
# Extract database connection details from DATABASE_URL
# Format: postgresql://username:password@host:port/database
if [[ "$DATABASE_URL" == postgresql://* ]]; then
DB_URL=${DATABASE_URL#postgresql://}
else
DB_URL=${DATABASE_URL#postgres://}
fi
DB_USER=$(echo $DB_URL | cut -d':' -f1)
DB_PASS=$(echo $DB_URL | cut -d':' -f2 | cut -d'@' -f1)
DB_HOST_PORT=$(echo $DB_URL | cut -d'@' -f2 | cut -d'/' -f1)
DB_HOST=$(echo $DB_HOST_PORT | cut -d':' -f1)
DB_PORT=$(echo $DB_HOST_PORT | cut -d':' -f2)
DB_NAME=$(echo $DB_URL | cut -d'/' -f2 | cut -d'?' -f1)
echo "ℹ️ Detected database: $DB_NAME on $DB_HOST:$DB_PORT"
# Export schema (structure only, no data)
echo "📤 Exporting database schema..."
pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \
--schema-only \
--no-owner \
--no-acl \
--format=plain \
> "$EXPORT_DIR/schema.sql"
echo "✅ Schema exported to $EXPORT_DIR/schema.sql"
# Export data (all tables, no schema)
echo "📤 Exporting database data..."
pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \
--data-only \
--no-owner \
--no-acl \
--format=plain \
> "$EXPORT_DIR/data.sql"
echo "✅ Data exported to $EXPORT_DIR/data.sql"
# Create a complete dump (schema + data)
echo "📤 Creating complete database dump..."
pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \
--no-owner \
--no-acl \
--format=custom \
> "$EXPORT_DIR/complete_dump.dump"
echo "✅ Complete dump created at $EXPORT_DIR/complete_dump.dump"
# Create a README file with import instructions
cat > "$EXPORT_DIR/README.md" << EOF
# CodePatchwork Database Migration
This folder contains database export files to migrate your CodePatchwork database to a local PostgreSQL instance.
## Files
- \`schema.sql\`: Database schema definition (tables, constraints, etc.)
- \`data.sql\`: Database data only (no schema)
- \`complete_dump.dump\`: Complete database export in PostgreSQL custom format
## Import Instructions
### Option 1: Using the complete dump (recommended)
1. Create a new database:
\`\`\`bash
createdb codepatchwork
\`\`\`
2. Import the complete dump:
\`\`\`bash
pg_restore -d codepatchwork --no-owner --role=your_username complete_dump.dump
\`\`\`
### Option 2: Using separate schema and data files
1. Create a new database:
\`\`\`bash
createdb codepatchwork
\`\`\`
2. Import the schema:
\`\`\`bash
psql -d codepatchwork -f schema.sql
\`\`\`
3. Import the data:
\`\`\`bash
psql -d codepatchwork -f data.sql
\`\`\`
## Database Configuration
After import, update your local .env file with:
\`\`\`
DATABASE_URL=postgres://your_username:your_password@localhost:5432/codepatchwork
\`\`\`
Replace \`your_username\` and \`your_password\` with your local PostgreSQL credentials.
EOF
echo "✅ README with import instructions created at $EXPORT_DIR/README.md"
# Create a zip file of all exported files
ZIP_FILE="codepatchwork-db-export.zip"
echo "🔒 Creating zip archive of all export files..."
(cd "$EXPORT_DIR" && zip -r "../$ZIP_FILE" .)
echo ""
echo "✅ Database migration completed!"
echo ""
echo "📦 All files are exported to: $EXPORT_DIR/"
echo "📦 Zip archive created: $ZIP_FILE"
echo ""
echo "Next steps:"
echo "1. Download the zip archive to your local machine"
echo "2. Follow the instructions in $EXPORT_DIR/README.md to import the database"
echo ""
echo "Thank you for using CodePatchwork! 🎉"