# HG changeset patch # User Vincent Tondellier # Date 1407156697 -7200 # Node ID f77c6719c05c5fe52059adbf1f06f81d7f5ec3e0 # Parent f65708dc1be16e01245987f14d00b6bb9840211e Add migration script diff -r f65708dc1be1 -r f77c6719c05c bin/fs_to_db.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/fs_to_db.pl Mon Aug 04 14:51:37 2014 +0200 @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use Mojo::Base -strict; + +use lib 'lib'; +use CrashTest::Storage::Sql; +use Mojo::JSON; +use Mojo::Util qw(decode slurp); +use File::Basename; + +sub load_config { + my ($file) = @_; + my $code = decode('UTF-8', slurp $file); + return eval($code); +} + +my $config = load_config("CrashTest.conf"); + +eval "require $config->{Storage}->{Type}"; +my $storage = $config->{Storage}->{Type}->new($config->{Storage}); + + +foreach my $arg (@ARGV) { + my $json = Mojo::JSON->new(); + my $pjson = $json->decode(slurp $arg); + + my($filename, $dirs, $suffix) = fileparse($arg, qr/\.[^.]*/); + + my $uuid = $filename; + $storage->_db_insert_processed_data($uuid, $pjson); +} diff -r f65708dc1be1 -r f77c6719c05c lib/CrashTest/Storage/Sql.pm --- a/lib/CrashTest/Storage/Sql.pm Mon Aug 04 14:51:09 2014 +0200 +++ b/lib/CrashTest/Storage/Sql.pm Mon Aug 04 14:51:37 2014 +0200 @@ -73,15 +73,10 @@ return $processed_data; } -sub store_processed_data { +sub _db_insert_processed_data { my ($self, $uuid, $pjson) = @_; my $json = Mojo::JSON->new; - my $j = $json->encode($pjson); - open JSON, '>', File::Spec->catfile($self->{data_path}, "$uuid.json") or die $!; - print JSON $j; - close JSON; - $self->{schema}->txn_do(sub { my $crash = $self->{schema}->resultset('CrashReport')->new({ uuid => $uuid }); @@ -99,7 +94,7 @@ version => $pjson->{client_info}->{Version}, }; - my $dbproduct = $self->{schema}->resultset('Product')->search($product)->next(); + my $dbproduct = $self->{schema}->resultset('Product')->search($product)->first(); if($dbproduct) { $crash->product($dbproduct); } else { @@ -111,7 +106,7 @@ user_id => $pjson->{client_info}->{UserID}, }; - my $dbuser = $self->{schema}->resultset('CrashUser')->search($user)->next(); + my $dbuser = $self->{schema}->resultset('CrashUser')->search($user)->first(); if($dbuser) { $crash->crash_user($dbuser); } else { @@ -128,6 +123,18 @@ }); } +sub store_processed_data { + my ($self, $uuid, $pjson) = @_; + + my $json = Mojo::JSON->new; + my $j = $json->encode($pjson); + open JSON, '>', File::Spec->catfile($self->{data_path}, "$uuid.json") or die $!; + print JSON $j; + close JSON; + + $self->_db_insert_processed_data($uuid, $pjson); +} + sub store_dump { my ($self, $uuid, $file) = @_;