Skip to content

Commit

Permalink
feat(ts/detours): add activated_at when detour is activated
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Dec 18, 2024
1 parent d0b28dc commit e4e5ea6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions assets/src/models/createDetourMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const createDetourMachine = setup({

selectedDuration?: string
selectedReason?: string

activatedAt?: Date
},

input: {} as
Expand Down Expand Up @@ -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()
})
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e4e5ea6

Please sign in to comment.