From e4e5ea6110ffc8cfebfed178f650dd5032a25f20 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Mon, 9 Dec 2024 10:11:02 -0500 Subject: [PATCH] feat(ts/detours): add `activated_at` when detour is activated --- assets/src/models/createDetourMachine.ts | 7 +++ ...218135700_backfill_detour_activated_at.exs | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 priv/repo/async_migrations/20241218135700_backfill_detour_activated_at.exs diff --git a/assets/src/models/createDetourMachine.ts b/assets/src/models/createDetourMachine.ts index 74bf0fa3e..fb40fcf9e 100644 --- a/assets/src/models/createDetourMachine.ts +++ b/assets/src/models/createDetourMachine.ts @@ -36,6 +36,8 @@ export const createDetourMachine = setup({ selectedDuration?: string selectedReason?: string + + activatedAt?: Date }, input: {} as @@ -624,6 +626,11 @@ export const createDetourMachine = setup({ }, "detour.share.activate-modal.activate": { target: "Done", + actions: assign({ + // Record current time, should be done on the backend, + // but that requires a larger refactor of the state machine + activatedAt: new Date() + }) }, }, }, diff --git a/priv/repo/async_migrations/20241218135700_backfill_detour_activated_at.exs b/priv/repo/async_migrations/20241218135700_backfill_detour_activated_at.exs new file mode 100644 index 000000000..54ff4c08b --- /dev/null +++ b/priv/repo/async_migrations/20241218135700_backfill_detour_activated_at.exs @@ -0,0 +1,49 @@ +defmodule Skate.Repo.Migrations.BackfillDetourActivatedAt do + # https://fly.io/phoenix-files/backfilling-data/ + + + import Ecto.Query + use Ecto.Migration + + @disable_ddl_transaction true + @disable_migration_lock true + + def up do + # The 'backfilling-data' blog post suggests using a "throttle" function + # so that we don't update too many at once, but we currently have less than + # 1000 detours, so I think this will be negligable and not worth the + # complexity cost at _this_ time. + repo().update_all( + from( + r in Skate.Repo.Migrations.BackfillDetourActivatedAt.MigratingSchema, + select: r.id, + where: not is_nil(r.state["value"]["Detour Drawing"]["Active"]) and is_nil(r.activated_at), + update: [set: [activated_at: r.updated_at]] + ), + [], + log: :info + ) + end + + def down, do: :ok +end + +defmodule Skate.Repo.Migrations.BackfillDetourActivatedAt.MigratingSchema do + @moduledoc """ + Detours database table schema frozen at this point in time. + """ + + use Skate.Schema + + alias Skate.Settings.Db.User + + typed_schema "detours" do + field :state, :map + belongs_to :author, User + + # When this detour was activated + field :activated_at, :utc_datetime_usec + + timestamps() + end +end