From 4a28023bbb2c86133df961a0cb67516c884b8b41 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Fri, 18 Oct 2024 18:01:05 +0200 Subject: [PATCH 1/6] write migration for changing available_from to a date --- ..._update_room_adverts_available_from_string_to_date.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb diff --git a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb new file mode 100644 index 00000000..7d9b281e --- /dev/null +++ b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb @@ -0,0 +1,9 @@ +class UpdateRoomAdvertsAvailableFromStringToDate < ActiveRecord::Migration[7.0] + def up + change_column :room_adverts, :available_from, :date + end + + def down + change_column :room_adverts, :available_from, :string + end +end From a2f88c45df1348e866f8421baabb2fbae71aff76 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Mon, 28 Oct 2024 21:15:37 +0100 Subject: [PATCH 2/6] just to be sure the migration does not fail --- ...155243_update_room_adverts_available_from_string_to_date.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb index 7d9b281e..d4ded817 100644 --- a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb +++ b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb @@ -1,5 +1,8 @@ class UpdateRoomAdvertsAvailableFromStringToDate < ActiveRecord::Migration[7.0] def up + # Clear existing data to avoid type conversion issues + RoomAdvert.update_all(available_from: nil) + # Change column type from string to date change_column :room_adverts, :available_from, :date end From 3efac148096a0de6acf6cba33015b9ae5d7cc9d0 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Mon, 28 Oct 2024 21:31:01 +0100 Subject: [PATCH 3/6] even better --- ...43_update_room_adverts_available_from_string_to_date.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb index d4ded817..bc48dae9 100644 --- a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb +++ b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb @@ -4,9 +4,16 @@ def up RoomAdvert.update_all(available_from: nil) # Change column type from string to date change_column :room_adverts, :available_from, :date + # Populate the column with a default value + Roomadvert.where(available_from: nil).update_all(available_from: Date.today) + # Make the column required + change_column_null :room_adverts, :available_from, false end def down + # Allow NULL values again + change_column_null :room_adverts, :available_from, true + # Change column type back to string change_column :room_adverts, :available_from, :string end end From e715391db6d56de5aa1c07356741167ae9c7da70 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Mon, 28 Oct 2024 22:46:22 +0100 Subject: [PATCH 4/6] I suppose this is also needed --- app/models/room_advert.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/room_advert.rb b/app/models/room_advert.rb index 53450456..9b2ac80d 100644 --- a/app/models/room_advert.rb +++ b/app/models/room_advert.rb @@ -7,6 +7,7 @@ class RoomAdvert < ApplicationRecord validates :contact, presence: true validates :description, presence: true validates :publicly_visible, inclusion: [true, false] + validates :available_from, presence: true scope :publicly_visible, (-> { where(publicly_visible: true) }) end From 2ab796674b87bb7cbe9e501cf576c37dbe71df10 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Mon, 28 Oct 2024 23:13:17 +0100 Subject: [PATCH 5/6] finally, figure out how to create a reversible migration --- ...m_adverts_available_from_string_to_date.rb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb index bc48dae9..e14fed0d 100644 --- a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb +++ b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb @@ -1,19 +1,6 @@ class UpdateRoomAdvertsAvailableFromStringToDate < ActiveRecord::Migration[7.0] - def up - # Clear existing data to avoid type conversion issues - RoomAdvert.update_all(available_from: nil) - # Change column type from string to date - change_column :room_adverts, :available_from, :date - # Populate the column with a default value - Roomadvert.where(available_from: nil).update_all(available_from: Date.today) - # Make the column required - change_column_null :room_adverts, :available_from, false - end - - def down - # Allow NULL values again - change_column_null :room_adverts, :available_from, true - # Change column type back to string - change_column :room_adverts, :available_from, :string + def change + remove_column :room_adverts, :available_from, :string + add_column :room_adverts, :available_from, :date end end From ba0d9ead7b6f43f3391978038dc4934d68a5023e Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Mon, 28 Oct 2024 23:19:05 +0100 Subject: [PATCH 6/6] oke en nu is ie ook non-null :smile: --- ...55243_update_room_adverts_available_from_string_to_date.rb | 1 + db/schema.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb index e14fed0d..7505d3b8 100644 --- a/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb +++ b/db/migrate/20241018155243_update_room_adverts_available_from_string_to_date.rb @@ -2,5 +2,6 @@ class UpdateRoomAdvertsAvailableFromStringToDate < ActiveRecord::Migration[7.0] def change remove_column :room_adverts, :available_from, :string add_column :room_adverts, :available_from, :date + change_column_null :room_adverts, :available_from, false end end diff --git a/db/schema.rb b/db/schema.rb index 3cb6bb07..428b436a 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.0].define(version: 2024_03_28_205012) do +ActiveRecord::Schema[7.0].define(version: 2024_10_18_155243) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -490,7 +490,6 @@ t.string "house_name", null: false t.string "contact", null: false t.string "location" - t.string "available_from" t.string "description", null: false t.string "cover_photo" t.boolean "publicly_visible" @@ -498,6 +497,7 @@ t.datetime "deleted_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.date "available_from", null: false end create_table "static_pages", id: :serial, force: :cascade do |t|