Advertisement
matthewpoer

Michigan Migration

Apr 26th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.87 KB | None | 0 0
  1. -- Copy all of the data, using account_id for contact_id even though it's wrong
  2. insert into contacts_contr_contributions_1_c
  3. (id,
  4. contacts_contr_contributions_1contacts_ida,
  5. contacts_contr_contributions_1contr_contributions_idb)
  6. select
  7. accounts_contr_contributions_3_c.id,
  8. accounts_contr_contributions_3_c.accounts_contr_contributions_3accounts_ida,
  9. accounts_contr_contributions_3_c.accounts_contr_contributions_3contr_contributions_idb
  10. from accounts_contr_contributions_3_c where accounts_contr_contributions_3_c.deleted = 0;
  11.  
  12. -- Replace the account_id with contact_id
  13. update contacts_contr_contributions_1_c
  14. join accounts_contacts on contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida = accounts_contacts.account_id
  15. join contacts on accounts_contacts.contact_id = contacts.id
  16. set contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida
  17.   = contacts.id
  18. where contacts.id in (
  19.   select contacts.id
  20.   from contacts
  21.   join accounts_contacts on contacts.id = accounts_contacts.contact_id
  22. );
  23.  
  24. -- find the leftovers
  25. select id,name from accounts
  26. where accounts.id in (
  27. select distinct(contacts_contr_contributions_1contacts_ida) from contacts_contr_contributions_1_c
  28. where contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida in
  29. (select accounts.id from accounts)
  30. );
  31.  
  32. -- turn the leftovers into contacts records of the same name
  33. insert into contacts(id,last_name,date_modified)
  34. select accounts.id,accounts.name,NOW() from accounts
  35. where accounts.id in (
  36. select distinct(contacts_contr_contributions_1contacts_ida) from contacts_contr_contributions_1_c
  37. where contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida in
  38. (select accounts.id from accounts)
  39. );
  40.  
  41. -- and link those bitches together, use the same ID for all because I'm awful
  42. insert into accounts_contacts (id,account_id,contact_id,date_modified)
  43. select accounts.id,accounts.id,accounts.id,NOW() from accounts
  44. where accounts.id in (
  45. select distinct(contacts_contr_contributions_1contacts_ida) from contacts_contr_contributions_1_c
  46. where contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida in
  47. (select accounts.id from accounts)
  48. );
  49.  
  50. -- Look up the Accounts that have the same name as a Contact, fuzzy match on first+last name
  51. select accounts.id as account_id,contacts.id as contact_id
  52. from accounts,contacts
  53. where accounts.name is not null and accounts.name <> '' and accounts.deleted = 0
  54. and contacts.first_name is not null and contacts.first_name <> '' and
  55. contacts.last_name is not null and contacts.last_name <> '' and contacts.deleted = 0
  56. and accounts.name = ltrim(rtrim(concat(contacts.first_name,' ',contacts.last_name)))
  57. order by accounts.name;
  58.  
  59. -- Update the contacts_contr_contributions_1_c Contact to parent account,
  60. -- and parent account to matching name-based contact
  61. update contacts_contr_contributions_1_c
  62. join accounts_contacts on accounts_contacts.contact_id = contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida
  63. join accounts on accounts_contacts.account_id = accounts.id
  64. join contacts new_contacts on accounts.name = ltrim(rtrim(concat(new_contacts.first_name,' ',new_contacts.last_name)))
  65. set contacts_contr_contributions_1_c.contacts_contr_contributions_1contacts_ida = new_contacts.id
  66. where contacts_contr_contributions_1_c.deleted = 0
  67. and accounts_contacts.deleted = 0
  68. and accounts.deleted = 0
  69. and new_contacts.deleted = 0;
  70.  
  71. -- Copy the one:many relate to the many:many
  72. -- done after the above, and after some manual data cleanup
  73. -- insert into
  74. -- campa_campaigncommittee_contr_contributions_1_c
  75. -- (
  76. --     id,date_modified,
  77. --     campa_camp05f8mmittee_ida,
  78. --     campa_camp678cbutions_idb
  79. -- )
  80. -- select
  81. --     id_c as link_id,NOW(),
  82. --     campa_campaigncommittee_id_c as campaign_committee_id,
  83. --     id_c as contribution_id
  84. -- from contr_contributions_cstm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement