aboutsummaryrefslogtreecommitdiff
path: root/migrate_2_to_3.sh
blob: a56382b3e0462db5ed57c66bb2f7ebfa22aec4ab (plain)
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
#!/usr/bin/env bash
set -euo pipefail
INDB=db.db
OUTDB=db_migrated.db

inversion="$(sqlite3 "$INDB" 'select version from Meta')"

if [[ $inversion != "2" ]]; then
	echo >&2 "$0 migrates from version 2 to version 3, but the database is at version $inversion."
	exit 1
fi

if [[ -f "$OUTDB" ]]; then
	echo >&2 "Output database $OUTDB already exists; remove before running this script"
	exit 1
fi

sqlite3 "$OUTDB" <schema.sql

sqlite3 "$INDB" '.dump' |
	sed '/^INSERT INTO/!d' |
	sqlite3 "$OUTDB"

sqlite3 "$OUTDB" 'update Meta set version = 3'

echo "Migrated '$INDB' to '$OUTDB'."