From dd0d42bdf57af906490bea23f42aeb17fa3bf989 Mon Sep 17 00:00:00 2001 From: Gideon Kimutai Date: Wed, 21 Nov 2018 05:51:43 +0300 Subject: [PATCH] Change refill column type (#1273) --- app/helpers/medications_helper.rb | 2 +- app/services/medication_reminders.rb | 3 --- db/migrate/20181119152925_change_refill_column_type.rb | 9 +++++++++ db/schema.rb | 4 ++-- 4 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20181119152925_change_refill_column_type.rb diff --git a/app/helpers/medications_helper.rb b/app/helpers/medications_helper.rb index ab84b4a80a..05f416d4cd 100644 --- a/app/helpers/medications_helper.rb +++ b/app/helpers/medications_helper.rb @@ -143,7 +143,7 @@ def medication_refill { type: 'date', label: t('medications.form.refill'), - value: @medication.refill || nil, + value: @medication.refill&.to_date || nil, info: t('medications.form.refill_hint'), required: true }.merge(medication_basic_props('refill')) diff --git a/app/services/medication_reminders.rb b/app/services/medication_reminders.rb index f4187fedf6..3406f2cfb8 100644 --- a/app/services/medication_reminders.rb +++ b/app/services/medication_reminders.rb @@ -21,9 +21,6 @@ def ready_for_refill .where('medications.refill': one_week_from_now_as_string) end - # TODO: Medication Refill dates are currently stored as strings in the - # database instead of timestamps. If we want to do more complicated reminders, - # we should migrate the data to timestamps. def one_week_from_now_as_string (Time.current + 1.week).strftime('%m/%d/%Y') end diff --git a/db/migrate/20181119152925_change_refill_column_type.rb b/db/migrate/20181119152925_change_refill_column_type.rb new file mode 100644 index 0000000000..cdbe0ef6d7 --- /dev/null +++ b/db/migrate/20181119152925_change_refill_column_type.rb @@ -0,0 +1,9 @@ +class ChangeRefillColumnType < ActiveRecord::Migration[5.2] + def up + change_column(:medications, :refill, 'timestamptz USING CAST(refill AS timestamptz)') + end + + def down + change_column(:medications, :refill, :string) + end +end diff --git a/db/schema.rb b/db/schema.rb index fa02aee522..0f5d25605f 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.define(version: 2018_11_03_000904) do +ActiveRecord::Schema.define(version: 2018_11_19_152925) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -91,7 +91,6 @@ create_table "medications", force: :cascade do |t| t.string "name" t.integer "dosage" - t.string "refill" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" @@ -104,6 +103,7 @@ t.string "slug" t.boolean "add_to_google_cal", default: false t.integer "weekly_dosage", default: [0, 1, 2, 3, 4, 5, 6], array: true + t.datetime "refill" t.index ["slug"], name: "index_medications_on_slug", unique: true end