From a15adb9cd13910e74d7b72b59cff609a464b8edd Mon Sep 17 00:00:00 2001 From: tim-s-ccs Date: Thu, 4 Jul 2024 13:16:13 +0100 Subject: [PATCH] Update the frameworks table in preparation for the other model changes --- app/models/framework.rb | 6 +- data/frameworks.csv | 4 ++ ...0240704110527_refactor_frameworks_table.rb | 55 +++++++++++++++++++ db/schema.rb | 7 +-- lib/tasks/frameworks.rake | 25 ++------- spec/factories/frameworks.rb | 2 +- spec/models/framework_spec.rb | 2 +- spec/support/framework_status.rb | 14 ++--- 8 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 data/frameworks.csv create mode 100644 db/migrate/20240704110527_refactor_frameworks_table.rb diff --git a/app/models/framework.rb b/app/models/framework.rb index 42d339a858..31be230598 100644 --- a/app/models/framework.rb +++ b/app/models/framework.rb @@ -8,11 +8,11 @@ class Framework < ApplicationRecord validate :dates_are_valid, on: :update def self.frameworks - pluck(:framework) + pluck(:id) end def self.live_frameworks - where('live_at <= ? AND expires_at > ?', Time.now.in_time_zone('London'), Time.now.in_time_zone('London')).pluck(:framework) + where('live_at <= ? AND expires_at > ?', Time.now.in_time_zone('London'), Time.now.in_time_zone('London')).pluck(:id) end def self.current_framework @@ -28,7 +28,7 @@ def self.recognised_framework?(framework) end def status - if self.class.send(service).live_framework?(framework) + if self.class.send(service).live_framework?(id) :live else live_at <= Time.now.in_time_zone('London') ? :expired : :coming diff --git a/data/frameworks.csv b/data/frameworks.csv new file mode 100644 index 0000000000..518240ae85 --- /dev/null +++ b/data/frameworks.csv @@ -0,0 +1,4 @@ +id,service,live_at,expires_at +RM6238,supply_teachers,"2022-09-12","2026-09-01" +RM6187,management_consultancy,"2021-09-04","2025-09-04" +RM6240,legal_services,"2022-10-03","2026-10-01" \ No newline at end of file diff --git a/db/migrate/20240704110527_refactor_frameworks_table.rb b/db/migrate/20240704110527_refactor_frameworks_table.rb new file mode 100644 index 0000000000..7cabd220ce --- /dev/null +++ b/db/migrate/20240704110527_refactor_frameworks_table.rb @@ -0,0 +1,55 @@ +class RefactorFrameworksTable < ActiveRecord::Migration[7.1] + class FrameworkOld < ApplicationRecord + self.table_name = 'frameworks_old' + end + + class Framework < ApplicationRecord + self.table_name = 'frameworks' + end + + def up + rename_table :frameworks, :frameworks_old + + create_table :frameworks, id: :string, limit: 6 do |t| + t.string :service, limit: 25 + t.date :live_at + t.date :expires_at + + t.timestamps + end + + FrameworkOld.find_each do |framework| + Framework.create( + id: framework.framework, + service: framework.service, + live_at: framework.live_at, + expires_at: framework.expires_at + ) + end + + drop_table :frameworks_old + end + + def down + create_table 'frameworks_old', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.string 'service', limit: 25 + t.string 'framework', limit: 6 + t.date 'live_at' + t.timestamps + t.date 'expires_at' + end + + Framework.find_each do |framework| + FrameworkOld.create( + framework: framework.id, + service: framework.service, + live_at: framework.live_at, + expires_at: framework.expires_at + ) + end + + drop_table :frameworks + + rename_table :frameworks_old, :frameworks + end +end diff --git a/db/schema.rb b/db/schema.rb index 5975638948..773845ede2 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.1].define(version: 2023_03_14_113131) do +ActiveRecord::Schema[7.1].define(version: 2024_07_04_110527) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -43,13 +43,12 @@ t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end - create_table "frameworks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + create_table "frameworks", id: { type: :string, limit: 6 }, force: :cascade do |t| t.string "service", limit: 25 - t.string "framework", limit: 6 t.date "live_at" + t.date "expires_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.date "expires_at" end create_table "legal_services_rm6240_admin_uploads", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| diff --git a/lib/tasks/frameworks.rake b/lib/tasks/frameworks.rake index 1282d19367..0e8aafb2f7 100644 --- a/lib/tasks/frameworks.rake +++ b/lib/tasks/frameworks.rake @@ -1,27 +1,10 @@ module Frameworks - def self.rm6238_expires_at - if Rails.env.test? - 1.year.from_now - else - # This is not correct but it is far in the future and we can update it with another migration later on - Time.new(2026, 9, 1).in_time_zone('London') - end - end - - def self.rm6240_expires_at - if Rails.env.test? - 1.year.from_now - else - # This is not correct but it is far in the future and we can update it with another migration later on - Time.new(2026, 10, 1).in_time_zone('London') - end - end - def self.add_frameworks ActiveRecord::Base.connection.truncate_tables(:frameworks) - Framework.create(service: 'supply_teachers', framework: 'RM6238', live_at: Time.new(2022, 9, 12).in_time_zone('London'), expires_at: rm6238_expires_at) - Framework.create(service: 'management_consultancy', framework: 'RM6187', live_at: Time.new(2021, 9, 4).in_time_zone('London'), expires_at: Time.new(2025, 9, 4).in_time_zone('London')) - Framework.create(service: 'legal_services', framework: 'RM6240', live_at: Time.new(2022, 10, 3).in_time_zone('London'), expires_at: rm6240_expires_at) + + CSV.foreach('data/frameworks.csv', headers: true) do |row| + Framework.create(id: row['id'], service: row['service'], live_at: Time.parse(row['live_at']).in_time_zone('London'), expires_at: Rails.env.test? ? 1.year.from_now : Time.parse(row['expires_at']).in_time_zone('London')) + end end end diff --git a/spec/factories/frameworks.rb b/spec/factories/frameworks.rb index 6ed4af2f88..741f876223 100644 --- a/spec/factories/frameworks.rb +++ b/spec/factories/frameworks.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :legacy_framework, class: 'Framework' do + id { "FK#{Array.new(4) { rand(10) }.join}" } service { ('a'..'z').to_a.sample(8).join } - framework { "FK#{Array.new(4) { rand(10) }.join}" } live_at { 1.year.from_now } expires_at { 2.years.from_now } end diff --git a/spec/models/framework_spec.rb b/spec/models/framework_spec.rb index b5f4762f43..0a9e5a1672 100644 --- a/spec/models/framework_spec.rb +++ b/spec/models/framework_spec.rb @@ -259,7 +259,7 @@ end describe '.status' do - let(:result) { described_class.find_by(framework:).status } + let(:result) { described_class.find(framework).status } context 'when considering supply_teacher frameworks' do context 'and RM6238 goes live tomorrow' do diff --git a/spec/support/framework_status.rb b/spec/support/framework_status.rb index 08948b6e59..9dd981e04d 100644 --- a/spec/support/framework_status.rb +++ b/spec/support/framework_status.rb @@ -1,27 +1,27 @@ RSpec.shared_context 'and RM6238 is live in the future' do - before { Framework.find_by(framework: 'RM6238').update(live_at: 1.day.from_now) } + before { Framework.find('RM6238').update(live_at: 1.day.from_now) } end RSpec.shared_context 'and RM6238 is live today' do - before { Framework.find_by(framework: 'RM6238').update(live_at: Time.zone.now) } + before { Framework.find('RM6238').update(live_at: Time.zone.now) } end RSpec.shared_context 'and RM6238 has expired' do - before { Framework.find_by(framework: 'RM6238').update(expires_at: Time.zone.now) } + before { Framework.find('RM6238').update(expires_at: Time.zone.now) } end RSpec.shared_context 'and RM6240 is live in the future' do - before { Framework.find_by(framework: 'RM6240').update(live_at: 1.day.from_now) } + before { Framework.find('RM6240').update(live_at: 1.day.from_now) } end RSpec.shared_context 'and RM6240 is live today' do - before { Framework.find_by(framework: 'RM6240').update(live_at: Time.zone.now) } + before { Framework.find('RM6240').update(live_at: Time.zone.now) } end RSpec.shared_context 'and RM6240 has expired' do - before { Framework.find_by(framework: 'RM6240').update(expires_at: Time.zone.now) } + before { Framework.find('RM6240').update(expires_at: Time.zone.now) } end RSpec.shared_context 'and RM6187 has expired' do - before { Framework.find_by(framework: 'RM6187').update(expires_at: Time.zone.now) } + before { Framework.find('RM6187').update(expires_at: Time.zone.now) } end