From d8bbb8cb08cecb5d192221d6174a94fb83b484a2 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 5 Dec 2017 16:37:58 +0100 Subject: [PATCH] Add timestamps on Entreprises --- ...1205151128_add_timestamps_to_entreprise.rb | 6 ++++++ db/schema.rb | 6 ++++-- ...initialize_timestamps_for_entreprises.rake | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20171205151128_add_timestamps_to_entreprise.rb create mode 100644 lib/tasks/2017_12_05_initialize_timestamps_for_entreprises.rake diff --git a/db/migrate/20171205151128_add_timestamps_to_entreprise.rb b/db/migrate/20171205151128_add_timestamps_to_entreprise.rb new file mode 100644 index 000000000..d4362cb58 --- /dev/null +++ b/db/migrate/20171205151128_add_timestamps_to_entreprise.rb @@ -0,0 +1,6 @@ +class AddTimestampsToEntreprise < ActiveRecord::Migration[5.0] + def change + add_column :entreprises, :created_at, :datetime + add_column :entreprises, :updated_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 5088f6f89..b7070879b 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: 20171123125346) do +ActiveRecord::Schema.define(version: 20171205151128) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -224,6 +224,8 @@ ActiveRecord::Schema.define(version: 20171123125346) do t.string "nom" t.string "prenom" t.integer "dossier_id" + t.datetime "created_at" + t.datetime "updated_at" t.index ["dossier_id"], name: "index_entreprises_on_dossier_id", using: :btree end @@ -419,8 +421,8 @@ ActiveRecord::Schema.define(version: 20171123125346) do t.boolean "for_individual", default: false t.boolean "individual_with_siret", default: false t.date "auto_archive_on" - t.datetime "hidden_at" t.datetime "published_at" + t.datetime "hidden_at" t.datetime "archived_at" t.index ["hidden_at"], name: "index_procedures_on_hidden_at", using: :btree end diff --git a/lib/tasks/2017_12_05_initialize_timestamps_for_entreprises.rake b/lib/tasks/2017_12_05_initialize_timestamps_for_entreprises.rake new file mode 100644 index 000000000..2928c6635 --- /dev/null +++ b/lib/tasks/2017_12_05_initialize_timestamps_for_entreprises.rake @@ -0,0 +1,19 @@ +namespace :'2017_12_05_initialize_timestamps_for_entreprises' do + task set: :environment do + entreprises = Entreprise.where(created_at: nil).includes(:dossier) + + puts "#{entreprises.count} to initialize..." + + entreprises.each { |e| initialize_entreprise(e) } + end + + def initialize_entreprise(entreprise) + puts "initializing entreprise #{entreprise.id}" + if entreprise.dossier.present? + entreprise.update_columns(created_at: entreprise.dossier.created_at, updated_at: entreprise.dossier.created_at) + else + puts "dossier #{entreprise.dossier_id} is missing for entreprise #{entreprise.id}" + entreprise.update_columns(created_at: DateTime.now, updated_at: DateTime.now) + end + end +end