Mercurial > hgrepos > hgweb.cgi > s4
annotate s4-migrate.sh @ 824:fcf485cbabc6
At migration, translate direct-link to attachment files
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Sat, 20 Jun 2020 18:18:17 +0900 |
parents | fc6855839333 |
children | 8c69e0c2e6af |
rev | line source |
---|---|
779 | 1 #!/bin/sh |
2 # s4 - migration module | |
3 # (C)2020 by HIROSE, Yuuji | |
4 | |
5 srcdb=`unset DB; [ -f $1 ] && . ./$1 && echo ${DB:-db/cgi.sq3}` | |
6 dst=`unset DB; [ -f $2 ] && . ./$2 && echo "${DB:-db/cgi.sq3}|$URL"` | |
7 dstdb=${dst%\|*} | |
8 dsturl=${dst##*\|} | |
9 type htmlescape >/dev/null 2>&1 || . `dirname $1`/s4-funcs.sh | |
780
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
10 case "$2" in |
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
11 s4-config.sh) world=Base ;; |
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
12 *) world=${2##*-config-}; world=${world%.*} ;; |
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
13 esac |
779 | 14 htmlworld=`echo "$world"|htmlescape` |
15 | |
786
809caeb20758
Migration caller set arguments beforehand
HIROSE Yuuji <yuuji@gentei.org>
parents:
784
diff
changeset
|
16 err "--- Migration Started with \$1=$1 \$2=$2 at `date` ---" |
780
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
17 err srcdb=$srcdb dstdb=$dstdb |
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
18 err URL=$URL |
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
19 err dstURL=$dsturl |
779 | 20 |
21 shift 2 | |
22 | |
23 query "ATTACH DATABASE \"$dstdb\" AS dst;" || abort "Cannot attach db #{dstdb}" | |
24 | |
25 failure=0 | |
26 for grid; do | |
27 grid=$((0 + $grid)) | |
28 gnamesql="(SELECT gname FROM main.grp WHERE rowid=$grid)" | |
29 grp=`query "SELECT gname FROM main.grp WHERE rowid=$grid;"` | |
30 htmlgrp=`echo "$grp"|htmlescape` | |
31 qgrp=`sqlquote "$grp"` | |
794
8448724f69e3
Account sync-ing and migration fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
792
diff
changeset
|
32 if [ -n "`query \"SELECT gname FROM dst.grp WHERE gname=$qgrp;\"`" ]; then |
779 | 33 echo "[$htmlgrp]グループがWorld[$htmlworld]にあるので中止します。"|html p |
34 failure=$((failure+1)) | |
35 continue | |
36 fi | |
37 echo "Copying $grid..." | |
38 query "BEGIN;" | |
39 query "REPLACE INTO dst.grp SELECT * FROM main.grp WHERE rowid=$grid;" | |
40 destrowid=`query "SELECT last_insert_rowid();"` | |
41 for tbl in grp_s grp_m grp_mem grp_mem_s grp_mem_m \ | |
42 grp_adm grp_adm_s grp_adm_m; do | |
43 query "REPLACE INTO dst.$tbl SELECT * FROM main.$tbl | |
44 WHERE gname=$gnamesql;" | |
45 done | |
46 blogs=`query "SELECT group_concat(\"'\"||id||\"'\", ',') | |
47 FROM main.blog_s WHERE key='owner' AND val=$gnamesql;"` | |
48 echo blogs=$blogs | |
49 for tbl in blog blog_s blog_m; do | |
50 query <<-EOF | |
51 REPLACE INTO dst.$tbl | |
52 SELECT * FROM main.$tbl | |
53 WHERE id IN ($blogs); | |
54 EOF | |
55 done | |
784 | 56 for tbl in article article_s article_m; do |
57 query <<-EOF | |
779 | 58 REPLACE INTO dst.$tbl |
59 SELECT * FROM main.$tbl | |
784 | 60 WHERE id IN (SELECT id FROM main.article WHERE blogid IN ($blogs)) |
61 ORDER BY rowid; | |
779 | 62 EOF |
63 done | |
64 ## Check the equality of two DBs | |
65 echo "grid=$grid grp=$grp qgrp=$qgrp" | htmlescape | |
66 # grp | |
67 d1=$(query <<-EOF | |
68 SELECT * FROM main.grp | |
69 NATURAL LEFT JOIN main.grp_s | |
70 NATURAL LEFT JOIN main.grp_m | |
71 WHERE gname=$qgrp | |
72 EXCEPT | |
73 SELECT * FROM dst.grp | |
74 NATURAL LEFT JOIN dst.grp_s | |
75 NATURAL LEFT JOIN dst.grp_m | |
76 WHERE gname=$qgrp; | |
77 EOF | |
78 ) | |
79 err DONE | |
80 err d1="$d1" | |
81 # blog | |
82 d2=$(query <<-EOF | |
83 SELECT * FROM main.blog | |
84 NATURAL LEFT JOIN main.blog_s | |
85 NATURAL LEFT JOIN main.blog_m | |
86 WHERE id IN (SELECT id FROM main.blog_s | |
87 WHERE key='owner' AND val=$qgrp) | |
88 EXCEPT | |
89 SELECT * FROM dst.blog | |
90 NATURAL LEFT JOIN dst.blog_s | |
91 NATURAL LEFT JOIN dst.blog_m | |
92 WHERE id IN (SELECT id FROM dst.blog_s | |
93 WHERE key='owner' AND val=$qgrp); | |
94 EOF | |
95 ) | |
786
809caeb20758
Migration caller set arguments beforehand
HIROSE Yuuji <yuuji@gentei.org>
parents:
784
diff
changeset
|
96 err d2="$d2" |
779 | 97 # article |
98 d3=$(query <<-EOF | |
99 SELECT * FROM main.article | |
100 NATURAL LEFT JOIN main.article_s | |
101 NATURAL LEFT JOIN main.article_m | |
102 WHERE blogid IN ($blogs) | |
103 EXCEPT | |
104 SELECT * FROM dst.article | |
105 NATURAL LEFT JOIN dst.article_s | |
106 NATURAL LEFT JOIN dst.article_m | |
107 WHERE blogid IN ($blogs); | |
108 EOF | |
109 ) | |
786
809caeb20758
Migration caller set arguments beforehand
HIROSE Yuuji <yuuji@gentei.org>
parents:
784
diff
changeset
|
110 err d3="$d3" |
779 | 111 if [ -z "$d1$d2$d3" ]; then |
821
2465fff63ad9
Old group deletion at migration done in a transaction
HIROSE Yuuji <yuuji@gentei.org>
parents:
813
diff
changeset
|
112 echo "Copy and verification done, rewriting URL in articles..." |
780
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
113 echo "Old URL: $URL" |
779 | 114 echo "New URL: $dsturl" |
115 query <<-EOF | |
116 UPDATE dst.article_s | |
790 | 117 SET val=replace(val, |
118 '${URL}?grp+$grid', | |
119 '${dsturl}?grp+$destrowid') | |
120 WHERE key='text' AND val LIKE '%${URL}%'; | |
792 | 121 EOF |
779 | 122 # Create blog-rowid conversion table |
123 sedfile=$tmpd/arttrans.sed | |
786
809caeb20758
Migration caller set arguments beforehand
HIROSE Yuuji <yuuji@gentei.org>
parents:
784
diff
changeset
|
124 # sedfile=tmp/arttrans.sed |
779 | 125 query <<-EOF > $sedfile |
126 WITH arttrans AS ( | |
127 SELECT s.rowid srcrid, d.rowid dstrid | |
128 FROM main.article s JOIN dst.article d ON s.id=d.id | |
129 WHERE s.id in (SELECT id | |
130 FROM article WHERE blogid IN ($blogs)) | |
795
6164d88fbc0e
Update replyblog URL with ERE
HIROSE Yuuji <yuuji@gentei.org>
parents:
794
diff
changeset
|
131 ) SELECT printf("/^>/s/\#%s($|[^0-9])/\#%s\1/g", srcrid, dstrid) |
779 | 132 FROM arttrans; |
133 EOF | |
134 query <<-EOF > $tmpd/repl.art.rowid | |
135 SELECT rowid FROM dst.article_s | |
136 WHERE key='text' AND val GLOB '>*#[1-9]*' | |
137 AND id IN (SELECT id FROM article WHERE blogid IN ($blogs)); | |
138 EOF | |
139 sql=$tmpd/update.sql | |
140 for arid in `cat $tmpd/repl.art.rowid`; do | |
141 newval=`query "SELECT hex(val) FROM dst.article_s WHERE rowid=$arid;" \ | |
786
809caeb20758
Migration caller set arguments beforehand
HIROSE Yuuji <yuuji@gentei.org>
parents:
784
diff
changeset
|
142 | unhexize | sed -Ef "$sedfile" | hexize` |
813
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
143 echo "UPDATE dst.article_s SET val=X'$newval' WHERE rowid=$arid;" |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
144 done >$sql |
779 | 145 # Rewrite blog-links in the group |
146 # Create sed script | |
147 sedfile2=${sedfile}2 | |
148 query <<-EOF > $sedfile2 | |
149 WITH blogtrans AS ( | |
150 SELECT s.rowid srcrid, d.rowid dstrid | |
151 FROM main.blog s JOIN dst.blog d ON s.id=d.id | |
152 WHERE s.id IN ($blogs) | |
795
6164d88fbc0e
Update replyblog URL with ERE
HIROSE Yuuji <yuuji@gentei.org>
parents:
794
diff
changeset
|
153 ) SELECT printf('s/(\?replyblog)\+%s($|[^0-9])/\1+%s\2/g', |
779 | 154 srcrid, dstrid) |
155 FROM blogtrans; | |
156 EOF | |
157 bloglinks=$tmpd/bloglinks.rowid | |
158 query <<-EOF > $bloglinks | |
159 SELECT rowid FROM dst.article_s | |
160 WHERE key='text' AND val LIKE '%?replyblog+%' | |
161 AND id IN (SELECT id FROM article WHERE blogid IN ($blogs)); | |
162 EOF | |
163 for arid in `cat $bloglinks`; do | |
164 newval=`query "SELECT hex(replace(val, '$URL', '$dsturl')) | |
165 FROM dst.article_s WHERE rowid=$arid;" \ | |
795
6164d88fbc0e
Update replyblog URL with ERE
HIROSE Yuuji <yuuji@gentei.org>
parents:
794
diff
changeset
|
166 | unhexize | sed -Ef "$sedfile2" | hexize` |
779 | 167 echo "UPDATE dst.article_s SET val=X'$newval' WHERE rowid=$arid;" >>$sql |
168 done | |
824
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
169 # Rewrite showattc Direct-Links |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
170 attclinks=$( |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
171 query <<-EOF |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
172 SELECT group_concat(rowid, ',') FROM dst.article_s |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
173 WHERE key='text' AND val LIKE '%?showattc+article_m+%' |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
174 AND id IN (SELECT id FROM article WHERE blogid IN ($blogs)); |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
175 EOF |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
176 ) |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
177 if [ -n "$attclinks" ]; then |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
178 err attclinks=$attclinks |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
179 sedfile3=${sedfile}3 |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
180 query <<-EOF > $sedfile3 |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
181 WITH attctrans AS ( |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
182 SELECT s.rowid srcarid, d.rowid dstarid |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
183 FROM main.article_m s NATURAL JOIN dst.article_m d |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
184 WHERE d.id IN (SELECT id FROM dst.article |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
185 WHERE blogid IN ($blogs)) |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
186 ) SELECT printf('s/(\?showattc\+article_m)\+%s($|[^0-9])/\1+%s\2/g', |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
187 srcarid, dstarid) |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
188 FROM attctrans; |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
189 EOF |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
190 echo $attclinks | tr , '\n' | while read arid; do |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
191 newval=`query "SELECT hex(val) FROM dst.article_s WHERE rowid=$arid;" \ |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
192 | unhexize | sed -Ef "$sedfile3" | hexize` |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
193 echo "UPDATE dst.article_s SET val=X'$newval' WHERE rowid=$arid;" |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
194 done >> $sql |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
195 fi |
813
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
196 if [ -z "`query \".read $sql\"`" ]; then |
824
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
197 echo "URL rewriting done." |
fcf485cbabc6
At migration, translate direct-link to attachment files
HIROSE Yuuji <yuuji@gentei.org>
parents:
822
diff
changeset
|
198 echo -n "Removing the group in the old world..." |
779 | 199 query <<-EOF |
821
2465fff63ad9
Old group deletion at migration done in a transaction
HIROSE Yuuji <yuuji@gentei.org>
parents:
813
diff
changeset
|
200 SAVEPOINT rmgroup; |
779 | 201 DELETE FROM main.article WHERE blogid IN ($blogs); |
202 DELETE FROM main.blog WHERE id IN ($blogs); | |
203 DELETE FROM main.grp WHERE rowid=$grid; | |
821
2465fff63ad9
Old group deletion at migration done in a transaction
HIROSE Yuuji <yuuji@gentei.org>
parents:
813
diff
changeset
|
204 RELEASE SAVEPOINT rmgroup; |
779 | 205 EOF |
813
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
206 s=`query "SELECT * FROM main.grp WHERE rowid=$grid;"` |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
207 if [ -z "$s" ]; then |
821
2465fff63ad9
Old group deletion at migration done in a transaction
HIROSE Yuuji <yuuji@gentei.org>
parents:
813
diff
changeset
|
208 echo "Done." |
813
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
209 echo "Success!!" |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
210 query "END;" |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
211 clean-orphaned |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
212 echo "Done." |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
213 else |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
214 echo Removal failed |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
215 echo "現行グループ消去ができませんでした。" |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
216 echo "書き込みの多いグループの場合は空いている時間帯に試して下さい。" |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
217 query "ROLLBACK;" |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
218 failure=-2 |
04034092338d
Removing group of previous world, confirmation added
HIROSE Yuuji <yuuji@gentei.org>
parents:
795
diff
changeset
|
219 fi |
779 | 220 else |
221 failure=-1 | |
222 echo "Replacing failed." | |
223 query "ROLLBACK;" | |
224 fi | |
225 else | |
226 failure=$((failure + 1)) | |
227 echo "Fail!" | |
228 query "ROLLBACK;" | |
229 fi | |
230 done | |
231 | |
780
b23f26406690
Migration logging and messages fixed
HIROSE Yuuji <yuuji@gentei.org>
parents:
779
diff
changeset
|
232 err "Migration ended at `date` with failure=$failure" |
779 | 233 return $failure |