Skip to content

Commit

Permalink
Rely on default index names
Browse files Browse the repository at this point in the history
This makes it automatically work for separate table per account type use
case.

While here, we also remove the duplicate index on JWT refresh keys
table, as `t.references` already creates an index by default.

We also add missing foreign keys in tests.
  • Loading branch information
janko committed Jun 25, 2024
1 parent 6c929fe commit f32020c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## HEAD

* Remove duplicate index on `account_id` in JWT refresh keys table (@janko)

* Rely on default index names in generated migrations (@thedumtechguy)

* Avoid generating Active Record specific `convert_token_id_to_integer?` when Sequel is used

## 1.14.1 (2024-05-15)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ create_table :<%= table_prefix %>_authentication_audit_logs<%= primary_key_type
<% else -%>
t.string :metadata
<% end -%>
t.index [:<%= table_prefix %>_id, :at], name: "audit_<%= table_prefix %>_at_idx"
t.index [:<%= table_prefix %>_id, :at]
t.index :at
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ create_table :<%= table_prefix %>_jwt_refresh_keys<%= primary_key_type %> do |t|
t.references :<%= table_prefix %>, foreign_key: true, null: false<%= primary_key_type(:type) %>
t.string :key, null: false
t.datetime :deadline, null: false
t.index :<%= table_prefix %>_id, name: "<%= table_prefix %>_jwt_rk_<%= table_prefix %>_id_idx"
end
2 changes: 1 addition & 1 deletion lib/generators/rodauth/migration/sequel/audit_logging.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ create_table :<%= table_prefix %>_authentication_audit_logs do
<% else -%>
String :metadata
<% end -%>
index [:<%= table_prefix %>_id, :at], name: :audit_<%= table_prefix %>_at_idx
index [:<%= table_prefix %>_id, :at]
index :at
end
2 changes: 1 addition & 1 deletion lib/generators/rodauth/migration/sequel/jwt_refresh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ create_table :<%= table_prefix %>_jwt_refresh_keys do
foreign_key :<%= table_prefix %>_id, :<%= table_prefix.pluralize %>, null: false, type: :Bignum
String :key, null: false
DateTime :deadline, null: false
index :<%= table_prefix %>_id, name: :<%= table_prefix %>_jwt_rk_<%= table_prefix %>_id_idx
index :<%= table_prefix %>_id
end
15 changes: 7 additions & 8 deletions test/rails_app/db/migrate/20200411171322_create_rodauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,28 @@ def change

# Used by the audit logging feature
create_table :account_authentication_audit_logs do |t|
t.references :account, null: false
t.references :account, foreign_key: true, null: false
t.datetime :at, null: false, default: -> { "CURRENT_TIMESTAMP" }
t.text :message, null: false
if ActiveRecord.version >= Gem::Version.new("5.2")
t.json :metadata
else
t.text :metadata
end
t.index [:account_id, :at], name: "audit_account_at_idx"
t.index :at, name: "audit_at_idx"
t.index [:account_id, :at]
t.index :at
end

# Used by the jwt refresh feature
create_table :account_jwt_refresh_keys do |t|
t.references :account, null: false
t.references :account, foreign_key: true, null: false
t.string :key, null: false
t.datetime :deadline, null: false
t.index :account_id, name: "account_jwt_rk_account_id_idx"
end

# Used by the disallow_password_reuse feature
create_table :account_previous_password_hashes do |t|
t.references :account
t.references :account, foreign_key: true
t.string :password_hash, null: false
end

Expand Down Expand Up @@ -117,7 +116,7 @@ def change

# Used by the active sessions feature
create_table :account_active_session_keys, primary_key: [:account_id, :session_id] do |t|
t.references :account
t.references :account, foreign_key: true
t.string :session_id
t.datetime :created_at, null: false, default: -> { "CURRENT_TIMESTAMP" }
t.datetime :last_use, null: false, default: -> { "CURRENT_TIMESTAMP" }
Expand All @@ -129,7 +128,7 @@ def change
t.string :webauthn_id, null: false
end
create_table :account_webauthn_keys, primary_key: [:account_id, :webauthn_id] do |t|
t.references :account
t.references :account, foreign_key: true
t.string :webauthn_id
t.string :public_key, null: false
t.integer :sign_count, null: false
Expand Down

0 comments on commit f32020c

Please sign in to comment.