Class: CortexReaver::ProjectsToPagesSchema

Inherits:
Sequel::Migration
  • Object
show all
Defined in:
lib/cortex_reaver/migrations/014_convert_projects_to_pages.rb

Instance Method Summary collapse

Instance Method Details

#downObject



4
5
6
# File 'lib/cortex_reaver/migrations/014_convert_projects_to_pages.rb', line 4

def down
  # Nope. Not even going to try.
end

#upObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cortex_reaver/migrations/014_convert_projects_to_pages.rb', line 8

def up
  pages = self[:pages]
  projects = self[:projects]

  # Create projects page
  unless root = pages.filter(:name => 'projects').first
    pages.insert(
      :name => 'projects',
      :title => 'Projects',
      :created_on => Time.now,
      :updated_on => Time.now
    )
  end
  root = pages.filter(:name => 'projects').first

  # Migrate each project
  projects.all.each do |project|
    project_id = project[:id]

    # Create page
    project.delete :description
    project.delete :id
    project[:page_id] = root[:id]
    pages << project

    # Get page id
    page_id = pages.filter(:page_id => root[:id], :name => project[:name]).first[:id]
    
    # Reparent comments
    self[:comments].filter(:project_id => project_id).update(
      :project_id => nil,
      :page_id => page_id
    )
   
    # Copy tags
    self[:projects_tags].filter(:project_id => project_id).each do |t|
      self[:pages_tags] << {:page_id => page_id, :tag_id => t[:tag_id]}
    end

    # Move attachments
    # This is a little tricky; I'd like to deprecate the entire model at
    # some point. We assume it lives in config.public_dir/data/projects/id
    project_dir = File.join(
      CortexReaver.config.public_root, 'data', 'projects', project_id.to_s
    )
    page_dir = File.join(
      CortexReaver.config.public_root, 'data', 'pages', page_id.to_s
    )
    if File.directory? project_dir
      if File.directory? page_dir
        puts "WARNING: #{page_dir} already exists. Not moving contents of #{project_dir} into it. You should take a look."
      else
        FileUtils.move project_dir, page_dir
      end
    end
    
    projects.filter(:id => project_id).delete
  end

  # Drop associated columns
  # Can't do this yet--foreign key constraints...
#      alter_table :comments do
#        drop_column :project_id
#      end

  # Drop projects table
  unless table_exists? :projects
    drop_table :projects
  end
end