Skip to content

Commit 8be8df9

Browse files
Merge pull request #189 from tosdr/mj-old-db-import
WiP: db import
2 parents fe31022 + 047a2d1 commit 8be8df9

14 files changed

+218
-58
lines changed

app/models/case.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Case < ApplicationRecord
2+
belongs_to :topic
3+
end

app/models/comment.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Comment < ApplicationRecord
2+
belongs_to :point
3+
end

db/import_cases_from_old_db.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# in repo root, run:
2+
# rails runner db/import_cases_from_old_db.rb
3+
4+
filename_cases = "old_db/cases.json"
5+
6+
def importCase(data)
7+
puts 'old data:'
8+
puts data
9+
puts 'new data:'
10+
topic = Topic.find_by_oldId(data['topic'])
11+
imported_case = Case.new(
12+
title: data['name'],
13+
classification: data['point'],
14+
score: data['score'],
15+
topic: topic
16+
)
17+
puts imported_case
18+
unless imported_case.valid?
19+
puts "### #{imported_case.title} not imported ! ###" #+ panic
20+
puts topic
21+
end
22+
imported_case.save
23+
puts 'saved.'
24+
end
25+
26+
puts "Importing cases..."
27+
file = File.read(filename_cases)
28+
data = JSON.parse(file)
29+
data.each { |line| importCase(line) }
30+
puts "Finishing importing cases"
31+
puts "Done!"

db/import_points_from_old_db.rb

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,70 @@
11
require 'json'
22
require 'pry'
33

4-
# in repo root, run
4+
# in repo root, run:
55
# rails runner db/import_points_from_old_db.rb
66

77
filepath_points = "old_db/points/"
88

9-
puts "Importing points..."
10-
Dir.foreach(filepath_points) do |filename|
11-
next if filename == '.' or filename == '..'
12-
file = File.read(filepath_points + filename)
13-
data = JSON.parse(file)
9+
def importPoint(data, service)
10+
puts 'old data:'
1411
puts data
12+
puts service
13+
puts 'new data:'
14+
userObj = User.find_by_email('[email protected]')
15+
topicObj = Topic.find_by_title('Personal Data')
16+
serviceObjDefault = Service.find_by_name('amazon')
17+
serviceObj = Service.find_by_name(service) || serviceObjDefault
18+
caseObjDefault = Case.find_by_title('info given about security practices')
19+
caseObj = Case.find_by_title(data['tosdr']['case']) || caseObjDefault
1520
imported_point = Point.new(
16-
# user_id: # go make the script look for the user, if no user, make it default
21+
# old_id: data['id'] + '-' + service,
1722
title: data['title'],
18-
source: data['discussion'],
19-
status: (data['needModeration'] == "false" ? "approved" : "pending"),
20-
analysis: data['tldr'],
21-
rating: data['tosdr']['score']
22-
# service = Service.find_by_name(line['services'])
23+
user: userObj,
24+
source: "http://perdu.com",
25+
status: "pending",
26+
analysis: "Bla bla bla",
27+
rating: 3,
28+
topic: topicObj,
29+
service: serviceObj,
30+
case_id: caseObj.id
31+
)
32+
33+
# validates :title, presence: true
34+
# validates :title, length: { in: 5..140 }
35+
# validates :source, presence: true
36+
# validates :status, inclusion: { in: ["approved", "pending", "declined", "disputed", "draft"], allow_nil: false }
37+
# validates :analysis, presence: true
38+
# validates :rating, presence: true
39+
# validates :rating, numericality: true
40+
41+
unless imported_point.valid?
42+
puts "### #{imported_point.title} not imported ! ###"
43+
# panic
44+
end
2345
# binding.pry
2446
# service_id: service.id,
2547
# topic = Topic.find_by_title(line['topics']) #need to import the services first and match it by string
2648
# topic_id: topic.id, #need to import topics first and match it by string
27-
)
2849
imported_point.save
50+
puts imported_point.id
51+
puts data['id']
52+
creationComment = Comment.new(
53+
point: imported_point,
54+
summary: 'imported from '+data['id']
55+
)
56+
creationComment.save
57+
puts 'saved.'
58+
end
59+
60+
puts "Importing points..."
61+
Dir.foreach(filepath_points) do |filename|
62+
next if filename == '.' or filename == '..' or filename == 'README.md'
63+
file = File.read(filepath_points + filename)
64+
data = JSON.parse(file)
65+
for i in 0 ... data['services'].size
66+
importPoint(data, data['services'][i])
67+
end
2968
end
3069
puts "Finishing importing points"
3170
puts "Done!"

db/import_services_from_old_db.rb

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
require 'json'
2-
require 'pry'
1+
# in repo root, run:
2+
# rails runner db/import_services_from_old_db.rb
33

4-
puts "Starting the loop and the import..."
5-
Dir[File.join(Rails.root,"old_db/services/*.json")].each do |json_file|
6-
hash = JSON.parse(File.read(json_file))
4+
filepath_services = "old_db/services/"
5+
6+
def importService(data)
7+
puts 'old data:'
8+
puts data
9+
puts 'new data:'
710
imported_service = Service.new(
8-
name: hash['name'],
9-
url: hash['fulltos']['terms']['url']
10-
)
11-
imported_service.save
12-
byebug
11+
# old_id: data['id'],
12+
name: data['name'].downcase,
13+
url: data['urls'][0] || 'url',
14+
grade: 'grade'
15+
)
16+
puts imported_service
1317
unless imported_service.valid?
14-
puts "### #{imported_service.name} not imported ! ###"
15-
else
16-
puts "Imported and saved: #{imported_service.name}"
18+
puts "### #{imported_service.name} not imported ! ###" #+ panic
1719
end
18-
puts "Exiting loop"
20+
imported_service.save
21+
puts 'saved.'
22+
end
23+
24+
puts "Importing services..."
25+
Dir.foreach(filepath_services) do |filename|
26+
next if filename == '.' or filename == '..' or filename == 'README.md'
27+
file = File.read(filepath_services + filename)
28+
data = JSON.parse(file)
29+
importService(data)
1930
end
20-
puts "Finishing importing topics!"
31+
puts "Finishing importing services"
32+
puts "Done!"

db/import_topics_from_old_db.rb

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1-
require 'json'
1+
# in repo root, run:
2+
# rails runner db/import_topics_from_old_db.rb
23

3-
puts "Starting the loop and the import..."
4-
Dir[File.join(Rails.root,"old_db/topics/*.json")].each do |json_file|
5-
hash = JSON.parse(File.read(json_file))
4+
filepath_topics = "old_db/topics/"
5+
6+
def importTopic(data)
7+
puts 'old data:'
8+
puts data
9+
puts 'new data:'
610
imported_topic = Topic.new(
7-
title: hash['name'],
8-
subtitle: hash['subtitle'] || 'no text here',
9-
description: hash['description'] || 'no text here'
10-
)
11-
imported_topic.save #add topic error handling
11+
title: data['title'] || 'title',
12+
subtitle: data['subtitle'] || 'subtitle',
13+
description: data['description'] || 'description',
14+
oldId: data['id']
15+
)
16+
puts imported_topic
1217
unless imported_topic.valid?
1318
puts "### #{imported_topic.title} not imported ! ###"
14-
else
15-
puts "Imported and saved: #{imported_topic.title}"
19+
panic
1620
end
17-
puts "Exiting loop"
21+
imported_topic.save
22+
puts 'saved.'
23+
end
24+
25+
puts "Importing topics..."
26+
Dir.foreach(filepath_topics) do |filename|
27+
next if filename == '.' or filename == '..' or filename == 'README.md'
28+
file = File.read(filepath_topics + filename)
29+
data = JSON.parse(file)
30+
importTopic(data)
1831
end
19-
puts "Finishing importing topics!"
32+
puts "Finishing importing topics"
33+
puts "Done!"
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class CreateCases < ActiveRecord::Migration[5.1]
2+
def change
3+
create_table :cases do |t|
4+
t.string :classification
5+
t.integer :score
6+
t.string :title
7+
t.text :description
8+
t.references :topic, foreign_key: true
9+
10+
t.timestamps
11+
end
12+
end
13+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateComments < ActiveRecord::Migration[5.1]
2+
def change
3+
create_table :comments do |t|
4+
t.references :point, foreign_key: true
5+
t.string :summary
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddQuoteToPoints < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :points, :quote, :string
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddRefsToPoints < ActiveRecord::Migration[5.1]
2+
def change
3+
add_reference :points, :case, foreign_key: true
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddOldIdToTopics < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :topics, :oldId, :string
4+
end
5+
end

db/schema.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20171120110106) do
13+
ActiveRecord::Schema.define(version: 20171215063327) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -29,6 +29,25 @@
2929
t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
3030
end
3131

32+
create_table "cases", force: :cascade do |t|
33+
t.string "classification"
34+
t.integer "score"
35+
t.string "title"
36+
t.text "description"
37+
t.bigint "topic_id"
38+
t.datetime "created_at", null: false
39+
t.datetime "updated_at", null: false
40+
t.index ["topic_id"], name: "index_cases_on_topic_id"
41+
end
42+
43+
create_table "comments", force: :cascade do |t|
44+
t.bigint "point_id"
45+
t.string "summary"
46+
t.datetime "created_at", null: false
47+
t.datetime "updated_at", null: false
48+
t.index ["point_id"], name: "index_comments_on_point_id"
49+
end
50+
3251
create_table "points", force: :cascade do |t|
3352
t.bigint "user_id"
3453
t.integer "rank", default: 0
@@ -42,6 +61,9 @@
4261
t.datetime "updated_at", null: false
4362
t.bigint "topic_id"
4463
t.bigint "service_id"
64+
t.string "quote"
65+
t.bigint "case_id"
66+
t.index ["case_id"], name: "index_points_on_case_id"
4567
t.index ["service_id"], name: "index_points_on_service_id"
4668
t.index ["topic_id"], name: "index_points_on_topic_id"
4769
t.index ["user_id"], name: "index_points_on_user_id"
@@ -72,6 +94,7 @@
7294
t.string "description"
7395
t.datetime "created_at", null: false
7496
t.datetime "updated_at", null: false
97+
t.string "oldId"
7598
end
7699

77100
create_table "users", force: :cascade do |t|
@@ -94,6 +117,9 @@
94117
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
95118
end
96119

120+
add_foreign_key "cases", "topics"
121+
add_foreign_key "comments", "points"
122+
add_foreign_key "points", "cases"
97123
add_foreign_key "points", "services"
98124
add_foreign_key "points", "topics"
99125
add_foreign_key "points", "users"

db/seeds.rb

+9-16
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,20 @@
1313
Service.destroy_all
1414
Topic.destroy_all
1515

16-
# puts "Importing topics"
17-
# load File.join(Rails.root,"db","import_topics_from_old_db.rb")
18-
# puts "Importing services"
19-
# load File.join(Rails.root,"db","import_services_from_old_db.rb")
20-
# load File.join(Rails.root,"db","import_points_from_old_db.rb")
21-
2216
puts "Starts new seeding"
2317
test_user = User.new(email: "[email protected]", username: "test user", password: "testnonadminuser", password_confirmation: "testnonadminuser")
2418
test_user.save
2519
curator_test_user = User.new(email: "[email protected]", password: "testcuratoruser", password_confirmation: "testcuratoruser", curator: true)
2620
curator_test_user.save
2721
admin_test_user = User.new(email: "[email protected]", username: "admin test user", password: "testadminuser", password_confirmation: "testadminuser", admin: true)
2822
admin_test_user.save
29-
test_service_1 = Service.new(name: "Test service 1", url: "http://perdu.com", grade: "A")
30-
test_service_1.save
31-
test_service_2 = Service.new(name: "Test service 2", url: "http://perdu.com", grade: "B")
32-
test_service_2.save
33-
test_topic = Topic.new(title: "Test topic", subtitle: "Test subtitle", description: "Test topic description")
34-
test_topic.save
35-
test_point_1 = Point.new(user_id: 1, title: "Test point 1", source: "http://perdu.com", status: "pending", analysis: "Bla bla bla", rating: 3, topic_id: 1, service_id: 1)
36-
test_point_1.save
37-
test_point_2 = Point.new(user_id: 1, title: "Test point 2", source: "http://perdu.com", status: "pending", analysis: "Bla bla bla", rating: 5, topic_id: 1, service_id: 1)
38-
test_point_2.save
23+
24+
puts "Importing topics"
25+
load File.join(Rails.root,"db","import_topics_from_old_db.rb")
26+
puts "Importing services"
27+
load File.join(Rails.root,"db","import_services_from_old_db.rb")
28+
puts "Importing cases"
29+
load File.join(Rails.root,"db","import_cases_from_old_db.rb")
30+
puts "Importing points"
31+
load File.join(Rails.root,"db","import_points_from_old_db.rb")
3932
puts "Done!"

0 commit comments

Comments
 (0)