diff options
| author | hukl <contact@smyck.org> | 2009-09-17 11:07:02 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-09-17 11:07:02 +0200 |
| commit | c7b11c1ba050e23ea460e26073beba5b7f15c0f1 (patch) | |
| tree | b52b4a971839aa00e2edee0905836de941ef4cd0 /lib | |
| parent | f952040dd44b914b2d5e405567eb655757dfc85d (diff) | |
preparing for postgres migration. adding rake task for conversion
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/tasks/mysql_to_psql.rake | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/lib/tasks/mysql_to_psql.rake b/lib/tasks/mysql_to_psql.rake new file mode 100644 index 0000000..98438fd --- /dev/null +++ b/lib/tasks/mysql_to_psql.rake | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | namespace :cccms do | ||
| 2 | |||
| 3 | desc "Import a cccms mysql dump into an external postgres db" | ||
| 4 | |||
| 5 | task :mysql_to_postgres => :environment do | ||
| 6 | |||
| 7 | $connection_options = { | ||
| 8 | :adapter => "postgresql", | ||
| 9 | :encoding => "unicode", | ||
| 10 | :host => "localhost", | ||
| 11 | :username => "rails", | ||
| 12 | :password => "r3v0lution", | ||
| 13 | :database => "cccms_dev" | ||
| 14 | } | ||
| 15 | |||
| 16 | class PGUser < ActiveRecord::Base | ||
| 17 | self.establish_connection($connection_options) | ||
| 18 | set_table_name "users" | ||
| 19 | end | ||
| 20 | |||
| 21 | class PGNode < ActiveRecord::Base | ||
| 22 | self.establish_connection($connection_options) | ||
| 23 | set_table_name "nodes" | ||
| 24 | end | ||
| 25 | |||
| 26 | class PGPage < ActiveRecord::Base | ||
| 27 | self.establish_connection($connection_options) | ||
| 28 | set_table_name "pages" | ||
| 29 | end | ||
| 30 | |||
| 31 | class PGPageTranslation < ActiveRecord::Base | ||
| 32 | self.establish_connection($connection_options) | ||
| 33 | set_table_name "page_translations" | ||
| 34 | end | ||
| 35 | |||
| 36 | class PGEvent < ActiveRecord::Base | ||
| 37 | self.establish_connection($connection_options) | ||
| 38 | set_table_name "events" | ||
| 39 | end | ||
| 40 | |||
| 41 | class PGMenuItem < ActiveRecord::Base | ||
| 42 | self.establish_connection($connection_options) | ||
| 43 | set_table_name "menu_items" | ||
| 44 | end | ||
| 45 | |||
| 46 | class PGMenuItemTranslation < ActiveRecord::Base | ||
| 47 | self.establish_connection($connection_options) | ||
| 48 | set_table_name "menu_item_translations" | ||
| 49 | end | ||
| 50 | |||
| 51 | class PGOccurrence < ActiveRecord::Base | ||
| 52 | self.establish_connection($connection_options) | ||
| 53 | set_table_name "occurrences" | ||
| 54 | end | ||
| 55 | |||
| 56 | class PGTag < ActiveRecord::Base | ||
| 57 | self.establish_connection($connection_options) | ||
| 58 | set_table_name "tags" | ||
| 59 | end | ||
| 60 | |||
| 61 | class PGTagging < ActiveRecord::Base | ||
| 62 | self.establish_connection($connection_options) | ||
| 63 | set_table_name "taggings" | ||
| 64 | end | ||
| 65 | |||
| 66 | PGUser.delete_all | ||
| 67 | PGNode.delete_all | ||
| 68 | PGPage.delete_all | ||
| 69 | PGPageTranslation.delete_all | ||
| 70 | PGEvent.delete_all | ||
| 71 | PGMenuItem.delete_all | ||
| 72 | PGMenuItemTranslation.delete_all | ||
| 73 | PGOccurrence.delete_all | ||
| 74 | PGTag.delete_all | ||
| 75 | PGTagging.delete_all | ||
| 76 | |||
| 77 | User.all.each do |user| | ||
| 78 | PGUser.create! user.attributes | ||
| 79 | end | ||
| 80 | |||
| 81 | Tag.all.each do |tag| | ||
| 82 | PGTag.create tag.attributes | ||
| 83 | end | ||
| 84 | |||
| 85 | Node.all.each do |node| | ||
| 86 | |||
| 87 | pg_node = PGNode.new node.attributes | ||
| 88 | pg_node.locking_user_id = nil | ||
| 89 | pg_node.save! | ||
| 90 | puts "PGNode #{pg_node.unique_name} created" | ||
| 91 | |||
| 92 | if node.event | ||
| 93 | pg_event = PGEvent.new node.event.attributes | ||
| 94 | pg_event.node_id = pg_node.id | ||
| 95 | pg_event.save | ||
| 96 | puts "PGEvent created" | ||
| 97 | end | ||
| 98 | |||
| 99 | node.pages.each do |page| | ||
| 100 | pg_page = PGPage.create!( page.attributes ) | ||
| 101 | pg_page.node_id = PGNode.find_by_unique_name(node.unique_name).id | ||
| 102 | pg_page.user_id = PGUser.find_by_login(page.user.login).id rescue PGUser.first.id | ||
| 103 | pg_page.save | ||
| 104 | puts "PGPage created" | ||
| 105 | |||
| 106 | page.tags.each do |tag| | ||
| 107 | pg_tagging = PGTagging.create( | ||
| 108 | :tag_id => PGTag.find_by_name(tag.name).id, | ||
| 109 | :taggable_id => pg_page.id, | ||
| 110 | :taggable_type => "Page" | ||
| 111 | ) | ||
| 112 | end | ||
| 113 | |||
| 114 | if node.head && page.id == node.head.id | ||
| 115 | pg_node.head_id = pg_page.id | ||
| 116 | pg_node.save | ||
| 117 | puts "======================Head applied #{pg_page.id}" | ||
| 118 | end | ||
| 119 | |||
| 120 | if node.draft && page.id == node.draft.id | ||
| 121 | pg_node.draft_id = pg_page.id | ||
| 122 | pg_node.save | ||
| 123 | puts "Draft applied" | ||
| 124 | end | ||
| 125 | |||
| 126 | page.globalize_translations.each do |trans| | ||
| 127 | pg_page_trans = PGPageTranslation.new trans.attributes | ||
| 128 | pg_page_trans.page_id = pg_page.id | ||
| 129 | pg_page_trans.save | ||
| 130 | puts "PGPageTranslation created" | ||
| 131 | end | ||
| 132 | end | ||
| 133 | end | ||
| 134 | |||
| 135 | |||
| 136 | MenuItem.all.each do |item| | ||
| 137 | pg_menu_item = PGMenuItem.new item.attributes | ||
| 138 | pg_menu_item.node_id = Node.find(item.node_id).id | ||
| 139 | pg_menu_item.save | ||
| 140 | puts "PGMenuItem created" | ||
| 141 | |||
| 142 | item.globalize_translations.each do |trans| | ||
| 143 | pg_menu_item_trans = PGMenuItemTranslation.new trans.attributes | ||
| 144 | pg_menu_item_trans.menu_item_id = item.id | ||
| 145 | pg_menu_item_trans.save | ||
| 146 | puts "PGMenuItemTranslation created" | ||
| 147 | end | ||
| 148 | end | ||
| 149 | |||
| 150 | |||
| 151 | puts "Now recreate Occurrences by running " \ | ||
| 152 | "Event.all.each {|x| Occurrence.generate(x)} in script/console" | ||
| 153 | |||
| 154 | end | ||
| 155 | |||
| 156 | end \ No newline at end of file | ||
