From 3945b760b57ffb163a141fe4c090d3d66d6e9436 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 22 May 2019 17:49:22 +0200 Subject: [PATCH] [ongoing] attach invoices to invoicingProfile instead of user --- app/models/invoice.rb | 1 + app/models/invoicing_profile.rb | 1 + app/models/user.rb | 2 ++ ...0190521122429_create_invoicing_profiles.rb | 1 + ...09_migrate_profile_to_invoicing_profile.rb | 9 +++++--- ...15230_migrate_user_to_invoicing_profile.rb | 23 +++++++++++++++++++ db/schema.rb | 20 +++------------- 7 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 898a4f06d..23fd8d03b 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -13,6 +13,7 @@ class Invoice < ActiveRecord::Base has_many :invoice_items, dependent: :destroy accepts_nested_attributes_for :invoice_items belongs_to :user + belongs_to :invoicing_profile belongs_to :wallet_transaction belongs_to :coupon diff --git a/app/models/invoicing_profile.rb b/app/models/invoicing_profile.rb index 0edd37eca..c08eb3300 100644 --- a/app/models/invoicing_profile.rb +++ b/app/models/invoicing_profile.rb @@ -2,4 +2,5 @@ class InvoicingProfile < ActiveRecord::Base belongs_to :user has_one :address, as: :placeable, dependent: :destroy has_one :organization, dependent: :destroy + has_many :invoices, dependent: :destroy end diff --git a/app/models/user.rb b/app/models/user.rb index 2c6f618e6..d2b7dbe8c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,6 +21,8 @@ class User < ActiveRecord::Base has_one :profile, dependent: :destroy accepts_nested_attributes_for :profile + has_one :invoicing_profile, dependent: :nullify + has_many :my_projects, foreign_key: :author_id, class_name: 'Project', dependent: :destroy has_many :project_users, dependent: :destroy has_many :projects, through: :project_users diff --git a/db/migrate/20190521122429_create_invoicing_profiles.rb b/db/migrate/20190521122429_create_invoicing_profiles.rb index e90e9ec79..02c627455 100644 --- a/db/migrate/20190521122429_create_invoicing_profiles.rb +++ b/db/migrate/20190521122429_create_invoicing_profiles.rb @@ -9,5 +9,6 @@ class CreateInvoicingProfiles < ActiveRecord::Migration end add_reference :organizations, :invoicing_profile, index: true, foreign_key: true + add_reference :invoices, :invoicing_profile, index: true, foreign_key: true end end diff --git a/db/migrate/20190521124609_migrate_profile_to_invoicing_profile.rb b/db/migrate/20190521124609_migrate_profile_to_invoicing_profile.rb index f7a0c2309..27d0b6444 100644 --- a/db/migrate/20190521124609_migrate_profile_to_invoicing_profile.rb +++ b/db/migrate/20190521124609_migrate_profile_to_invoicing_profile.rb @@ -1,8 +1,11 @@ class MigrateProfileToInvoicingProfile < ActiveRecord::Migration def up - Profile.all.each do |p| + User.all.each do |u| + p = u.profile + puts "WARNING: User #{u.id} has no profile" and next unless p + ip = InvoicingProfile.create!( - user: p.user, + user: u, first_name: p.first_name, last_name: p.last_name ) @@ -20,7 +23,7 @@ class MigrateProfileToInvoicingProfile < ActiveRecord::Migration profile = ip.user.profile profile.update_attributes( first_name: ip.first_name, - last_name: ip.last_name, + last_name: ip.last_name ) ip.address&.update_attributes( placeable: profile diff --git a/db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb b/db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb new file mode 100644 index 000000000..6f28d8469 --- /dev/null +++ b/db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb @@ -0,0 +1,23 @@ +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 + # if everything is ok, proceed with migration + Invoice.order(:created_at).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 + end + + def down + 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 + end +end diff --git a/db/schema.rb b/db/schema.rb index 9ecec388c..18c91bd5f 100644 --- a/db/schema.rb +++ b/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: 20190521124609) do +ActiveRecord::Schema.define(version: 20190320091148) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -287,16 +287,6 @@ ActiveRecord::Schema.define(version: 20190521124609) do add_index "invoices", ["user_id"], name: "index_invoices_on_user_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" @@ -393,13 +383,11 @@ ActiveRecord::Schema.define(version: 20190521124609) 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| @@ -898,10 +886,8 @@ ActiveRecord::Schema.define(version: 20190521124609) do add_foreign_key "invoices", "coupons" 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"