-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.sh
executable file
·75 lines (68 loc) · 2.41 KB
/
migrate.sh
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
#!/bin/sh
python3 backend/models.py
sqlite3 data/link2.sqlite <<EOF
ATTACH DATABASE 'data/link1.sqlite' AS old;
PRAGMA foreign_keys = ON;
INSERT INTO user (id, username, password, email)
SELECT id, username, password, COALESCE(email, "")
FROM old.user;
INSERT INTO friendship (friend_a_id, friend_b_id, confirmed)
SELECT friend_a_id, friend_b_id, confirmed
FROM old.friendship;
INSERT INTO survey (id, name, description, long_description, user_id)
SELECT id, name, description, long_description, user_id
FROM old.survey;
INSERT INTO question (id, survey_id, "order", section, text, extra)
SELECT id, survey_id, "order", section, text, extra
FROM old.question AS old_question
WHERE old_question.flip_id IS NULL;
INSERT INTO question (id, survey_id, "order", section, text, flip, extra)
SELECT id, survey_id, "order", section, text, (SELECT text FROM old.question AS oq2 WHERE oq2.id=old_question.flip_id), extra
FROM old.question AS old_question
WHERE old_question.flip_id IS NOT NULL AND old_question.id < old_question.flip_id;
INSERT INTO response (id, user_id, survey_id, privacy)
SELECT id, user_id, survey_id,
CASE privacy
WHEN 'public' THEN 'PUBLIC'
WHEN 'hidden' THEN 'ANONYMOUS'
WHEN 'private' THEN 'ANONYMOUS'
WHEN 'friends' THEN 'FRIENDS'
ELSE NULL
END
FROM old.response;
INSERT INTO answer (response_id, question_id, value, flip)
SELECT response_id, question_id,
CASE value
WHEN 2 THEN 'WANT'
WHEN 1 THEN 'WILL'
WHEN 0 THEN 'NA'
WHEN -1 THEN 'NA'
WHEN -2 THEN 'WONT'
ELSE NULL
END,
CASE COALESCE(
(
SELECT value
FROM old.answer AS oa2
WHERE oa2.response_id=old_answer.response_id
AND oa2.question_id=(
SELECT flip_id
FROM old.question AS old_question
WHERE old_question.id=old_answer.question_id
)
),
0
)
WHEN 2 THEN 'WANT'
WHEN 1 THEN 'WILL'
WHEN 0 THEN 'NA'
WHEN -1 THEN 'NA'
WHEN -2 THEN 'WONT'
ELSE NULL
END
FROM old.answer AS old_answer
WHERE old_answer.question_id IN (
SELECT id
FROM question
);
EOF