diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index 5be4f1bd..d3a00281 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -37,7 +37,6 @@ jobs: bundle exec rails db:create bundle exec rails db:migrate bundle exec rails db:schema:load - bundle exec rails db:seed - name: Set up certs run: | # multi-line run command: https://stackoverflow.com/a/66809682/6410635 diff --git a/config/environments/development.rb b/config/environments/development.rb index 08445202..30063746 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -85,4 +85,10 @@ "localhost:3001", "127.0.0.1:3001" ] + + # Log to STDOUT by default + # config.logger = ActiveSupport::Logger.new(STDOUT) + config.logger = ActiveSupport::Logger.new($stdout) + .tap { |logger| logger.formatter = ::Logger::Formatter.new } + .then { |logger| ActiveSupport::TaggedLogging.new(logger) } end diff --git a/config/environments/test.rb b/config/environments/test.rb index 7d17b090..565ff9bb 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -64,4 +64,10 @@ # Raise error when a before_action's only/except options reference missing actions. config.action_controller.raise_on_missing_callback_actions = true + + # Log to STDOUT by default + # config.logger = ActiveSupport::Logger.new(STDOUT) + config.logger = ActiveSupport::Logger.new($stdout) + .tap { |logger| logger.formatter = ::Logger::Formatter.new } + .then { |logger| ActiveSupport::TaggedLogging.new(logger) } end diff --git a/db/schema.rb b/db/schema.rb index 10c1ba2f..fa6065e5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_01_04_040902) do +ActiveRecord::Schema[8.0].define(version: 2025_01_04_040902) do create_table "addresses", force: :cascade do |t| t.string "street", null: false t.string "street2" diff --git a/db/seeds/models/legislator.rb b/db/seeds/models/legislator.rb index e3e89d8c..eac83cce 100644 --- a/db/seeds/models/legislator.rb +++ b/db/seeds/models/legislator.rb @@ -41,7 +41,6 @@ def initialize(j, sway_locale) party: Legislator.to_party_char_from_name(j.fetch("partyName")) ).present? Rails.logger.info("SKIP Seeding Congressional Legislator #{bioguide_id}. Already exists by external_id, district.name and party char.") - puts "SKIP Seeding Congressional Legislator #{bioguide_id}. Already exists by external_id, district.name and party char." return end @@ -87,7 +86,6 @@ def seed sig { returns(Legislator) } def legislator Rails.logger.info("Seeding #{sway_locale.city.titleize} Legislator #{external_id}") - puts "Seeding #{sway_locale.city.titleize} Legislator #{external_id}" l = Legislator.find_or_initialize_by( external_id:, @@ -95,8 +93,6 @@ def legislator last_name: ) - Legislator.find_by(district: district)&.update_attribute(:active, false) - l.address = address l.district = district l.title = title diff --git a/lib/tasks/assign_new_user_legislators.rake b/lib/tasks/assign_new_user_legislators.rake index 59c14ee5..9c814f32 100644 --- a/lib/tasks/assign_new_user_legislators.rake +++ b/lib/tasks/assign_new_user_legislators.rake @@ -2,13 +2,11 @@ namespace :sway do desc "De-activates all UserLegislators and assigns new ones to each user. Run after each election." task assign_new_user_legislators: :environment do Rails.logger.info("assign_new_user_legislators.rake - de-activate all UserLegislators") - puts "assign_new_user_legislators.rake - de-activate all UserLegislators" UserLegislator.update_all(active: false) User.all.each do |user| user.sway_locales.each do |sway_locale| Rails.logger.info("assign_new_user_legislators.rake - Updating UserLegislators for User: #{user.id}, SwayLocale: #{sway_locale.name}") - puts "assign_new_user_legislators.rake - Updating UserLegislators for User: #{user.id}, SwayLocale: #{sway_locale.name}" SwayRegistrationService.new(user, user.address, sway_locale, invited_by_id: nil).create_user_legislators end end diff --git a/lib/tasks/deactivate_old_legislators.rake b/lib/tasks/deactivate_old_legislators.rake new file mode 100644 index 00000000..1cf44d02 --- /dev/null +++ b/lib/tasks/deactivate_old_legislators.rake @@ -0,0 +1,13 @@ +namespace :sway do + # Call like: ./bin/rake "sway:deactivate_old_legislators[[2024]]" + + desc "De-activates all Legislators that do not have an election_year within the passed array of election years." + task :deactivate_old_legislators, [:election_years] => [:environment] do |task, args| + election_years = JSON.parse(args[:election_years]) + + Rails.logger.info("deactivate_old_legislators.rake - de-activate all Legislators in election years: #{election_years}") + + ids = Legislator.where(active: true).filter { |legislator| !election_years.include?(legislator.election_year) }.map(&:id) + Legislator.where(id: ids).update_all(active: false) + end +end diff --git a/lib/tasks/sway.rake b/lib/tasks/sway.rake index cba8a97c..3e11a577 100644 --- a/lib/tasks/sway.rake +++ b/lib/tasks/sway.rake @@ -21,17 +21,17 @@ namespace :sway do end def backup_db(attempt = 0) - puts "sway.rake -> backup_db attempt #{attempt}" + Rails.logger.info("sway.rake -> backup_db attempt #{attempt}") if File.exist? "storage/production.sqlite3" - puts "sway.rake -> Uploading production.sqlite3 to google storage as backup." + Rails.logger.info("sway.rake -> Uploading production.sqlite3 to google storage as backup.") upload_file(bucket_name: "sway-sqlite", bucket_file_path: "production.sqlite3", local_file_path: "/rails/storage/production.sqlite3") elsif attempt < 5 sleep 1 backup_db(attempt + 1) # else - # puts 'Getting production.sqlite3 from google storage backup.' + # Rails.logger.info('Getting production.sqlite3 from google storage backup.') # download_file(bucket_name: 'sway-sqlite', bucket_file_path: 'production.sqlite3', # local_file_path: 'storage/production.sqlite3') end