-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.rb
89 lines (77 loc) · 3.04 KB
/
migrate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'sequel'
require 'ruby-progressbar'
OLD_DB = Sequel.connect(ENV.fetch('OLD_DB_URL'), encoding: 'latin1')
NEW_DB = Sequel.connect(ENV.fetch('NEW_DB_URL'))
def migrate_user(user)
NEW_DB[:users] << {
id: user[:id],
login: user[:login],
email: user[:email],
created_on: user[:created_on],
last_login: user[:last_login],
}
end
def migrate_page(page)
page = NEW_DB[:pages] << {
title: page[:title].force_encoding('UTF-8'),
slug: page[:shorthand_title].force_encoding('UTF-8').downcase,
title_char: page[:title_char].force_encoding('UTF-8'),
content: page[:content].force_encoding('UTF-8'),
compiled_content: page[:compiled_content].force_encoding('UTF-8'),
comment: page[:comment].force_encoding('UTF-8'),
compiled_comment: page[:compiled_comment].force_encoding('UTF-8'),
description: page[:description].force_encoding('UTF-8'),
compiled_description: page[:compiled_description].force_encoding('UTF-8'),
created_on: page[:created_on],
updated_on: page[:updated_on],
markup: page[:markup],
revision: page[:revision],
author_id: page[:updated_by],
}
end
def migrate_revision(revision)
old_page = OLD_DB[:pages].
where(id: revision[:page_id]).
first
new_page = NEW_DB[:pages].
where(slug: old_page[:shorthand_title].force_encoding('UTF-8').downcase).
first
unless new_page
puts "Skipped #{revision[:title]}"
return
end
NEW_DB[:revisions] << {
page_id: new_page[:id],
title: revision[:title].force_encoding('UTF-8'),
slug: revision[:shorthand_title].force_encoding('UTF-8').downcase,
title_char: revision[:title_char].force_encoding('UTF-8'),
content: revision[:content].force_encoding('UTF-8'),
compiled_content: revision[:compiled_content].force_encoding('UTF-8'),
comment: revision[:comment].force_encoding('UTF-8'),
compiled_comment: revision[:compiled_comment].force_encoding('UTF-8'),
description: revision[:description].force_encoding('UTF-8'),
compiled_description: revision[:compiled_description].force_encoding('UTF-8'),
created_on: revision[:created_on],
updated_on: revision[:updated_on],
markup: revision[:markup],
revision: revision[:revision],
author_id: revision[:updated_by],
}
end
NEW_DB.transaction do
count = OLD_DB[:users].count + OLD_DB[:pages].count + OLD_DB[:revisions].count
progress_bar = ProgressBar.create(total: count)
OLD_DB[:users].each do |user|
progress_bar.increment
migrate_user(user)
end
NEW_DB.execute("SELECT setval('users_id_seq', max(id)) FROM users")
OLD_DB[:pages].each do |page|
progress_bar.increment
migrate_page(page)
end
OLD_DB[:revisions].each do |revision|
progress_bar.increment
migrate_revision(revision)
end
end