generate SQL from table and pk
[mongodb-experiments.git] / dbi2mongo.pl
1 #!/usr/bin/perl -w
2
3 sub BEGIN {
4 $ENV{DBI_AUTOPROXY}='dbi:Gofer:transport=stream;url=ssh:dpavlin@koha.ffzg.hr';
5 }
6
7 use strict;
8 use DBI;
9 use MongoDB;
10 use Data::Dump qw/dump/;
11
12 $|++;
13
14 my $debug = @ARGV ? 1 : 0;
15
16 our ( $dbi, $user, $password ) = ( "DBI:mysql:database=test" );
17 our ( $database, $collection ) = ( 'test', 'test' );
18 our ( $table, $pk ) = ( 'items', 'id' );
19
20 require 'config.pl';
21
22 warn "# $dbi $user -> $database $collection\n";
23
24 my $conn = MongoDB::Connection->new;
25 my $db   = $conn->get_database( $database );
26 my $coll = $db->get_collection( $collection );
27 my $dbh  = DBI->connect($dbi,$user,$password, {
28         RaiseError => 1,
29 #       mysql_enable_utf8 => 1,
30 });
31
32 $db->drop if $debug;
33
34 # db.items.find().sort({_id:-1}).limit(1);
35 my $last = $coll->query()->sort({ '_id' => -1 })->limit(1)->next;
36 warn dump( $last );
37 my $last_id = $last->{_id} || 0;
38
39 print "Fetching items from $dbi _id > $last_id\n";
40
41 my $sth = $dbh->prepare(qq{
42         select
43                 $pk as _id,
44                 $table.*
45         from $table
46         where $pk > ?
47         order by $pk asc
48         limit 100000
49 });
50
51 $sth->execute( $last_id );
52
53 warn dump( $sth->{NAME} );
54
55 print "found ",$sth->rows," items to process...\n";
56
57 while (my $row = $sth->fetchrow_hashref() ) {
58
59         map { $row->{$_} * 1 } grep { defined $row->{$_} && $row->{$_} =~ /^\d+$/ } keys %$row;
60         $coll->insert( $row );
61 }
62