-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: RoutePatterns table, part 1: Adding to db #2900
base: main
Are you sure you want to change the base?
Changes from 3 commits
a3de97c
e28f18a
96daebf
e395753
3b255f1
6571983
9751804
52b9580
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Should this live here under detours? | ||
# I don't like how the db folders are scattered throughout /lib subdirectories. | ||
# I'd rather they all live in one subfolder. | ||
# Example: there's db files in /notifications and /skate/detours and /skate/settings, etc | ||
defmodule Skate.Detours.Db.RoutePattern do | ||
@moduledoc """ | ||
Ecto Model for `route_patterns` Database table | ||
""" | ||
|
||
use Skate.Schema | ||
import Ecto.Changeset | ||
|
||
alias Skate.Detours.Db.Detour | ||
alias Skate.Detours.DirectionName | ||
|
||
@required_fields [ | ||
:hash, | ||
:gtfs_route_pattern_id, | ||
:gtfs_route_pattern_name, | ||
:gtfs_route_pattern_headsign, | ||
# :gtfs_route_pattern_shape, | ||
:gtfs_route_pattern_direction_name, | ||
:gtfs_route_id, | ||
:gtfs_route_name | ||
] | ||
|
||
typed_schema "route_patterns" do | ||
field(:hash, :integer) | ||
field(:gtfs_route_pattern_id, :string) | ||
field(:gtfs_route_pattern_name, :string) | ||
field(:gtfs_route_pattern_headsign, :string) | ||
field(:gtfs_route_pattern_direction_name, DirectionName) | ||
|
||
# The given route pattern will always have the given route | ||
# Does it need to be stored here as well? | ||
field(:gtfs_route_id, :string) | ||
field(:gtfs_route_name, :string) | ||
|
||
field(:gtfs_route_pattern_time_description, :string) | ||
|
||
# To be added: gtfs_route_pattern_shape (separate table) | ||
|
||
has_many(:detours, Detour) | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(route_pattern, attrs \\ %{}) do | ||
route_pattern | ||
|> cast(attrs, @required_fields, [:gtfs_route_pattern_time_description]) | ||
|> validate_required(@required_fields) | ||
|> unique_constraint([:hash], name: :route_patterns_unique_index) | ||
end | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. based on our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh rats. I didn't realize direction could be so varied in gtfs. I do believe we as an agency embrace only the Inbound / Outbound, so I could confirm that with TD. On the other hand, I had a related dilemma. I just realized that we need to be able to repopulate this field in the state machine: I'll need to adjust the db schema accordingly. Maybe store the map and the direction id instead of the direction name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need to store that info on a That's a good point though that we'll want to somehow gate the "route pattern picker" part based on some criteria, like if the route they're viewing is no longer current. A detour shouldn't be saved to the database in this state though. So we should be able to discard this info and make the state machine refetch this info. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah that's right! Let me think about this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok had to doublecheck:
Unfortunately, no, because of this line in DiversionPage: which used at every stage of the detour-creation workflow. So that will need to be refactored a bit on the frontend, which will necessitate some more tweaks on the backend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't spend too much time on it if it's tricky, but I think you could reference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And if you need to create new type aliases that'd be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gottttttt it, ok. I'm having a little trouble, but expect to figure it out. But in the meantime, follow-up question: does enforcing this schema on input also enforce it on retrieval? (That might impact whether I try to get through the trickiness or just use strings.) Also do we have any examples of using custom types in the field of a typed_schema? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we have an example, but I'm picturing something like this, I've not tested it though and can't confirm it works as is. field(:gtfs_route_pattern_id, :string) :: Schedule.Gtfs.RoutePattern.id() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh I see, thank you. I'll test it now. The Edit: Yes, that works! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Skate.Detours.DirectionName do | ||
@moduledoc false | ||
|
||
use Ecto.Type | ||
|
||
@type t :: String.t() | ||
|
||
@valid_directions ["Inbound", "Outbound"] | ||
|
||
@impl true | ||
def type, do: :string | ||
|
||
@impl true | ||
def cast(direction), do: allow_valid_direction(direction) | ||
|
||
@impl true | ||
def load(direction), do: allow_valid_direction(direction) | ||
|
||
@impl true | ||
def dump(direction), do: allow_valid_direction(direction) | ||
|
||
defp allow_valid_direction(direction) do | ||
if direction in @valid_directions do | ||
{:ok, direction} | ||
else | ||
:error | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Skate.Repo.Migrations.CreateRoutePatternsTable do | ||
use Ecto.Migration | ||
|
||
def change do | ||
execute("CREATE TYPE direction_name AS ENUM ('Inbound', 'Outbound')") | ||
|
||
create table(:route_patterns) do | ||
add :hash, :integer | ||
add :gtfs_route_pattern_id, :string | ||
add :gtfs_route_pattern_name, :string | ||
add :gtfs_route_pattern_headsign, :string | ||
add :gtfs_route_pattern_direction_name, :direction_name | ||
add :gtfs_route_id, :string | ||
add :gtfs_route_name, :string | ||
add :gtfs_route_pattern_time_description, :string | ||
timestamps() | ||
end | ||
|
||
create( | ||
unique_index( | ||
:route_patterns, | ||
[:hash], | ||
name: :route_patterns_unique_index | ||
) | ||
) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Skate.Repo.Migrations.DetourHasOneRoutePattern do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:detours) do | ||
add :route_pattern_id, references(:route_patterns) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know this existed! I think we should set
writable: :insert
on these fields so we don't accidentally update them