rsstootalizer/update_db.pl

52 lines
1.1 KiB
Perl
Raw Normal View History

2017-04-23 18:49:07 +00:00
#!/usr/bin/perl -w -I.
use strict;
use Data::Dumper;
2017-04-23 18:51:48 +00:00
use RSSTootalizer::Migration;
2017-04-23 19:22:46 +00:00
use JSON;
sub Error {
my $headline = shift;
my $msg = shift;
print "$headline: $msg\n";
}
2017-04-23 18:49:07 +00:00
our $config = "";
2017-04-23 18:51:48 +00:00
open CONFIG, "rsstootalizer.conf.json" or die "Cannot open rsstootalizer.conf.json";
2017-04-23 18:49:07 +00:00
{
2017-04-23 19:22:46 +00:00
local $/ = undef;
2017-04-23 18:49:07 +00:00
$config = <CONFIG>;
}
close CONFIG;
$config = decode_json($config);
# Force Unicode output
binmode STDERR, ":utf8";
binmode STDOUT, ":utf8";
my @migrations = glob ("migrations/*sql");
foreach my $migration (@migrations){
2017-04-23 19:22:46 +00:00
my $sth = RSSTootalizer::DB->doSELECT("SELECT * FROM migrations WHERE name = ?", $migration);
if (!$sth){
print "Running migration $migration\n";
open (M, "<", $migration);
my $sql = "";
while (<M>){
chomp;
print "Read: $_\n";
$sql .= $_;
if ($sql =~ /;/){
print "Running: $sql\n";
RSSTootalizer::DB->doDELETE($sql); # Using doDELETE for lack of error handling...
$sql = "";
}
2017-04-23 18:49:07 +00:00
}
close M;
my %migdata;
$migdata{name} = $migration;
2017-04-23 18:51:48 +00:00
RSSTootalizer::Migration->create(%migdata);
2017-04-23 19:22:46 +00:00
} else {
print "Migration $migration already done\n";
2017-04-23 18:49:07 +00:00
}
}