Merge pull request #6085 from betagouv/fill_missing_date_of_france_connect_informations

[#6084] rempli des dates manquantes dans france_connect_informations
This commit is contained in:
LeSim 2021-04-13 10:08:18 +02:00 committed by GitHub
commit eb8d5b711f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 124 additions and 1 deletions

View file

@ -5,6 +5,7 @@
# id :integer not null, primary key
# birthdate :date
# birthplace :string
# data :jsonb
# email_france_connect :string
# family_name :string
# gender :string

View file

@ -0,0 +1,5 @@
class AddDataColumnToFranceConnectInformations < ActiveRecord::Migration[6.1]
def change
add_column :france_connect_informations, :data, :jsonb
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_04_09_130604) do
ActiveRecord::Schema.define(version: 2021_04_12_092710) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -410,6 +410,7 @@ ActiveRecord::Schema.define(version: 2021_04_09_130604) do
t.string "email_france_connect"
t.datetime "created_at"
t.datetime "updated_at"
t.jsonb "data"
t.index ["user_id"], name: "index_france_connect_informations_on_user_id"
end

View file

@ -0,0 +1,39 @@
namespace :after_party do
desc 'Deployment task: fill_missing_date_of_fc'
task fill_missing_date_of_fc: :environment do
rake_puts "Remove invalid FranceConnectInformation records with no associated user…"
FranceConnectInformation
.where(user_id: nil)
.destroy_all
rake_puts "Fill-in missing created_at from updated_at column on FranceConnectInformation records…"
created_from_updated_sql = <<~EOF
created_at = updated_at,
data = '{ "note": "missing created_at has been copied from updated_at" }'
EOF
FranceConnectInformation
.where(created_at: nil)
.where.not(updated_at: nil)
.update_all(created_from_updated_sql)
rake_puts "Fill-in missing created_at, updated_at columns from users.created on FranceConnectInformation records…"
created_updated_from_user_created_sql = <<~EOF
UPDATE france_connect_informations
SET created_at = users.created_at,
updated_at = users.created_at,
data = '{ "note": "missing created_at, updated_at have been copied from users.created_at" }'
FROM users
WHERE users.id = france_connect_informations.user_id
AND france_connect_informations.created_at IS NULL
AND france_connect_informations.updated_at IS NULL
EOF
FranceConnectInformation
.connection
.execute(created_updated_from_user_created_sql)
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -0,0 +1,77 @@
describe '20210412093054_fill_missing_date_of_fc' do
let(:rake_task) { Rake::Task['after_party:fill_missing_date_of_fc'] }
let!(:user) { create(:user, created_at: Time.zone.parse('2000/01/01')) }
let!(:valid_fci) do
FranceConnectInformation.create!(
user: user,
france_connect_particulier_id: '123',
created_at: Time.zone.parse('2010/01/01'),
updated_at: Time.zone.parse('2012/01/01')
)
end
let!(:missing_created_fci) do
fci = FranceConnectInformation.create!(
user: user,
france_connect_particulier_id: '123',
updated_at: Time.zone.parse('2013/01/01')
)
fci.update_column('created_at', nil)
fci
end
let!(:missing_created_updated_fci) do
fci = FranceConnectInformation.create!(
user: user,
france_connect_particulier_id: '123'
)
fci.update_column('created_at', nil)
fci.update_column('updated_at', nil)
fci
end
let!(:missing_created_updated_without_user_fci) do
fci = FranceConnectInformation.create!(
france_connect_particulier_id: '123'
)
fci.update_column('created_at', nil)
fci.update_column('updated_at', nil)
fci
end
before do
rake_task.invoke
end
after { rake_task.reenable }
it "does not change valid fci" do
valid_fci.reload
expect(valid_fci.created_at).to eq(Time.zone.parse('2010/01/01'))
expect(valid_fci.updated_at).to eq(Time.zone.parse('2012/01/01'))
expect(valid_fci.data).to be_nil
end
it "fills missing created from updated" do
missing_created_fci.reload
expect(missing_created_fci.created_at).to eq(Time.zone.parse('2013/01/01'))
expect(missing_created_fci.data['note']).to eq("missing created_at has been copied from updated_at")
end
it "fills missing created, updated from users created" do
missing_created_updated_fci.reload
expect(missing_created_updated_fci.created_at).to eq(Time.zone.parse('2000/01/01'))
expect(missing_created_updated_fci.updated_at).to eq(Time.zone.parse('2000/01/01'))
expect(missing_created_updated_fci.data['note']).to eq("missing created_at, updated_at have been copied from users.created_at")
end
it "destroys fci when there is no user" do
expect { missing_created_updated_without_user_fci.reload }
.to raise_error(ActiveRecord::RecordNotFound)
end
end