Class: AddWebauthnIdToUsers

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/ruby_cms/templates/db/migrate/20260605151936_add_webauthn_id_to_users.rb

Instance Method Summary collapse

Instance Method Details

#downObject



18
19
20
21
# File 'lib/generators/ruby_cms/templates/db/migrate/20260605151936_add_webauthn_id_to_users.rb', line 18

def down
  remove_index :users, :webauthn_id, if_exists: true
  remove_column :users, :webauthn_id, if_exists: true
end

#upObject

Add nullable, backfill every existing user with a stable handle, THEN enforce NOT NULL + unique. WebAuthn requires every user to have a permanent id.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/generators/ruby_cms/templates/db/migrate/20260605151936_add_webauthn_id_to_users.rb', line 4

def up
  return if column_exists?(:users, :webauthn_id)

  add_column :users, :webauthn_id, :string

  User.reset_column_information
  User.where(webauthn_id: nil).find_each do |user|
    user.update_columns(webauthn_id: SecureRandom.base64(32))
  end

  change_column_null :users, :webauthn_id, false
  add_index :users, :webauthn_id, unique: true, if_not_exists: true
end