diff --git a/db/migrate/20221129104327_create_api_tokens.rb b/db/migrate/20221129104327_create_api_tokens.rb new file mode 100644 index 000000000..836963063 --- /dev/null +++ b/db/migrate/20221129104327_create_api_tokens.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ef138ebf4..4234f4f8c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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 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" 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| t.datetime "created_at", precision: 6, 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_procedures", "administrateurs" 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", "groupe_instructeurs" add_foreign_key "assign_tos", "groupe_instructeurs" diff --git a/lib/tasks/deployment/20221129162903_migrate_api_tokens.rake b/lib/tasks/deployment/20221129162903_migrate_api_tokens.rake new file mode 100644 index 000000000..d0bb72c1b --- /dev/null +++ b/lib/tasks/deployment/20221129162903_migrate_api_tokens.rake @@ -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