mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-26 20:54:21 +01:00
migrate from Invoice.user_id to Invoice.invoicing_profile_id
This commit is contained in:
parent
ca4d73bb6a
commit
0df97cffc5
@ -1,23 +1,97 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# migrate the invoices from being attached to a user to invoicing_profiles which are GDPR compliant
|
||||
class MigrateUserToInvoicingProfile < ActiveRecord::Migration
|
||||
def up
|
||||
# first, check the footprints
|
||||
puts 'Checking all invoices footprints. This may take a while...'
|
||||
Invoice.where.not(footprint: nil).order(:created_at).all.each do |i|
|
||||
raise "Invalid footprint for invoice #{i.id}" unless i.check_footprint
|
||||
end
|
||||
check_footprints
|
||||
|
||||
# if everything is ok, proceed with migration
|
||||
Invoice.order(:created_at).all.each do |i|
|
||||
# remove and save periods in memory
|
||||
periods = backup_and_remove_periods
|
||||
# migrate invoices
|
||||
puts 'Migrating invoices. This may take a while...'
|
||||
Invoice.order(:id).all.each do |i|
|
||||
i.update_column('invoicing_profile_id', i.user.invoicing_profile.id)
|
||||
i.update_column('user_id', nil)
|
||||
i.chain_record
|
||||
end
|
||||
# chain all records
|
||||
InvoiceItem.order(:id).all.each(&:chain_record)
|
||||
Invoice.order(:id).all.each(&:chain_record)
|
||||
# write memory dump into database
|
||||
restore_periods(periods)
|
||||
end
|
||||
|
||||
def down
|
||||
# here we don't check footprints to save processing time and because this is pointless when reverting the migrations
|
||||
|
||||
# remove and save periods in memory
|
||||
periods = backup_and_remove_periods
|
||||
# reset invoices
|
||||
Invoice.order(:created_at).all.each do |i|
|
||||
i.update_column('user_id', i.invoicing_profile.user_id)
|
||||
i.update_column('invoicing_profile_id', nil)
|
||||
i.chain_record
|
||||
end
|
||||
# chain all records
|
||||
InvoiceItem.order(:id).all.each(&:chain_record)
|
||||
Invoice.order(:id).all.each(&:chain_record)
|
||||
# write memory dump into database
|
||||
restore_periods(periods)
|
||||
end
|
||||
|
||||
def check_footprints
|
||||
if AccountingPeriod.count.positive?
|
||||
last_period = AccountingPeriod.order(start_at: 'DESC').first
|
||||
puts "Checking invoices footprints from #{last_period.end_at}. This may take a while..."
|
||||
Invoice.where('created_at > ?', last_period.end_at).order(:id).each do |i|
|
||||
raise "Invalid footprint for invoice #{i.id}" unless i.check_footprint
|
||||
end
|
||||
else
|
||||
puts 'Checking all invoices footprints. This may take a while...'
|
||||
Invoice.order(:id).all.each do |i|
|
||||
raise "Invalid footprint for invoice #{i.id}" unless i.check_footprint
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# will return an array of hash containing the removed periods data
|
||||
def backup_and_remove_periods
|
||||
return unless AccountingPeriod.count.positive?
|
||||
|
||||
puts 'Removing accounting archives...'
|
||||
# 1. remove protection for AccountingPeriods
|
||||
execute("DROP RULE IF EXISTS accounting_periods_del_protect ON #{AccountingPeriod.arel_table.name};")
|
||||
# 2. backup AccountingPeriods in memory
|
||||
periods = []
|
||||
AccountingPeriod.all.each do |p|
|
||||
periods.push(
|
||||
start_at: p.start_at,
|
||||
end_at: p.end_at,
|
||||
closed_at: p.closed_at,
|
||||
closed_by: p.closed_by
|
||||
)
|
||||
end
|
||||
# 3. Delete periods from database
|
||||
AccountingPeriod.all.each do |ap|
|
||||
execute("DELETE FROM accounting_periods WHERE ID=#{ap.id};")
|
||||
end
|
||||
periods
|
||||
end
|
||||
|
||||
def restore_periods(periods)
|
||||
return unless periods.size.positive?
|
||||
|
||||
# 1. recreate AccountingPeriods
|
||||
puts 'Recreating accounting archives. This may take a while...'
|
||||
periods.each do |p|
|
||||
AccountingPeriod.create!(
|
||||
start_at: p[:start_at],
|
||||
end_at: p[:end_at],
|
||||
closed_at: p[:closed_at],
|
||||
closed_by: p[:closed_by]
|
||||
)
|
||||
end
|
||||
# 2. reset protection for AccountingPeriods
|
||||
execute("CREATE RULE accounting_periods_del_protect AS ON DELETE TO #{AccountingPeriod.arel_table.name} DO INSTEAD NOTHING;")
|
||||
end
|
||||
end
|
||||
|
5
db/migrate/20190528140012_remove_user_id_from_invoice.rb
Normal file
5
db/migrate/20190528140012_remove_user_id_from_invoice.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class RemoveUserIdFromInvoice < ActiveRecord::Migration
|
||||
def change
|
||||
remove_column :invoices, :user_id, :integer
|
||||
end
|
||||
end
|
25
db/schema.rb
25
db/schema.rb
@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20190320091148) do
|
||||
ActiveRecord::Schema.define(version: 20190528140012) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@ -266,7 +266,6 @@ ActiveRecord::Schema.define(version: 20190320091148) do
|
||||
t.integer "total"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.integer "user_id"
|
||||
t.string "reference", limit: 255
|
||||
t.string "avoir_mode", limit: 255
|
||||
t.datetime "avoir_date"
|
||||
@ -280,13 +279,24 @@ ActiveRecord::Schema.define(version: 20190320091148) do
|
||||
t.string "footprint"
|
||||
t.string "environment"
|
||||
t.integer "operator_id"
|
||||
t.integer "invoicing_profile_id"
|
||||
end
|
||||
|
||||
add_index "invoices", ["coupon_id"], name: "index_invoices_on_coupon_id", using: :btree
|
||||
add_index "invoices", ["invoice_id"], name: "index_invoices_on_invoice_id", using: :btree
|
||||
add_index "invoices", ["user_id"], name: "index_invoices_on_user_id", using: :btree
|
||||
add_index "invoices", ["invoicing_profile_id"], name: "index_invoices_on_invoicing_profile_id", using: :btree
|
||||
add_index "invoices", ["wallet_transaction_id"], name: "index_invoices_on_wallet_transaction_id", using: :btree
|
||||
|
||||
create_table "invoicing_profiles", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.string "first_name"
|
||||
t.string "last_name"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_index "invoicing_profiles", ["user_id"], name: "index_invoicing_profiles_on_user_id", using: :btree
|
||||
|
||||
create_table "licences", force: :cascade do |t|
|
||||
t.string "name", limit: 255, null: false
|
||||
t.text "description"
|
||||
@ -383,11 +393,13 @@ ActiveRecord::Schema.define(version: 20190320091148) do
|
||||
|
||||
create_table "organizations", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "profile_id"
|
||||
t.integer "invoicing_profile_id"
|
||||
end
|
||||
|
||||
add_index "organizations", ["invoicing_profile_id"], name: "index_organizations_on_invoicing_profile_id", using: :btree
|
||||
add_index "organizations", ["profile_id"], name: "index_organizations_on_profile_id", using: :btree
|
||||
|
||||
create_table "plans", force: :cascade do |t|
|
||||
@ -884,10 +896,13 @@ ActiveRecord::Schema.define(version: 20190320091148) do
|
||||
add_foreign_key "history_values", "settings"
|
||||
add_foreign_key "history_values", "users"
|
||||
add_foreign_key "invoices", "coupons"
|
||||
add_foreign_key "invoices", "invoicing_profiles"
|
||||
add_foreign_key "invoices", "users", column: "operator_id"
|
||||
add_foreign_key "invoices", "wallet_transactions"
|
||||
add_foreign_key "invoicing_profiles", "users"
|
||||
add_foreign_key "o_auth2_mappings", "o_auth2_providers"
|
||||
add_foreign_key "open_api_calls_count_tracings", "open_api_clients"
|
||||
add_foreign_key "organizations", "invoicing_profiles"
|
||||
add_foreign_key "organizations", "profiles"
|
||||
add_foreign_key "prices", "groups"
|
||||
add_foreign_key "prices", "plans"
|
||||
|
Loading…
x
Reference in New Issue
Block a user