9 |
|
|
10 |
my $database = 'reblog'; |
my $database = 'reblog'; |
11 |
|
|
12 |
my $connect = "DBI:mysql:database=$database"; |
my $dbi = "DBI:mysql:database=$database"; |
13 |
$connect .= ";host=127.0.0.1;port=13306"; # XXX over ssh |
$dbi .= ";host=127.0.0.1;port=13306"; # XXX over ssh |
14 |
|
|
|
my $dbh = DBI->connect($connect,"","") || die $DBI::errstr; |
|
|
|
|
|
# select all posts which have been read or unread |
|
15 |
my $sql = qq{ |
my $sql = qq{ |
16 |
select |
select |
17 |
items.id as _id, |
items.id as _id, |
22 |
feeds.description as feed_description |
feeds.description as feed_description |
23 |
from items |
from items |
24 |
join items_userdata on items.id = item_id |
join items_userdata on items.id = item_id |
|
-- where label = 'read' and value_numeric = ? |
|
25 |
join feeds on items.feed_id = feeds.id |
join feeds on items.feed_id = feeds.id |
26 |
|
where items.id > ? |
27 |
order by items.id asc |
order by items.id asc |
28 |
limit 5000 |
limit 1000 |
29 |
}; |
}; |
30 |
|
|
|
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
|
|
$sth->execute( 1 ) || die $sth->errstr(); |
|
|
|
|
|
warn dump( $sth->{NAME} ); |
|
|
|
|
|
print "found ",$sth->rows," items to process..."; |
|
31 |
|
|
32 |
my $c = CouchDB::Client->new(uri => 'http://localhost:5984/'); |
my $c = CouchDB::Client->new(uri => 'http://localhost:5984/'); |
33 |
|
|
34 |
$c->testConnection or die "The server cannot be reached"; |
$c->testConnection or die "The server cannot be reached"; |
35 |
print "Running version " . $c->serverInfo->{version} . "\n"; |
print "CouchDB version " . $c->serverInfo->{version} . "\n"; |
36 |
my $db = $c->newDB( $database ); |
my $db = $c->newDB( $database ); |
37 |
$db->create unless $c->dbExists( $database ); |
$db->create unless $c->dbExists( $database ); |
38 |
|
|
39 |
my @docs = $db->listDocs; |
my $status = $db->newDoc( '_sync' ); |
40 |
my $row_id = shift @docs || 0; |
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 |
|
my $dbh = DBI->connect($dbi,"","") || die $DBI::errstr; |
48 |
|
|
49 |
|
print "Fetching items from $dbi id > $last_row\n"; |
50 |
|
|
51 |
|
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
52 |
|
$sth->execute( $last_row ) || die $sth->errstr(); |
53 |
|
|
54 |
|
warn dump( $sth->{NAME} ); |
55 |
|
|
56 |
|
print "found ",$sth->rows," items to process..."; |
57 |
|
|
58 |
my $pk = 'id'; |
my $pk = 'id'; |
59 |
|
|
60 |
|
my $count = 0; |
61 |
|
|
62 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
63 |
my $_id = $row->{_id} || die "row needs _id"; |
my $_id = $row->{_id} || die "row needs _id"; |
64 |
my $doc = $db->newDoc( $_id ); |
my $doc = $db->newDoc( $_id ); |
84 |
} else { |
} else { |
85 |
warn "created ",dump( $row ),$/; |
warn "created ",dump( $row ),$/; |
86 |
} |
} |
87 |
|
|
88 |
|
$count++; |
89 |
|
if ( $count++ % 100 == 0 ) { # checkpoint every 100 records |
90 |
|
$status->{data}->{last_row_id} = $row->{id}; |
91 |
|
$status->update; |
92 |
|
} |
93 |
} |
} |
94 |
|
|
95 |
|
# end checkpoint |
96 |
|
$status->{data}->{last_row_id} = $last_row; |
97 |
|
$status->update; |
98 |
|
|
99 |
__END__ |
__END__ |
100 |
|
|
101 |
$sql = qq{ |
$sql = qq{ |