From 92dbcbee9b7fb39142a818076e5bc14786419597 Mon Sep 17 00:00:00 2001 From: Ivan Kocienski Date: Mon, 30 Jan 2023 14:55:41 +0000 Subject: [PATCH 1/5] Drop `seed_migration` in favour of native seeds --- .erdconfig | 2 +- Gemfile | 3 -- Gemfile.lock | 2 - bin/setup | 1 - bin/update | 3 +- db/seeds.rb | 20 ++++----- db/seeds_to_migrate.rb | 100 ----------------------------------------- 7 files changed, 11 insertions(+), 120 deletions(-) delete mode 100644 db/seeds_to_migrate.rb diff --git a/.erdconfig b/.erdconfig index bc62f56b2..7b8351c7e 100644 --- a/.erdconfig +++ b/.erdconfig @@ -14,7 +14,7 @@ polymorphism: false sort: true warn: true title: PlaceCal Database Model -exclude: ActiveRecord::InternalMetadata, ActiveRecord::SchemaMigration, SeedMigration::DataMigration, Delayed::Backend::ActiveRecord::Job +exclude: ActiveRecord::InternalMetadata, ActiveRecord::SchemaMigration, Delayed::Backend::ActiveRecord::Job only: null only_recursion_depth: null prepend_primary: false diff --git a/Gemfile b/Gemfile index 6f2122135..f51a60a6e 100644 --- a/Gemfile +++ b/Gemfile @@ -75,9 +75,6 @@ gem 'delayed_job_active_record' gem 'graphql' gem 'rack-cors', require: 'rack/cors' -# Seeds and data -gem 'seed_migration' - # Utilities gem 'active_link_to' gem 'bootsnap', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 65feba48c..79c5a78a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -445,7 +445,6 @@ GEM sprockets (> 3.0) sprockets-rails tilt - seed_migration (1.2.3) select2-rails (4.0.13) selenium-webdriver (4.2.1) childprocess (>= 0.5, < 5.0) @@ -593,7 +592,6 @@ DEPENDENCIES rubocop-rails rubocop-rake sass-rails (= 6.0.0) - seed_migration select2-rails selenium-webdriver sendgrid-actionmailer diff --git a/bin/setup b/bin/setup index c27d48071..74727c214 100755 --- a/bin/setup +++ b/bin/setup @@ -25,7 +25,6 @@ FileUtils.chdir APP_ROOT do puts "\n== Preparing database ==" system! 'bin/rails db:prepare' - system! 'bin/rails seed:migrate' puts "\n== Importing events ==" system! 'bin/rails import:all_events' diff --git a/bin/update b/bin/update index 3f37c6a80..1b601fab9 100755 --- a/bin/update +++ b/bin/update @@ -22,8 +22,7 @@ chdir APP_ROOT do system! 'bin/yarn install' puts "\n== Updating database ==" - system! 'bin/rails db:migrate' - system! 'bin/rails seed:migrate' + system! 'bin/rails db:migrate db:seed' puts "\n== Removing old logs and tempfiles ==" system! 'bin/rails log:clear tmp:clear' diff --git a/db/seeds.rb b/db/seeds.rb index e2e1822f8..186062005 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,17 +1,15 @@ # frozen_string_literal: true -# This file is auto-generated from the current content of the database. Instead -# of editing this file, please use the migrations feature of Seed Migration to -# incrementally modify your database, and then regenerate this seed file. +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). # -# If you need to create the database on another system, you should be using -# db:seed, not running all the migrations from scratch. The latter is a flawed -# and unsustainable approach (the more migrations you'll amass, the slower -# it'll run and the greater likelihood for issues). +# Examples: +# +# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) +# Character.create(name: 'Luke', movie: movies.first) # -# It's strongly recommended to check this file into your version control system. -ActiveRecord::Base.transaction do +Dir[Rails.root.join('db/seeds/*.rb')].sort.each do |file| + Rails.logger.debug { "Seeding from #{file.split('/').slice(-3, 3).join('/')}" } + require file end - -SeedMigration::Migrator.bootstrap(20_180_625_172_033) diff --git a/db/seeds_to_migrate.rb b/db/seeds_to_migrate.rb deleted file mode 100644 index 636842ce1..000000000 --- a/db/seeds_to_migrate.rb +++ /dev/null @@ -1,100 +0,0 @@ -# frozen_string_literal: true - -# TODO: Get all this junk in seed migrations, or get rid - -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) -# - -# Create Partners - -Calendar.create(name: 'Zion Centre Guide', - type: :outlook, - partner_id: blc.id, - place_id: zion_place.id, - source: 'https://outlook.office365.com/owa/calendar/8a1f38963ce347bab8cfe0d0d8c5ff16@thebiglifegroup.com/5c9fc0f3292e4f0a9af20e18aa6f17739803245039959967240/calendar.ics') - -Calendar.create(name: 'Kath Locke Centre Guide', - type: :outlook, - partner_id: blc.id, - place_id: kath_locke_place.id, - strategy: 'room_number', - source: 'https://outlook.office365.com/owa/calendar/51588529b2f24206a4b1098e837159c7@thebiglifegroup.com/9d568b127b2c4760b726aff87e3ddaca9265853961212541948/calendar.ics') - -# Red Bricks - Bentley House TARA Office -bentley_address = Address.create(street_address: '26 Humberstone Ave', city: 'Manchester', postcode: 'M155FD') -red_bricks = Partner.create(name: 'Red Bricks') -bentley_place = Place.create(name: 'Bentley House TARA Office', address: bentley_address) -red_bricks.places << bentley_place - -Calendar.create(name: "Red Bricks What's On", - type: :other, - partner_id: red_bricks.id, - place_id: bentley_place.id, - source: 'https://www.redbricks.org/events/') - -# Afro-Carribbean Care Group (ACCG) - ACCG Centre -accg_address = Address.create(street_address: 'Rolls Crescent', city: 'Manchester', postcode: 'M15 5FS') -african_caribbean = Partner.create(name: 'Afro-Carribbean Care Group (ACCG)') -accg_place = Place.create(name: 'ACCG Centre', address: accg_address) -Calendar.create(name: 'African Caribbean Care Group', - type: :outlook, - partner_id: african_caribbean.id, - place_id: accg_place.id, - source: 'https://outlook.office365.com/owa/calendar/53862ba33d814e8a8ca983ebbf796fee@accg.org.uk/c4100179b7ca49a88916b4a1c41de9fd686060547943940178/calendar.ics') - -# Old Abbey Taphouse - Old Abbey Taphouse -abbey_address = Address.create(street_address: 'Guildhall Close', city: 'Manchester', postcode: 'M15 6SY') -abbey_taphouse = Partner.create(name: 'Old Abbey Taphouse') -abbey_taphouse_place = Place.create(name: 'Old Abbey Taphouse', address: abbey_address) - -Calendar.create(name: 'Old Abbey Taphouse', - type: :facebook, - partner_id: abbey_taphouse.id, - place_id: abbey_taphouse_place.id, - source: 'abbeyinnmcr') - -# Hulme & Moss Side Age Friendly Events -hulme_moss = Partner.create(name: 'Hulme & Moss Side Age Friendly Events') - -Turf.where(name: 'Hulme', - slug: 'hulme', - turf_type: 'neighbourhood', - description: 'hulme').first_or_create -Turf.where(name: 'Moss side', - slug: 'moss-side', - turf_type: 'neighbourhood', - description: 'moss-side').first_or_create -Turf.where(name: 'Rusholme', - slug: 'rusholme', - turf_type: 'neighbourhood', - description: 'rusholme').first_or_create -Turf.where(name: 'Age friendly', - slug: 'age-friendly', - turf_type: 'interest', - description: 'age-friendly').first_or_create - -u = User.where(first_name: 'Place', last_name: 'Cal', role: 'root', email: 'info@placecal.org').first_or_initialize -unless u.persisted? - u.password = 'password' - u.save -end - -u.turfs << Turf.first -u.turfs << Turf.last - -s = Site.where(name: 'Hulme', - domain: 'hulme.placecal.org', - description: 'PlaceCal is part of the Hulme and Moss Side Age Friendly Partnership, who are working to make Hulme a better place to be for over 50s.').first_or_create -s.sites_turfs.create(turf_id: Turf.first.id, relation_type: 'primary') - -s1 = Site.where(name: 'South', - domain: 'south.placecal.org', - description: 'south').first_or_create -s1.sites_turfs.create(turf_id: Turf.first.id, relation_type: 'secondary') -s1.sites_turfs.create(turf_id: Turf.last.id, relation_type: 'secondary') From 5d008f62f3af9842064b3166a2e769fc2b370a65 Mon Sep 17 00:00:00 2001 From: Ivan Kocienski Date: Mon, 30 Jan 2023 15:05:31 +0000 Subject: [PATCH 2/5] Use the seeds to create an admin user --- README.md | 2 +- bin/setup | 3 --- db/seeds/001_users.rb | 10 ++++++++++ 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 db/seeds/001_users.rb diff --git a/README.md b/README.md index 0c6a2a29d..68e608ad9 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ bin/setup Amongst other things, this will create an admin user for you: -- Username: `admin@placecal.org` +- Username: `admin@lvh.me` - Password: `password` ### Run the thing diff --git a/bin/setup b/bin/setup index 74727c214..779fcb3c2 100755 --- a/bin/setup +++ b/bin/setup @@ -29,9 +29,6 @@ FileUtils.chdir APP_ROOT do puts "\n== Importing events ==" system! 'bin/rails import:all_events' - puts "\n== Creating admin user ==" - system! 'bin/rails runner \'User.where(email: "admin@placecal.org").exists? || User.create!(email: "admin@placecal.org", password: "password", password_confirmation: "password", role: :root)\'' - puts "\n== Removing old logs and tempfiles ==" system! 'bin/rails log:clear tmp:clear' diff --git a/db/seeds/001_users.rb b/db/seeds/001_users.rb new file mode 100644 index 000000000..264e5eb05 --- /dev/null +++ b/db/seeds/001_users.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +User.find_or_create_by!( + email: 'admin@lvh.me' +) do |user| + user.password = 'password' + user.password_confirmation = 'password' +end.update!( + role: :root +) From 932abad31e45987f5f1618267bf353583a2ad74f Mon Sep 17 00:00:00 2001 From: Ivan Kocienski Date: Mon, 30 Jan 2023 15:10:06 +0000 Subject: [PATCH 3/5] Seed for default site --- db/seeds/002_site.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 db/seeds/002_site.rb diff --git a/db/seeds/002_site.rb b/db/seeds/002_site.rb new file mode 100644 index 000000000..d7f22a484 --- /dev/null +++ b/db/seeds/002_site.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +Site.find_or_create_by!( + slug: 'default-site' +) do |site| + site.name = 'Normal Island' + site.domain = "#{site.slug}.lvh.me" +end From f93b87fea5652c69e3239c0f9441da9a355756ad Mon Sep 17 00:00:00 2001 From: Ivan Kocienski Date: Mon, 30 Jan 2023 15:14:59 +0000 Subject: [PATCH 4/5] `rails db:info` database info dump --- lib/tasks/db.rake | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index d1f7b6cf6..cbb0a4554 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -225,6 +225,42 @@ namespace :db do $stdout.puts '... done.' end + desc 'Prints out a list of tables and their counts' + task info: :environment do + IGNORE_TABLES = %w[ar_internal_metadata delayed_jobs schema_migrations seed_migration_data_migrations].freeze + + puts 'Database Info:' + puts " Rails.env=#{Rails.env}" + + db_config = Rails.configuration.database_configuration[Rails.env] + puts " database='#{db_config['database']}'" + puts " host='#{db_config['host']}'" + puts " user='#{db_config['username']}'" + puts " password=[#{'*' * db_config['password'].length}]" + puts '' + + max_len = 0 + table_info = [] + + ActiveRecord::Base.connection.tables.sort.each do |table| + next if IGNORE_TABLES.include?(table) + + sql = "select count(*) from #{table}" + result = ActiveRecord::Base.connection.execute(sql) + count = result.first['count'] + + max_len = table.length if table.length > max_len + table_info << [table, count] + end + + table_info.each do |name, count| + count_s = count.positive? ? count : '.' + puts " #{name.rjust(max_len)} #{count_s}" + end + + puts '' + end + private def ensure_format(format) From cdb330418b255b136813563eeb63bf2f34c92a9f Mon Sep 17 00:00:00 2001 From: Ivan Kocienski Date: Tue, 31 Jan 2023 12:03:44 +0000 Subject: [PATCH 5/5] User seed migration authenticates admin user Sends invitation, accepts invitation. Now you can log in with `admin@lvh.me` and `password`. --- db/seeds/001_users.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/db/seeds/001_users.rb b/db/seeds/001_users.rb index 264e5eb05..4c2011d44 100644 --- a/db/seeds/001_users.rb +++ b/db/seeds/001_users.rb @@ -1,10 +1,18 @@ # frozen_string_literal: true -User.find_or_create_by!( - email: 'admin@lvh.me' -) do |user| - user.password = 'password' - user.password_confirmation = 'password' -end.update!( - role: :root -) +module UserSeeder + module_function + + def run + user = User.find_or_create_by!(email: 'admin@lvh.me') do |u| + u.password = 'password' + u.password_confirmation = 'password' + u.role = :root + end + + user.invite! + user.accept_invitation! + end +end + +UserSeeder.run