Skip to content

Commit 53c1877

Browse files
committed
#11 optimize add migrate captain repos migrate
1 parent 2ea2b11 commit 53c1877

File tree

7 files changed

+142
-28
lines changed

7 files changed

+142
-28
lines changed

app/models/captain/practice.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class Captain::Practice < ApplicationRecord
22
include SubjectOrPracticeCommon
3+
34
belongs_to :subject, class_name: 'Captain::Subject'
5+
has_many :repos, class_name: 'Captain::Repo'
6+
47
end

app/models/captain/repo.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Captain::Repo < ApplicationRecord
2+
class Status
3+
WAIT_TO_APPROVE = 'wait_to_approve'
4+
APPROVED = 'approved'
5+
REJECTED = 'rejected'
6+
end
7+
8+
belongs_to :practice, class_name: 'Captain::Practice'
9+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateCaptainRepos < ActiveRecord::Migration[5.0]
2+
def change
3+
create_table :captain_repos do |t|
4+
t.string :name
5+
t.string :url
6+
t.string :status
7+
t.integer :practice_id
8+
9+
t.timestamps
10+
end
11+
end
12+
end

db/schema.rb

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20160508031830) do
14+
ActiveRecord::Schema.define(version: 20160508034341) do
1515

1616
create_table "adpositions", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
1717
t.string "name"
@@ -41,6 +41,15 @@
4141
t.datetime "updated_at", null: false
4242
end
4343

44+
create_table "captain_repos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
45+
t.string "name"
46+
t.string "url"
47+
t.string "status"
48+
t.integer "practice_id"
49+
t.datetime "created_at", null: false
50+
t.datetime "updated_at", null: false
51+
end
52+
4453
create_table "captain_subjects", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
4554
t.string "name"
4655
t.string "desc"
@@ -75,6 +84,25 @@
7584
t.datetime "updated_at", null: false
7685
end
7786

87+
create_table "docs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
88+
t.string "name"
89+
t.text "con", limit: 4294967295
90+
t.text "markdown", limit: 4294967295
91+
t.string "cover", limit: 55
92+
t.integer "mem_id"
93+
t.datetime "created_at", null: false
94+
t.datetime "updated_at", null: false
95+
end
96+
97+
create_table "docsubs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
98+
t.string "name"
99+
t.integer "mem_id"
100+
t.text "con", limit: 65535
101+
t.string "status", limit: 10, default: "UNREAD"
102+
t.datetime "created_at", null: false
103+
t.datetime "updated_at", null: false
104+
end
105+
78106
create_table "links", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
79107
t.string "name"
80108
t.string "logo"
@@ -131,12 +159,14 @@
131159
create_table "mems", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
132160
t.string "nc"
133161
t.string "email"
134-
t.string "avatar"
135-
t.datetime "created_at", null: false
136-
t.datetime "updated_at", null: false
162+
t.string "avatar", limit: 100
163+
t.datetime "created_at", null: false
164+
t.datetime "updated_at", null: false
137165
t.string "pwd"
138166
end
139167

168+
add_index "mems", ["nc", "email"], name: "search", using: :btree
169+
140170
create_table "menutyps", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
141171
t.string "key"
142172
t.string "sdesc"
@@ -163,22 +193,23 @@
163193
create_table "readmes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
164194
t.integer "mem_id"
165195
t.integer "repo_id"
166-
t.text "about", limit: 65535
167-
t.text "old", limit: 65535
168-
t.string "sdesc"
169-
t.string "status", default: "UNREAD"
170-
t.datetime "created_at", null: false
171-
t.datetime "updated_at", null: false
196+
t.text "about", limit: 4294967295
197+
t.text "old", limit: 4294967295
198+
t.string "sdesc", limit: 200, default: "初始化文档"
199+
t.string "status", limit: 45, default: "UNREAD"
200+
t.datetime "created_at", null: false
201+
t.datetime "updated_at", null: false
172202
end
173203

174204
create_table "repo_resources", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
175-
t.string "recsts", default: "1"
205+
t.string "recsts", limit: 2, default: "1"
176206
t.string "title"
177207
t.string "url"
178-
t.integer "repo_id"
208+
t.string "repo_alia"
179209
t.integer "mem_id"
180-
t.datetime "created_at", null: false
181-
t.datetime "updated_at", null: false
210+
t.datetime "created_at", null: false
211+
t.datetime "updated_at", null: false
212+
t.integer "repo_id"
182213
end
183214

184215
create_table "repo_trends", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
@@ -195,33 +226,36 @@
195226
t.string "full_name"
196227
t.string "alia"
197228
t.string "html_url"
198-
t.string "description"
229+
t.string "description", limit: 1000
230+
t.string "description_cn", limit: 1000
199231
t.string "homepage"
200232
t.integer "stargazers_count"
201233
t.integer "forks_count"
202234
t.integer "subscribers_count"
203235
t.datetime "pushed_at"
204-
t.text "about", limit: 65535
205-
t.text "about_zh", limit: 65535
236+
t.text "about", limit: 4294967295
237+
t.text "about_zh", limit: 4294967295
206238
t.string "typcd"
207239
t.string "rootyp"
208-
t.string "owner"
209-
t.datetime "created_at", null: false
210-
t.datetime "updated_at", null: false
211-
t.string "outdated", limit: 1, default: "0"
240+
t.string "owner", limit: 100
241+
t.datetime "created_at", null: false
242+
t.datetime "updated_at", null: false
243+
t.string "outdated", limit: 1, default: "0"
212244
t.string "tag"
213245
t.string "cover"
214-
t.integer "recommend", default: 0
215-
t.integer "trend", default: 0
246+
t.integer "recommend", default: 0
247+
t.integer "trend", default: 0
216248
t.datetime "github_created_at"
217-
t.integer "mark", default: 0
218-
t.integer "issue_res", default: 0
249+
t.integer "mark", default: 0
250+
t.integer "issue_res", default: 0
219251
t.string "typcd_zh"
220252
t.string "rootyp_zh"
221253
t.integer "demo"
222254
t.integer "startup"
223255
end
224256

257+
add_index "repos", ["rootyp", "typcd", "html_url"], name: "search", using: :btree
258+
225259
create_table "sites", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
226260
t.string "typ"
227261
t.string "sdesc"
@@ -247,9 +281,9 @@
247281
t.string "html_url"
248282
t.string "rootyp"
249283
t.string "typcd"
250-
t.string "status", default: "UNREAD"
251-
t.datetime "created_at", null: false
252-
t.datetime "updated_at", null: false
284+
t.string "status", limit: 45, default: "UNREAD"
285+
t.datetime "created_at", null: false
286+
t.datetime "updated_at", null: false
253287
end
254288

255289
create_table "topics", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace :db do
2+
namespace :repos do
3+
4+
desc 'migrate old repos to captain repos'
5+
task :migrate do
6+
puts 'migrate old repos to captain repos --> '
7+
subject_id_code_maps = Captain::Subject.all.each_with_object({}) do |subject, container|
8+
container[subject.id] = subject.code
9+
end
10+
practice_code_ip_maps = Captain::Practice.all.each_with_object({}) do |practice, container|
11+
subject_code = subject_id_code_maps[practice.subject_id]
12+
next if subject_code.blank?
13+
code_key = "#{practice.code}_#{subject_code}"
14+
container[code_key] = practice.id
15+
end
16+
17+
unless practice_code_ip_maps.empty?
18+
old_repos_count = Repo.count
19+
Repo.all.each_with_index do |repo, index|
20+
puts "[#{index+1}/#{old_repos_count}] #{repo.name}"
21+
attributes= repo.attributes.symbolize_keys
22+
slice_attrs = attributes.slice(:name, :html_url, :description, :typcd, :rootyp)
23+
code_key = "#{slice_attrs[:typcd].downcase}_#{slice_attrs[:rootyp].downcase}"
24+
practice_id = practice_code_ip_maps[code_key]
25+
26+
next if practice_id.blank?
27+
28+
assign_repo_attrs = {name: slice_attrs[:name], status: Captain::Repo::Status::WAIT_TO_APPROVE, url: slice_attrs[:html_url], practice_id: practice_id}
29+
Captain::Repo.find_or_create_by assign_repo_attrs do |captain_repo|
30+
captain_repo.update_attributes assign_repo_attrs
31+
end
32+
33+
end
34+
end
35+
end
36+
37+
end
38+
end

test/fixtures/captain/repos.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
name: MyString
5+
url: MyString
6+
status: MyString
7+
8+
two:
9+
name: MyString
10+
url: MyString
11+
status: MyString

test/models/captain/repo_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test_helper'
2+
3+
class Captain::RepoTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)