Skip to content
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

Add foreign key constraint and relationship checks from timecard_entry to eventdate #529

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NoRePercussions
Copy link
Contributor

@NoRePercussions NoRePercussions commented Nov 13, 2023

Prevent inconsistency when eventdates are deleted by enforcing that timecard entries must reference a valid eventdate.

This commit alone does not update the UI in response to this change. At minimum, the delete eventdate button should be disabled if there is at least one timecard.

…y to eventdate

Prevent inconsistency when eventdates are deleted by enforcing that timecard entries must reference a valid eventdate
@NoRePercussions NoRePercussions marked this pull request as ready for review November 13, 2023 20:58
@@ -0,0 +1,10 @@
class AddEventdateFkToTimecards < ActiveRecord::Migration[6.1]
def up
change_column :timecard_entries, :eventdate_id, :bigint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing this to a bigint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eventdate ID column is a bigint

belongs_to :timecard

extend Enumerize
enumerize :eventpart, in: ["call", "show", "strike"], default: "show"

validates_presence_of :member_id, :eventdate_id, :hours, :eventpart
validates_presence_of :eventdate, :member_id, :eventdate_id, :hours, :eventpart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should remove :eventdate_id if adding :eventdate.

app/models/timecard_entry.rb Show resolved Hide resolved
@NoRePercussions NoRePercussions force-pushed the bugs/timecard-referential-integrity branch from fcc589c to 7c8f252 Compare November 14, 2023 18:39
Add can_destroy? helper method, orm-level checks before
the eventdate is destroyed, and a validator to event so
trying to destroy an eventdate that has been billed for
raises an error.
@NoRePercussions NoRePercussions force-pushed the bugs/timecard-referential-integrity branch from 7c8f252 to b562178 Compare November 14, 2023 18:41
@NoRePercussions
Copy link
Contributor Author

Form-level validation is fixed. My theory is that the destroyed eventdates are being removed from the event before they are validated, thus no form errors. I added another validator to event that checks each eventdate that will be destroyed.

@DaAwesomeP
Copy link
Member

I am hesitant to go forward with this. If these constraints have not been implemented for so long then there is a strong chance that adding them will expose some missing data in Tracker. I was having lot of issues with these commits: 1a918f5 and 60a3d3d. I ultimately had to disable the check in prod because looping over the events in the migration would fail due to validations created after those events: a025884

I think that this and some of your other pulls warrant a return of a full dev instance of Tracker (i.e. copy the prod DB to a dev instance).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants