Skip to content

Commit 4a278f0

Browse files
authored
Identifierモデルの移行タスクを追加 (#1833)
* add enju_leaf:migrate_identifiers task * add logger to enju_leaf:migrate_identifiers task * add validation
1 parent 61d6d19 commit 4a278f0

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

app/models/jpno_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class JpnoRecord < ApplicationRecord
22
belongs_to :manifestation
3-
validates :body, presence: true
3+
validates :body, presence: true, uniqueness: true
44
strip_attributes
55
end
66

app/models/ndla_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class NdlaRecord < ApplicationRecord
22
belongs_to :agent
3-
validates :body, presence: true
3+
validates :body, presence: true, uniqueness: true
44
end
55

66
# == Schema Information

lib/tasks/enju_leaf_tasks.rake

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,59 @@ namespace :enju_leaf do
8585
migrate_attachment
8686
end
8787
end
88+
89+
desc 'Migrate identifiers'
90+
task :migrate_identifiers => :environment do
91+
IdentifierType.find_each do |identifier_type|
92+
Identifier.where(identifier_type: identifier_type).find_each do |identifier|
93+
Manifestation.transaction do
94+
case identifier_type.name
95+
when 'isbn'
96+
IsbnRecordAndManifestation.create(
97+
manifestation: identifier.manifestation,
98+
isbn_record: IsbnRecord.find_by(body: identifier.body)
99+
)
100+
when 'issn'
101+
IssnRecordAndManifestation.create(
102+
manifestation: identifier.manifestation,
103+
issn_record: IssnRecord.find_by(body: identifier.body)
104+
)
105+
when 'issn_l'
106+
IssnRecordAndManifestation.create(
107+
manifestation: identifier.manifestation,
108+
issn_record: IssnRecord.find_by(body: identifier.body)
109+
)
110+
when 'jpno'
111+
JpnoRecord.create(
112+
manifestation: identifier.manifestation,
113+
body: identifier.body
114+
)
115+
when 'iss_itemno'
116+
NdlBibIdRecord.create(
117+
manifestation: identifier.manifestation,
118+
body: identifier.body
119+
)
120+
when 'ncid'
121+
NcidRecord.create(
122+
manifestation: identifier.manifestation,
123+
body: identifier.body
124+
)
125+
when 'lccn'
126+
LccnRecord.create(
127+
manifestation: identifier.manifestation,
128+
body: identifier.body
129+
)
130+
when 'doi'
131+
DoiRecord.create(
132+
manifestation: identifier.manifestation,
133+
body: identifier.body
134+
)
135+
end
136+
137+
identifier.destroy
138+
Rails.logger.info "#{identifier_type.name} #{identifier.body} migrated"
139+
end
140+
end
141+
end
142+
end
88143
end

0 commit comments

Comments
 (0)