/[couchdb]/scripts/reblog2couchdb.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /scripts/reblog2couchdb.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Tue Aug 5 16:36:57 2008 UTC (15 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 2350 byte(s)
fix commit of last_row_id to _status document in couchdb
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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26