Skip to content

Commit

Permalink
Merge pull request #1662 from geeksforsocialchange/ik-1480-db-seeding
Browse files Browse the repository at this point in the history
Begin improving seeds
  • Loading branch information
ivan-kocienski-gfsc authored Jan 31, 2023
2 parents 2e8835c + cdb3304 commit 428a0a7
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .erdconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -593,7 +592,6 @@ DEPENDENCIES
rubocop-rails
rubocop-rake
sass-rails (= 6.0.0)
seed_migration
select2-rails
selenium-webdriver
sendgrid-actionmailer
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ 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'

puts "\n== Creating admin user =="
system! 'bin/rails runner \'User.where(email: "[email protected]").exists? || User.create!(email: "[email protected]", password: "password", password_confirmation: "password", role: :root)\''

puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'

Expand Down
3 changes: 1 addition & 2 deletions bin/update
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
20 changes: 9 additions & 11 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions db/seeds/001_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module UserSeeder
module_function

def run
user = User.find_or_create_by!(email: '[email protected]') do |u|
u.password = 'password'
u.password_confirmation = 'password'
u.role = :root
end

user.invite!
user.accept_invitation!
end
end

UserSeeder.run
8 changes: 8 additions & 0 deletions db/seeds/002_site.rb
Original file line number Diff line number Diff line change
@@ -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
100 changes: 0 additions & 100 deletions db/seeds_to_migrate.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 428a0a7

Please sign in to comment.