feat(api_token): add api_tokens table
This commit is contained in:
parent
040009f2c9
commit
47f716f9fa
3 changed files with 53 additions and 1 deletions
11
db/migrate/20221129104327_create_api_tokens.rb
Normal file
11
db/migrate/20221129104327_create_api_tokens.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class CreateAPITokens < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
create_table :api_tokens, id: :uuid do |t|
|
||||||
|
t.references :administrateur, null: false, foreign_key: true
|
||||||
|
t.string :encrypted_token, null: false
|
||||||
|
t.string :name, null: false
|
||||||
|
t.integer :version, null: false, default: 3
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
13
db/schema.rb
13
db/schema.rb
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2022_12_01_091658) do
|
ActiveRecord::Schema.define(version: 2022_12_05_144624) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pgcrypto"
|
enable_extension "pgcrypto"
|
||||||
|
@ -86,6 +86,16 @@ ActiveRecord::Schema.define(version: 2022_12_01_091658) do
|
||||||
t.index ["procedure_id"], name: "index_administrateurs_procedures_on_procedure_id"
|
t.index ["procedure_id"], name: "index_administrateurs_procedures_on_procedure_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "api_tokens", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||||
|
t.bigint "administrateur_id", null: false
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.string "encrypted_token", null: false
|
||||||
|
t.string "name", null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.integer "version", default: 3, null: false
|
||||||
|
t.index ["administrateur_id"], name: "index_api_tokens_on_administrateur_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "archives", force: :cascade do |t|
|
create_table "archives", force: :cascade do |t|
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.string "job_status", null: false
|
t.string "job_status", null: false
|
||||||
|
@ -928,6 +938,7 @@ ActiveRecord::Schema.define(version: 2022_12_01_091658) do
|
||||||
add_foreign_key "administrateurs_instructeurs", "instructeurs"
|
add_foreign_key "administrateurs_instructeurs", "instructeurs"
|
||||||
add_foreign_key "administrateurs_procedures", "administrateurs"
|
add_foreign_key "administrateurs_procedures", "administrateurs"
|
||||||
add_foreign_key "administrateurs_procedures", "procedures"
|
add_foreign_key "administrateurs_procedures", "procedures"
|
||||||
|
add_foreign_key "api_tokens", "administrateurs"
|
||||||
add_foreign_key "archives_groupe_instructeurs", "archives"
|
add_foreign_key "archives_groupe_instructeurs", "archives"
|
||||||
add_foreign_key "archives_groupe_instructeurs", "groupe_instructeurs"
|
add_foreign_key "archives_groupe_instructeurs", "groupe_instructeurs"
|
||||||
add_foreign_key "assign_tos", "groupe_instructeurs"
|
add_foreign_key "assign_tos", "groupe_instructeurs"
|
||||||
|
|
30
lib/tasks/deployment/20221129162903_migrate_api_tokens.rake
Normal file
30
lib/tasks/deployment/20221129162903_migrate_api_tokens.rake
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
namespace :after_party do
|
||||||
|
desc 'Deployment task: migrate_api_tokens'
|
||||||
|
task migrate_api_tokens: :environment do
|
||||||
|
puts "Running deploy task 'migrate_api_tokens'"
|
||||||
|
|
||||||
|
administrateurs = Administrateur
|
||||||
|
.where.not(encrypted_token: nil)
|
||||||
|
.where.missing(:api_tokens)
|
||||||
|
|
||||||
|
progress = ProgressReport.new(administrateurs.count)
|
||||||
|
|
||||||
|
administrateurs.find_each do |administrateur|
|
||||||
|
administrateur.transaction do
|
||||||
|
administrateur
|
||||||
|
.api_tokens
|
||||||
|
.create!(name: administrateur.updated_at.strftime('Jeton d’API généré le %d/%m/%Y'),
|
||||||
|
encrypted_token: administrateur.encrypted_token,
|
||||||
|
version: 1)
|
||||||
|
administrateur.update_column(:encrypted_token, nil)
|
||||||
|
end
|
||||||
|
progress.inc
|
||||||
|
end
|
||||||
|
progress.finish
|
||||||
|
|
||||||
|
# Update task as completed. If you remove the line below, the task will
|
||||||
|
# run with every deploy (or every time you call after_party:run).
|
||||||
|
AfterParty::TaskRecord
|
||||||
|
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue