-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Saved segments/create table #4797
Conversation
def change do | ||
create table(:segments) do | ||
add :name, :string, null: false | ||
add :personal, :boolean, default: true, null: false |
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.
Q: Should this be an enum of sorts? Are we certain this will remain 2-option list?
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.
For the first iteration, I think we'll have 2 cases. If we end up needing more cases, we can always migrate to a "type" column down the road.
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.
These migrations are generally a pain and so is dealing with booleans in code. I suggest switching to an enum: https://hexdocs.pm/ecto/Ecto.Enum.html - even in the 2-option case this will IMO result in cleaner code.
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.
Can you give an example of a potential issue with dealing booleans in code? As for cleaner code, I'm not so sure about that. Atoms are convenient in the backend, but using enums in the FE can be quite verbose.
import SegmentType from '../../segments.ts'
// ...
if (segment.type === SegmentType.personal) {
// ...
}
vs
if (segment.personal) {
// ...
}
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 prefer the first option. Elixir sample as well:
handle(segment.personal, segment.segment_data)
def handle(true, segment_data), do: ...
def handle(false, segment_data), do: ...
vs
handle(segment.type, segment.segment_data)
def handle(:personal, segment_data), do: ...
def handle(:team, segment_data), do: ...
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.
Hmm, it's not something that I'm currently branching functions on (see here: https://github.com/plausible/analytics/tree/saved-segments-spec/). The branching is happening in Ecto queries.
As for the example you gave, I wonder whether a better spec would actually be not to split the segment to pieces in function arguments. That seems to have been a successful strategy with Plausible.Query.
@spec handle(Plausible.Segment.t()) :: :ok
def handle(%Plausible.Segment{personal: true} = segment) do end
def handle(%Plausible.Segment{personal: false} = segment) do end
* Add migration for Saved Segments * Remove premature optimisation * Format * Refactor to explicit segment type
Changes
Creates the saved segments table.
Tests
Changelog
Documentation
Dark mode