|
1 | 1 | require 'json'
|
2 | 2 | require 'pry'
|
3 | 3 |
|
4 |
| -# in repo root, run |
| 4 | +# in repo root, run: |
5 | 5 | # rails runner db/import_points_from_old_db.rb
|
6 | 6 |
|
7 | 7 | filepath_points = "old_db/points/"
|
8 | 8 |
|
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:' |
14 | 11 | 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 |
15 | 20 | 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, |
17 | 22 | 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 |
23 | 45 | # binding.pry
|
24 | 46 | # service_id: service.id,
|
25 | 47 | # topic = Topic.find_by_title(line['topics']) #need to import the services first and match it by string
|
26 | 48 | # topic_id: topic.id, #need to import topics first and match it by string
|
27 |
| - ) |
28 | 49 | 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 |
29 | 68 | end
|
30 | 69 | puts "Finishing importing points"
|
31 | 70 | puts "Done!"
|
0 commit comments