/[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

Annotation of /scripts/reblog2couchdb.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (hide annotations)
Tue Aug 5 16:36:57 2008 UTC (15 years, 9 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 dpavlin 2 #!/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 dpavlin 6 my $dbi = "DBI:mysql:database=$database";
13     $dbi .= ";host=127.0.0.1;port=13306"; # XXX over ssh
14 dpavlin 2
15     my $sql = qq{
16     select
17 dpavlin 4 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 dpavlin 2 from items
24 dpavlin 4 join items_userdata on items.id = item_id
25     join feeds on items.feed_id = feeds.id
26 dpavlin 6 where items.id > ?
27 dpavlin 4 order by items.id asc
28 dpavlin 7 limit 5000
29 dpavlin 2 };
30    
31    
32     my $c = CouchDB::Client->new(uri => 'http://localhost:5984/');
33    
34     $c->testConnection or die "The server cannot be reached";
35 dpavlin 6 print "CouchDB version " . $c->serverInfo->{version} . "\n";
36 dpavlin 2 my $db = $c->newDB( $database );
37     $db->create unless $c->dbExists( $database );
38    
39 dpavlin 6 my $status = $db->newDoc( '_sync' );
40     eval { $status->retrieve };
41     $status->create if $@;
42 dpavlin 2
43 dpavlin 6 print "status ",dump( $status->{data} ), "\n";
44    
45     my $last_row = $status->{data}->{last_row_id} || 0;
46    
47 dpavlin 7 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 dpavlin 6 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 dpavlin 2 my $pk = 'id';
65    
66 dpavlin 6 my $count = 0;
67    
68 dpavlin 2 while (my $row = $sth->fetchrow_hashref() ) {
69 dpavlin 4 my $_id = $row->{_id} || die "row needs _id";
70 dpavlin 2 my $doc = $db->newDoc( $_id );
71 dpavlin 4
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 dpavlin 2 eval { $doc->create };
85     if ( $@ ) {
86 dpavlin 4 $doc->retrieve;
87     row2doc( $row, $doc )->update;
88     # eval { $doc->update };
89 dpavlin 7 warn $@ ? "ERROR $_id $@\n" : "updated $_id\n";
90 dpavlin 2 } else {
91 dpavlin 7 warn "created $_id\n";
92 dpavlin 2 }
93 dpavlin 6
94 dpavlin 7 $last_row = $row->{id};
95 dpavlin 6 $count++;
96 dpavlin 7
97     commit_last_row if $count % 100 == 0 # checkpoint every 100 records
98 dpavlin 2 }
99    
100 dpavlin 7 commit_last_row;
101 dpavlin 6
102 dpavlin 2 __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