Web: added task db:seed:production-archive to seed DB from acrhived .jsons
Handy for development.
--- a/web/Rakefile Sat May 09 17:29:16 2015 +0100
+++ b/web/Rakefile Sat May 09 17:32:23 2015 +0100
@@ -3,10 +3,4 @@
require File.expand_path('../config/application', __FILE__)
-CalipelWeb::Application.load_tasks
-
-
-desc "Fetches production databse"
-task :'db:seed:production' do
- `scp "swing.fit.cvut.cz:/home/hlopkmar/calipel/current/db/production.sqlite3" "db/development.sqlite3"`
-end
+CalipelWeb::Application.load_tasks
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/web/db/seeds-common.rb Sat May 09 17:32:23 2015 +0100
@@ -0,0 +1,17 @@
+# This file contains helper function to seed database from
+# a set of .json reports. Used by db:seed and db:seed:production-archive
+# rake tasks
+
+def db_seed(directory, timestamp=nil)
+ if not File.directory?(directory) then
+ raise Exception.new("#{directory} is not a valid directory")
+ end
+ Dir.glob(File.join(directory, '*.json')).each do | json_file |
+ json = JSON.parse( IO.read( json_file) )
+ if timestamp != nil
+ json['timestamp'] = timestamp.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
+ end
+ puts "Loading #{json_file}..."
+ BenchmarkBatch.from_json json
+ end
+end
\ No newline at end of file
--- a/web/db/seeds.rb Sat May 09 17:29:16 2015 +0100
+++ b/web/db/seeds.rb Sat May 09 17:32:23 2015 +0100
@@ -7,18 +7,9 @@
# Mayor.create(name: 'Emanuel', city: cities.first)
require 'json'
-
+require File.join(Rails.root, 'db', 'seeds-common')
-def seed(filename)
- root = File.join(Rails.root, 'db', 'seeds')
- data = JSON.parse( IO.read( File.join(root, filename) ) )
- for m in 1..12
- timestamp = Time.new(2014, m, 1, 10, 10, 10)
- data['timestamp'] = timestamp.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
- puts "Seeding #{filename} at #{data['timestamp']}"
- BenchmarkBatch.from_json data
- end
+for m in 1..12
+ timestamp = Time.new(2014, m, 1, 10, 10, 10)
+ db_seed(File.join(Rails.root, 'db', 'seeds'), timestamp)
end
-
-seed 'results1.json'
-seed 'results2.json'
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/tasks/db.rake Sat May 09 17:32:23 2015 +0100
@@ -0,0 +1,18 @@
+desc "Fetches production database"
+task :'db:seed:production' do
+ `scp "swing.fit.cvut.cz:/home/hlopkmar/calipel/current/db/production.sqlite3" "db/development.sqlite3"`
+end
+
+desc "Fetches production archive and seeds local database from it"
+task :'db:seed:production-archive' => [ :'db:drop', :'db:create', :'db:migrate' ] do
+ tmp_production_archive = File.expand_path("../tmp/production-archive", __FILE__)
+ if not File.exist?(tmp_production_archive) then
+ mkdir_p tmp_production_archive
+ end
+ if not system("rsync swing.fit.cvut.cz:/home/hlopkmar/calipel/current/public/uploads/archive/*.json #{tmp_production_archive}" ) then
+ raise Exception.new("Cannot rsync archive")
+ end
+ if not system("rails r \"require File.join(Rails.root, 'db' , 'seeds-common'); db_seed(File.join(Rails.root, 'tmp', 'production-archive'))\"" ) then
+ raise Exception.new("Canno seed database")
+ end
+end
\ No newline at end of file