Skip to content

Commit

Permalink
Added verbose (failure) logging to the seeding process
Browse files Browse the repository at this point in the history
  • Loading branch information
SpookyBoy99 committed Sep 15, 2024
1 parent b029f49 commit c995266
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 111 deletions.
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@
require Rails.root.join('db', 'seeds', 'payments.rb')
require Rails.root.join('db', 'seeds', 'posts.rb')

puts '-- done'
puts '-- Done'
77 changes: 40 additions & 37 deletions db/seeds/activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,53 @@
# 2: Single day, all day
# 3: Multiple days, with start and end time
# 4: Single day, with start and end time
@multiple = Faker::Boolean.boolean
@single = !@multiple
@entire = Faker::Boolean.boolean
@part = !@entire
@multiple = Faker::Boolean.boolean
@single = !@multiple
@entire = Faker::Boolean.boolean
@part = !@entire

viewable = Faker::Boolean.boolean(true_ratio: 0.9)
viewable = Faker::Boolean.boolean(true_ratio: 0.9)
enrollable = viewable ? Faker::Boolean.boolean(true_ratio: 0.9) : false
notes = Faker::Boolean.boolean(true_ratio: 0.2) ? Faker::Lorem.question : nil

activity = Activity.create(
name: Faker::Hacker.ingverb.capitalize,
price: Faker::Commerce.price / 5,
name: Faker::Hacker.ingverb.capitalize,
price: Faker::Commerce.price / 5,

# TODO: use date as timefield as well in model, perhaps add boolean for all day
start_date: start_date,
start_time: @part ? start_date.to_time : nil,

end_date: @multiple ? Faker::Date.between(from: start_date + 1.day, to: start_date + 7.days) : nil,
end_time: @part ? Faker::Time.between_dates(from: start_date, to: Date.today, period: :evening) : nil,

location: Faker::TvShows::FamilyGuy.location,
organized_by: Faker::Boolean.boolean(true_ratio: 0.8) ? Group.all.sample.id : nil,
description_nl: Faker::Lorem.paragraph(sentence_count: 5),
description_en: Faker::Lorem.paragraph(sentence_count: 5),

is_enrollable: enrollable,
is_masters: Faker::Boolean.boolean(true_ratio: 0.2),
is_viewable: viewable,
is_alcoholic: Faker::Boolean.boolean(true_ratio: 0.2),
is_freshmans: Faker::Boolean.boolean(true_ratio: 0.2),
is_sophomores: Faker::Boolean.boolean(true_ratio: 0.2),
is_seniors: Faker::Boolean.boolean(true_ratio: 0.2),
is_payable: Faker::Boolean.boolean(true_ratio: 0.8),
start_date: start_date,
start_time: @part ? start_date.to_time : nil,

end_date: @multiple ? Faker::Date.between(from: start_date + 1.day, to: start_date + 7.days) : nil,
end_time: @part ? Faker::Time.between_dates(from: start_date, to: Date.today, period: :evening) : nil,

location: Faker::TvShows::FamilyGuy.location,
organized_by: Faker::Boolean.boolean(true_ratio: 0.8) ? Group.all.sample.id : nil,
description_nl: Faker::Lorem.paragraph(sentence_count: 5),
description_en: Faker::Lorem.paragraph(sentence_count: 5),

is_enrollable: enrollable,
is_masters: Faker::Boolean.boolean(true_ratio: 0.2),
is_viewable: viewable,
is_alcoholic: Faker::Boolean.boolean(true_ratio: 0.2),
is_freshmans: Faker::Boolean.boolean(true_ratio: 0.2),
is_sophomores: Faker::Boolean.boolean(true_ratio: 0.2),
is_seniors: Faker::Boolean.boolean(true_ratio: 0.2),
is_payable: Faker::Boolean.boolean(true_ratio: 0.8),

participant_limit: enrollable && Faker::Boolean.boolean(true_ratio: 0.5) ? Faker::Number.within(range: 2..18) : nil,

VAT: ["0","9","21"].sample,
VAT: ["0", "9", "21"].sample,

notes: notes,
notes_mandatory: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.2) : false,
notes_public: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.6) : false
notes: notes,
notes_mandatory: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.2) : false,
notes_public: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.6) : false
)

puts(" -> #{ activity.name } (#{ start_date })#{', enrollable' if enrollable}" )
puts " [#{ activity.valid? ? ' Ok ' : 'Fail' }] #{ activity.name } (#{ start_date })#{ ', enrollable' if enrollable }"
activity.errors.objects.each do |error|
puts " > #{error.full_message}"
end

activity.poster.attach(io: File.open('public/poster-example.pdf'), filename: 'poster-example.pdf', content_type: 'application/pdf')

Expand All @@ -77,12 +80,12 @@

eligible.sample(Faker::Number.within(range: 18..22)).each do |member|
Participant.create(
member: member,
activity: activity,
reservist: true,
price: (Faker::Boolean.boolean(true_ratio: 0.2) ? Faker::Commerce.price / 5 : nil),
paid: Faker::Boolean.boolean(true_ratio: 0.4), # if price is 0 then the paid attribute is not used
notes: response
member: member,
activity: activity,
reservist: true,
price: (Faker::Boolean.boolean(true_ratio: 0.2) ? Faker::Commerce.price / 5 : nil),
paid: Faker::Boolean.boolean(true_ratio: 0.4), # if price is 0 then the paid attribute is not used
notes: response
)
end

Expand Down
24 changes: 16 additions & 8 deletions db/seeds/groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
puts '-- Creating committees'
8.times do
group = Group.create(
name: Faker::Team.unique.name,
category: 2,
name: Faker::Team.unique.name,
category: 2,
created_at: Faker::Date.between(from: 3.years.ago, to: Date.today - 1.year)
)

puts " [#{ group.valid? ? ' Ok ' : 'Fail' }] #{ group.name } (#{ group.created_at.study_year }-#{ Date.today.study_year })"
group.errors.objects.each do |error|
puts " > #{error.full_message}"
end

Member.all.sample(10).each do |member|
GroupMember.create(
member: member,
group: group,
year: Faker::Date.between(from: [member.join_date, group.created_at].max, to: Date.today).study_year,
group_member = GroupMember.create(
member: member,
group: group,
year: Faker::Date.between(from: [member.join_date, group.created_at].max, to: Date.today).study_year,
position: Faker::Boolean.boolean(true_ratio: 0.5) ? group.positions.sample : nil
)
end

puts(" -> #{ group.name } (#{ group.created_at.study_year }-#{ Date.today.study_year })")
puts " [#{ group_member.valid? ? ' Ok ' : 'Fail' }] + #{ group_member.member.first_name } #{ group_member.member.last_name }"
group_member.errors.objects.each do |error|
puts " > #{error.full_message}"
end
end
end
48 changes: 28 additions & 20 deletions db/seeds/members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,52 @@
puts '-- Creating members'
60.times do
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
last_name = Faker::Name.last_name

member = Member.create(
first_name: first_name,
infix: (Faker::Number.within(range: 1..10) > 7 ? Faker::Name.tussenvoegsel : ' '),
last_name: last_name,
address: Faker::Address.street_name,
first_name: first_name,
infix: (Faker::Number.within(range: 1..10) > 7 ? Faker::Name.tussenvoegsel : ' '),
last_name: last_name,
address: Faker::Address.street_name,
house_number: Faker::Address.building_number,
postal_code: Faker::Address.postcode,
city: Faker::Address.city,
postal_code: Faker::Address.postcode,
city: Faker::Address.city,
phone_number: Faker::Base.numerify('+316########'),
emergency_phone_number: Faker::Base.numerify('+316########'),
email: Faker::Internet.safe_email(name: (first_name + '.' + last_name).parameterize),
student_id: "F#{ Faker::Number.number(digits: 6) }",
birth_date: Faker::Date.between(from: 28.years.ago, to: 16.years.ago),
join_date: Faker::Date.between(from: 6.years.ago, to: Date.today),
comments: (Faker::Boolean.boolean(true_ratio: 0.3) ? Faker::Hacker.say_something_smart : nil)
email: Faker::Internet.safe_email(name: (first_name + '.' + last_name).parameterize),
student_id: "F#{ Faker::Number.number(digits: 6) }",
birth_date: Faker::Date.between(from: 28.years.ago, to: 16.years.ago),
join_date: Faker::Date.between(from: 6.years.ago, to: Date.today),
comments: (Faker::Boolean.boolean(true_ratio: 0.3) ? Faker::Hacker.say_something_smart : nil)
)

puts(" -> #{ member.name } (#{ member.student_id })")
puts " [#{ member.valid? ? ' Ok ' : 'Fail' }] #{ member.name } (#{ member.student_id })"
member.errors.objects.each do |error|
puts " > #{error.full_message}"
end
end

puts '-- Creating educations'
Member.all.each do |member|
Faker::Number.within(range: 1..3).times do
# because of the [member, study, start_date] key this goes wrong often
suppress(ActiveRecord::RecordNotUnique) do
status = Faker::Number.within(range: 0..2)
status = Faker::Number.within(range: 0..2)
start_date = Faker::Date.between(from: member.join_date, to: Date.today)
end_date = (status > 0 ? Faker::Date.between(from: start_date, to: Date.today) : nil)
end_date = (status > 0 ? Faker::Date.between(from: start_date, to: Date.today) : nil)

Education.create(
member_id: member.id,
study_id: Study.all.sample.id,
education = Education.create(
member_id: member.id,
study_id: Study.all.sample.id,
start_date: start_date,
end_date: end_date,
status: status
end_date: end_date,
status: status
)

puts " [#{ education.valid? ? ' Ok ' : 'Fail' }] #{ education.study_id } for #{ education.member_id }"
education.errors.objects.each do |error|
puts " > #{error.full_message}"
end
end
end
end
61 changes: 31 additions & 30 deletions db/seeds/payments.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
require Rails.root.join('db', 'seeds', 'members.rb')
require Rails.root.join('db', 'seeds', 'activities.rb')

puts '-- member transaction creation'
puts '-- Creating member transactions'

Member.all.sample(30).each do |member|

15.times do
transactiontype = Faker::Number.within(range: 0..1)
# either ideal or pin (see app/models/payment.rb)
paymenttype = Faker::Number.within(range: 0.0..1.0) < 0.5 ? 0 : 3
status = Faker::Number.within(range:0..2)
if transactiontype == 1 && status == 0
participants = Participant.where(member:member).where.not(activity:[nil,1]).select{|p| p.currency != nil}.sample(Faker::Number.within(range:1..6))
participants.map {|p|p.update(paid:true)}
transaction_id = participants.map{|p| p.activity.id}
amount = 0
participants.map{ |p| amount += p.currency}
status = 2
else
transaction_id = [1]
status = 0
end
description = transactiontype == 0 ? "Mongoose Opwaardering" : "Activiteiten - #{transaction_id}"

@payment =Payment.create(member: member,
transaction_type: transactiontype,
payment_type: paymenttype ,
transaction_id: transaction_id,
description: description,
amount: Faker::Number.within(range: 1.0..10.0),
status: status,
trxid: Digest::MD5.hexdigest(description + Time.now.to_f.to_s),
created_at: Faker::Date.between(from: 1.weeks.ago, to: Date.today),
message: 'seeding',
redirect_uri: ENV['KOALA_DOMAIN']
status = Faker::Number.within(range: 0..2)
payment_type = Faker::Number.within(range: 0.0..1.0) < 0.5 ? 0 : 3

participants = Participant.where(member: member).where.not(activity: [nil, 1]).select { |p| p.currency != nil }.sample(Faker::Number.within(range: 1..6))

participants.map { |p| p.update(paid: true) }

amount = 0
participants.map { |p| amount += p.currency }

transaction_id = participants.map{|p| p.activity.id}

description = "Activiteiten - #{transaction_id}"

payment = Payment.create(member: member,
transaction_type: 1,
payment_type: payment_type,
transaction_id: transaction_id,
description: description,
amount: amount,
status: status,
trxid: Digest::MD5.hexdigest(description + Time.now.to_f.to_s),
created_at: Faker::Date.between(from: 1.weeks.ago, to: Date.today),
message: 'seeding',
redirect_uri: ENV['KOALA_DOMAIN']
)

puts " [#{ payment.valid? ? ' Ok ' : 'Fail' }] #{['Failed', 'In progress', 'Successful'][status]} transaction by #{ payment.member.first_name } #{ payment.member.last_name } of #{ payment.amount } for #{ payment.transaction_id } using #{ payment_type == 0 ? "ideal" : "pin" }"
payment.errors.objects.each do |error|
puts " > #{error.full_message}"
end
end
end
8 changes: 6 additions & 2 deletions db/seeds/posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
6.times do
created_at = Faker::Time.between(from: 2.years.ago, to: Date.yesterday)

Post.create(
post = Post.create(
title: Faker::Movie.title,
status: [0,1].sample,
status: [0, 1].sample,
tags: Faker::Lorem.sentence(word_count: 2),

author: Admin.all.sample,
Expand All @@ -18,4 +18,8 @@
content: Faker::Lorem.paragraph(sentence_count: 5)
)

puts " [#{ post.valid? ? ' Ok ' : 'Fail' }] #{ post.title } by #{ post.author.first_name } #{ post.author.last_name }"
post.errors.objects.each do |error|
puts " > #{error.full_message}"
end
end
38 changes: 25 additions & 13 deletions db/seeds/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,46 @@

# Creating login accounts
puts '-- Creating users'
Admin.create(
email: '[email protected]',

dev = Admin.create(
email: '[email protected]',
password: 'sticky123'
)
puts ' -> [email protected] (admin)'

Admin.create(
email: '[email protected]',
password: 'sticky123',
puts " [#{ dev.valid? ? ' Ok ' : 'Fail' }] [email protected] (admin)"
dev.errors.objects.each do |error|
puts " > #{error.full_message}"
end

martijn = Admin.create(
email: '[email protected]',
password: 'sticky123',

first_name: 'Martijn',
last_name: 'Casteel',
signature: 'Martijn'
last_name: 'Casteel',
signature: 'Martijn'
)
puts ' -> [email protected] (admin)'

puts " [#{ martijn.valid? ? ' Ok ' : 'Fail' }] [email protected] (admin)"
martijn.errors.objects.each do |error|
puts " > #{error.full_message}"
end

# create actual members, but give the first member [email protected]
member = Member.joins(:educations).where(educations: { status: 0 }).first
member.update(email: '[email protected]')

user = User.new(
email: member.email,
password: 'sticky123',
email: member.email,
password: 'sticky123',
credentials: member,
language: Faker::Number.within(range: 0..1)
language: Faker::Number.within(range: 0..1)
)

user.skip_confirmation!
user.save!

puts " -> #{ user.email } (member)"
puts " [#{ user.valid? ? ' Ok ' : 'Fail' }] #{ user.email } (member)"
user.errors.objects.each do |error|
puts " > #{error.full_message}"
end

0 comments on commit c995266

Please sign in to comment.