1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use DBI; |
5 |
use CouchDB::Client; |
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
$|++; |
9 |
|
10 |
my $database = 'reblog'; |
11 |
|
12 |
my $dbi = "DBI:mysql:database=$database"; |
13 |
$dbi .= ";host=127.0.0.1;port=13306"; # XXX over ssh |
14 |
|
15 |
my $sql = qq{ |
16 |
select |
17 |
items.id as _id, |
18 |
items.*, |
19 |
feeds.url as feed_url, |
20 |
feeds.title as feed_title, |
21 |
feeds.link as feed_link, |
22 |
feeds.description as feed_description |
23 |
from items |
24 |
join items_userdata on items.id = item_id |
25 |
join feeds on items.feed_id = feeds.id |
26 |
where items.id > ? |
27 |
order by items.id asc |
28 |
limit 5000 |
29 |
}; |
30 |
|
31 |
|
32 |
my $c = CouchDB::Client->new(uri => 'http://localhost:5984/'); |
33 |
|
34 |
$c->testConnection or die "The server cannot be reached"; |
35 |
print "CouchDB version " . $c->serverInfo->{version} . "\n"; |
36 |
my $db = $c->newDB( $database ); |
37 |
$db->create unless $c->dbExists( $database ); |
38 |
|
39 |
my $status = $db->newDoc( '_sync' ); |
40 |
eval { $status->retrieve }; |
41 |
$status->create if $@; |
42 |
|
43 |
print "status ",dump( $status->{data} ), "\n"; |
44 |
|
45 |
my $last_row = $status->{data}->{last_row_id} || 0; |
46 |
|
47 |
sub commit_last_row { |
48 |
warn "commit_last_row $last_row\n"; |
49 |
$status->{data}->{last_row_id} = $last_row; |
50 |
$status->update; |
51 |
} |
52 |
|
53 |
my $dbh = DBI->connect($dbi,"","") || die $DBI::errstr; |
54 |
|
55 |
print "Fetching items from $dbi id > $last_row\n"; |
56 |
|
57 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
58 |
$sth->execute( $last_row ) || die $sth->errstr(); |
59 |
|
60 |
warn dump( $sth->{NAME} ); |
61 |
|
62 |
print "found ",$sth->rows," items to process..."; |
63 |
|
64 |
my $pk = 'id'; |
65 |
|
66 |
my $count = 0; |
67 |
|
68 |
while (my $row = $sth->fetchrow_hashref() ) { |
69 |
my $_id = $row->{_id} || die "row needs _id"; |
70 |
my $doc = $db->newDoc( $_id ); |
71 |
|
72 |
sub row2doc { |
73 |
my ( $row, $doc ) = @_; |
74 |
my $a = delete( $row->{xml} ); |
75 |
$doc->addAttachment( 'item.xml', 'application/xhtml+xml', $a ) if $a; |
76 |
my $a = delete( $row->{content} ); |
77 |
$doc->addAttachment( 'content.html', 'text/html', $a ) if $a; |
78 |
$doc->{data} = $row; |
79 |
return $doc; |
80 |
} |
81 |
|
82 |
row2doc( $row, $doc ); |
83 |
|
84 |
eval { $doc->create }; |
85 |
if ( $@ ) { |
86 |
$doc->retrieve; |
87 |
row2doc( $row, $doc )->update; |
88 |
# eval { $doc->update }; |
89 |
warn $@ ? "ERROR $_id $@\n" : "updated $_id\n"; |
90 |
} else { |
91 |
warn "created $_id\n"; |
92 |
} |
93 |
|
94 |
$last_row = $row->{id}; |
95 |
$count++; |
96 |
|
97 |
commit_last_row if $count % 100 == 0 # checkpoint every 100 records |
98 |
} |
99 |
|
100 |
commit_last_row; |
101 |
|
102 |
__END__ |
103 |
|
104 |
$sql = qq{ |
105 |
update items_userdata |
106 |
set value_numeric = 1 |
107 |
where label = 'read' and item_id in ($ids) |
108 |
}; |
109 |
|
110 |
$dbh->do( $sql ); |
111 |
|