Skip to content

Add timelog_category_id to timelogs

What does this MR do and why?

Add timelog_category_id to timelogs.

We introduced TimelogCategory a while back, but now need to add the relationship to Timelog.

This will allow us to attach a TimelogCategory to a Timelog (through rails).

An MR will follow to extend the /spend quick action to allow passing a category.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Database migrations

bundle exec rake db:migrate:up:main VERSION=20240104124057 RAILS_ENV=development

main: == [advisory_lock_connection] object_id: 182480, pg_backend_pid: 139138
main: == 20240104124057 AddCategoryToTimelogs: migrating ============================
main: -- add_column(:timelogs, :timelog_category_id, :bigint)
main:    -> 0.0021s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0202s
main: -- index_exists?(:timelogs, :timelog_category_id, {:algorithm=>:concurrently})
main:    -> 0.0062s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0005s
main: -- add_index(:timelogs, :timelog_category_id, {:algorithm=>:concurrently})
main:    -> 0.0022s
main: -- execute("RESET statement_timeout")
main:    -> 0.0003s
main: -- index_name(:timelogs, :timelog_category_id)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("ALTER TABLE timelogs ADD CONSTRAINT fk_f12ef8db70 FOREIGN KEY (timelog_category_id) REFERENCES timelog_categories (id) ON DELETE SET NULL NOT VALID;")
main:    -> 0.0012s
main: -- execute("ALTER TABLE timelogs VALIDATE CONSTRAINT fk_f12ef8db70;")
main:    -> 0.0017s
main: == 20240104124057 AddCategoryToTimelogs: migrated (0.0769s) ===================

bundle exec rake db:migrate:down:main VERSION=20240104124057 RAILS_ENV=development

main: == [advisory_lock_connection] object_id: 182560, pg_backend_pid: 138638
main: == 20240104124057 AddCategoryToTimelogs: reverting ============================
main: -- remove_column(:timelogs, :timelog_category_id)
main:    -> 0.0034s
main: == 20240104124057 AddCategoryToTimelogs: reverted (0.0073s) ===================

image

image

How to set up and validate locally

t = Timelog.last
c = TimeTracking::TimelogCategory.new
c.namespace_id = t.project.root_namespace.id
c.name = 'bob'
c.created_at = Date.today
c.updated_at = Date.today
c.save
t.timelog_category_id = c.id
t.save

[22] pry(main)> t.timelog_category
  TimeTracking::TimelogCategory Load (1.4ms)  SELECT "timelog_categories".* FROM "timelog_categories" WHERE "timelog_categories"."id" = 1 LIMIT 1 /*application:console,db_config_name:main,console_hostname:debian,console_username:debian,line:(pry):22:in `__pry__'*/
=> #<TimeTracking::TimelogCategory:0x0000ffff7ab0b1f0
 id: 1,
 namespace_id: 33,
 created_at: Thu, 04 Jan 2024 00:00:00.000000000 UTC +00:00,
 updated_at: Thu, 04 Jan 2024 15:14:09.893148000 UTC +00:00,
 billing_rate: 0.0,
 billable: false,
 name: "bob",
 description: nil,
 color: #<Gitlab::Color:0x0000ffff7a94a578 @value="#6699cc">>
[23] pry(main)> c.timelogs
  Timelog Load (1.0ms)  SELECT "timelogs".* FROM "timelogs" WHERE "timelogs"."timelog_category_id" = 1 /*application:console,db_config_name:main,console_hostname:debian,console_username:debian,line:bin/rails:4:in `<main>'*/
=> [#<Timelog:0x0000ffff7a8c5c10
  id: 8,
  time_spent: 14400,
  user_id: 1,
  created_at: Thu, 04 Jan 2024 12:38:42.481354000 UTC +00:00,
  updated_at: Thu, 04 Jan 2024 15:14:26.213568000 UTC +00:00,
  issue_id: 498,
  merge_request_id: nil,
  spent_at: Tue, 02 Jan 2024 00:00:00.000000000 UTC +00:00,
  project_id: 7,
  summary: nil,
  note_id: nil,
  timelog_category_id: 1>]
Edited by Lee Tickett

Merge request reports