Skip to content

Commit dfaf55e

Browse files
committed
New migration to hash tokens
1 parent 9c9ede6 commit dfaf55e

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class HashAccessTokenValues < ActiveRecord::Migration[7.1]
2+
disable_ddl_transaction! if System::Database.postgres?
3+
4+
BATCH_SIZE = 1000
5+
DIGEST_PREFIX = 'SHA384$'.freeze
6+
7+
def up
8+
say "Hashing legacy access token values..."
9+
10+
loop do
11+
rows_updated = exec_update(batch_update_sql)
12+
break if rows_updated == 0
13+
14+
sleep(0.1)
15+
end
16+
17+
say "Done."
18+
end
19+
20+
private
21+
22+
def batch_update_sql
23+
if System::Database.mysql?
24+
"UPDATE access_tokens SET value = CONCAT('#{DIGEST_PREFIX}', SHA2(value, 384)) " \
25+
"WHERE value NOT LIKE '#{DIGEST_PREFIX}%' LIMIT #{BATCH_SIZE}"
26+
elsif System::Database.postgres?
27+
"UPDATE access_tokens SET value = '#{DIGEST_PREFIX}' || encode(sha384(value::bytea), 'hex') " \
28+
"WHERE id IN (SELECT id FROM access_tokens WHERE value NOT LIKE '#{DIGEST_PREFIX}%' LIMIT #{BATCH_SIZE})"
29+
elsif System::Database.oracle?
30+
"UPDATE access_tokens SET value = '#{DIGEST_PREFIX}' || LOWER(STANDARD_HASH(value, 'SHA384')) " \
31+
"WHERE ROWID IN (SELECT ROWID FROM access_tokens WHERE value NOT LIKE '#{DIGEST_PREFIX}%' AND ROWNUM <= #{BATCH_SIZE})"
32+
end
33+
end
34+
35+
def down
36+
raise ActiveRecord::IrreversibleMigration
37+
end
38+
end

db/oracle_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2025_05_22_195407) do
13+
ActiveRecord::Schema[7.1].define(version: 2026_03_10_134934) do
1414
create_table "access_tokens", force: :cascade do |t|
1515
t.integer "owner_id", precision: 38, null: false
1616
t.text "scopes"

db/postgres_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2025_05_22_195407) do
13+
ActiveRecord::Schema[7.1].define(version: 2026_03_10_134934) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2025_05_22_195407) do
13+
ActiveRecord::Schema[7.1].define(version: 2026_03_10_134934) do
1414
create_table "access_tokens", charset: "utf8mb3", collation: "utf8mb3_bin", force: :cascade do |t|
1515
t.bigint "owner_id", null: false
1616
t.text "scopes"

0 commit comments

Comments
 (0)