Skip to content

Create UserAchievement model

What does this MR do and why?

Related to #385380 (closed)

This MR introduces the linking table used to award achievements to users.

Migrations

lee@Lees-MBP gitlab % bundle exec rake db:migrate:up VERSION=20221214201256 RAILS_ENV=development           
main: == 20221214201256 CreateUserAchievements: migrating ===========================
main: -- create_table(:user_achievements, {})
main:    -> 0.0109s
main: == 20221214201256 CreateUserAchievements: migrated (0.0120s) ==================

lee@Lees-MBP gitlab % bundle exec rake db:migrate:up VERSION=20221214204247 RAILS_ENV=development
main: == 20221214204247 UserAchievementsForeignKeys: migrating ======================
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:user_achievements)
main:    -> 0.1202s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE user_achievements\nADD CONSTRAINT fk_4efde02858\nFOREIGN KEY (user_id)\nREFERENCES users (id)\nON DELETE CASCADE\nNOT VALID;\n")
main:    -> 0.0017s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0005s
main: -- execute("ALTER TABLE user_achievements VALIDATE CONSTRAINT fk_4efde02858;")
main:    -> 0.0030s
main: -- execute("RESET statement_timeout")
main:    -> 0.0007s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:user_achievements)
main:    -> 0.0020s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE user_achievements\nADD CONSTRAINT fk_60b12fcda3\nFOREIGN KEY (awarded_by_user_id)\nREFERENCES users (id)\nON DELETE SET NULL\nNOT VALID;\n")
main:    -> 0.0015s
main: -- execute("ALTER TABLE user_achievements VALIDATE CONSTRAINT fk_60b12fcda3;")
main:    -> 0.0009s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:user_achievements)
main:    -> 0.0019s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE user_achievements\nADD CONSTRAINT fk_d7653ef780\nFOREIGN KEY (revoked_by_user_id)\nREFERENCES users (id)\nON DELETE SET NULL\nNOT VALID;\n")
main:    -> 0.0007s
main: -- execute("ALTER TABLE user_achievements VALIDATE CONSTRAINT fk_d7653ef780;")
main:    -> 0.0009s
main: == 20221214204247 UserAchievementsForeignKeys: migrated (0.1533s) =============

lee@Lees-MBP gitlab % bundle exec rake db:migrate:down VERSION=20221214204247 RAILS_ENV=development
main: == 20221214204247 UserAchievementsForeignKeys: reverting ======================
main: -- transaction_open?()
main:    -> 0.0007s
main: -- remove_foreign_key(:user_achievements, {:column=>:user_id})
main:    -> 0.1454s
main: -- remove_foreign_key(:user_achievements, {:column=>:awarded_by_user_id})
main:    -> 0.0033s
main: -- remove_foreign_key(:user_achievements, {:column=>:revoked_by_user_id})
main:    -> 0.0035s
main: == 20221214204247 UserAchievementsForeignKeys: reverted (0.1832s) =============

lee@Lees-MBP gitlab % bundle exec rake db:migrate:down VERSION=20221214201256 RAILS_ENV=development
main: == 20221214201256 CreateUserAchievements: reverting ===========================
main: -- drop_table(:user_achievements)
main:    -> 0.0021s
main: == 20221214201256 CreateUserAchievements: reverted (0.0031s) ==================

How to set up and validate locally

Achievements are not exposed via API/frontend etc yet- the only way to validate is via the rails console. You could do something like:

a = Achievements::Achievement.new
a.namespace_id = 31
a.name = "Test"
a.save

ua = Achievements::UserAchievement.new
ua.achievement_id = 1
ua.user_id = 1
ua.awarded_by = 2
ua.revoked_by = 3
ua.revoked_at = DateTime.current
ua.save

a.users
ua.user
ua.achievement
Users.find(1).achievements
Users.find(1).user_achievements
Users.find(2).awarded_user_achievements
Users.find(3).revoked_userachievements

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Lee Tickett

Merge request reports

Loading