Recent comments and posts model methods. Tags cloud. Post modtime fix.
[blog.pm.git] / bin / upgrade-sqlite-0.05.pl
blob09b385fc4c976757068b66ba7372ff5e3c81029c
1 #!/usr/bin/perl
3 use strict;
5 use DBI;
7 my ( $default_lang, $db_from, $db_to ) = @ARGV;
9 my @languages = (qw/ ru en /);
11 die "usage: $0 <default-lang> <from> <to>"
12 unless $default_lang && -f $db_from && $db_to;
14 my $dbh_from = DBI->connect( "dbi:SQLite:$db_from" ) or die $DBI::errstr;
15 my $dbh_to = DBI->connect( "dbi:SQLite:$db_to" ) or die $DBI::errstr;
17 my $posts = $dbh_from->selectall_arrayref( 'SELECT * FROM post ORDER BY id ASC' );
18 foreach my $post ( @$posts ) {
19 my ( $id, $key, $addtime, $modtime, $title, $annot, $content ) = @$post;
20 my $sth = $dbh_to->prepare(
21 qq/
22 INSERT INTO post(id,key,addtime,modtime,orig_lang)
23 VALUES(?, ?, ?, ?, ?)
26 $sth->execute( $id, $key, $addtime, $modtime, $default_lang )
27 or warn $dbh_to->errstr;
29 $content =~ s/\Q$annot\E/$annot\n\[cut\]\n/s;
30 foreach my $lang ( @languages ) {
31 my $sth = $dbh_to->prepare(
32 qq/
33 INSERT INTO post_i18n(post_id,lang,istran,title,annot,content)
34 VALUES (?, ?, ?, ?, ?, ?)
37 $sth->execute( $id, $lang, 0, $title, $annot, $content );
41 my $tags = $dbh_from->selectall_arrayref( 'SELECT * FROM tag' );
42 foreach my $tag ( @$tags ) {
43 my $sth = $dbh_to->prepare(
44 qq/
45 INSERT INTO tag VALUES(?, ?)
48 $sth->execute( @$tag )
49 or warn $dbh_to->errstr;
52 my $tag_maps = $dbh_from->selectall_arrayref( 'SELECT * FROM post_tag_map' );
53 foreach my $tag_map ( @$tag_maps ) {
54 my $sth = $dbh_to->prepare(
55 qq/
56 INSERT INTO post_tag_map VALUES(?, ?)
59 $sth->execute( @$tag_map )
60 or warn $dbh_to->errstr;
63 my $comments = $dbh_from->selectall_arrayref( 'SELECT * FROM comment' );
64 foreach my $comment ( @$comments ) {
65 my $sth = $dbh_to->prepare(
66 qq/
67 INSERT INTO comment VALUES(?, ?, ?, ?, ?, ?, ?, ?)
70 $sth->execute( @$comment )
71 or warn $dbh_to->errstr;
74 my $pages = $dbh_from->selectall_arrayref( 'SELECT * FROM page ORDER BY id ASC' );
75 foreach my $page ( @$pages ) {
76 my ( $id, $key, $addtime, $modtime, $title, $content ) = @$page;
77 my $sth = $dbh_to->prepare(
78 qq/
79 INSERT INTO page(id,key,addtime,modtime,orig_lang)
80 VALUES(?, ?, ?, ?, ?)
83 $sth->execute( $id, $key, $addtime, $modtime, $default_lang )
84 or warn $dbh_to->errstr;
86 foreach my $lang ( @languages ) {
87 my $sth = $dbh_to->prepare(
88 qq/
89 INSERT INTO page_i18n(page_id,lang,istran,title,content)
90 VALUES (?, ?, ?, ?, ?)
93 $sth->execute( $id, $lang, 0, $title, $content );
97 $dbh_from->disconnect();
98 $dbh_to->disconnect();