Class: Ro::Migrator

Inherits:
Object show all
Defined in:
lib/ro/migrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path, options = {}) ⇒ Migrator

Returns a new instance of Migrator.



5
6
7
8
9
10
11
12
13
# File 'lib/ro/migrator.rb', line 5

def initialize(root_path, options = {})
  @root_path = Path.for(root_path)
  @options = {
    dry_run: false,
    backup: false,
    verbose: false,
    force: false
  }.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/ro/migrator.rb', line 3

def options
  @options
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



3
4
5
# File 'lib/ro/migrator.rb', line 3

def root_path
  @root_path
end

Instance Method Details

#backupObject

Create backup



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/ro/migrator.rb', line 241

def backup
  timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  backup_name = "#{@root_path.basename}.backup.#{timestamp}"
  backup_path = @root_path.parent.join(backup_name)

  log "Creating backup: #{backup_path}"

  unless dry_run?
    FileUtils.cp_r(@root_path.to_s, backup_path.to_s)
  end

  backup_path
end

#backup?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ro/migrator.rb', line 19

def backup?
  @options[:backup]
end

#dry_run?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ro/migrator.rb', line 15

def dry_run?
  @options[:dry_run]
end

#force?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ro/migrator.rb', line 27

def force?
  @options[:force]
end

#migrateObject

Migrate entire root



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ro/migrator.rb', line 204

def migrate
  log "Starting full migration of #{@root_path}"

  if backup?
    backup_path = backup
    log "Created backup at #{backup_path}"
  end

  validation = validate

  if validation[:old_nodes].empty?
    log "No old structure nodes found to migrate"
    return { success: true, nodes_migrated: 0, collections_migrated: 0 }
  end

  collections = validation[:old_nodes].map { |n| n[:collection] }.uniq
  total_migrated = 0
  collections_migrated = 0

  collections.each do |collection_name|
    result = migrate_collection(collection_name)
    if result[:success]
      collections_migrated += 1
      total_migrated += result[:migrated_count]
    end
  end

  log "Migration complete! Migrated #{total_migrated} nodes across #{collections_migrated} collections"

  {
    success: true,
    nodes_migrated: total_migrated,
    collections_migrated: collections_migrated
  }
end

#migrate_collection(collection_name) ⇒ Object

Migrate an entire collection



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ro/migrator.rb', line 178

def migrate_collection(collection_name)
  log "Migrating collection: #{collection_name}"

  validation = validate
  collection_nodes = validation[:old_nodes].select { |n| n[:collection] == collection_name }

  migrated_count = 0
  errors = []

  collection_nodes.each do |old_node|
    result = migrate_node(collection_name, old_node[:node_id])
    if result[:success]
      migrated_count += 1
    else
      errors << result[:error]
    end
  end

  {
    success: errors.empty?,
    migrated_count: migrated_count,
    errors: errors
  }
end

#migrate_node(collection_name, node_id) ⇒ Object

Migrate a single node



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ro/migrator.rb', line 136

def migrate_node(collection_name, node_id)
  log "Migrating #{collection_name}/#{node_id}..."

  collection_path = @root_path.join(collection_name)
  old_node_path = collection_path.join(node_id)

  unless old_node_path.directory?
    return { success: false, error: "Node directory not found: #{old_node_path}" }
  end

   = collection_path.join("#{node_id}.yml")

  # Look for attributes file in any format
  old_attributes_file = nil
  %w[yml yaml json toml].each do |ext|
    candidate = old_node_path.join("attributes.#{ext}")
    if candidate.exist?
      old_attributes_file = candidate
      break
    end
  end

  unless dry_run?
    if old_attributes_file
      # Move existing attributes file to collection level
      log "  Moving #{old_attributes_file} → #{new_metadata_file}"
      FileUtils.mv(old_attributes_file.to_s, .to_s)
    else
      # Create empty metadata file for nodes without attributes
      log "  Creating empty metadata #{new_metadata_file} (node had no attributes)"
      File.write(.to_s, {}.to_yaml)
    end
  end

  # Assets stay in assets/ subdirectory - no moving needed
  # Old: collection/identifier/assets/foo.png
  # New: collection/identifier/assets/foo.png (same location)

  { success: true, node_id: node_id, had_attributes: !old_attributes_file.nil? }
end

#previewObject

Preview migration without making changes



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ro/migrator.rb', line 98

def preview
  validation = validate
  plan = []

  validation[:old_nodes].each do |old_node|
    collection_name = old_node[:collection]
    node_id = old_node[:node_id]
    old_path = old_node[:old_path]
    has_attributes = old_node[:has_attributes]
    attributes_file = old_node[:attributes_file]

    collection_path = @root_path.join(collection_name)
     = collection_path.join("#{node_id}.yml")
    new_asset_dir = collection_path.join(node_id)

    actions = []
    if has_attributes
      actions << "Move #{attributes_file} → #{new_metadata_file}"
    else
      actions << "Create empty #{new_metadata_file} (node has no attributes)"
    end
    actions << "Assets remain in #{old_path}/assets/ (no change needed)"

    plan << {
      node_id: node_id,
      collection: collection_name,
      old_path: old_path,
      new_metadata_file: ,
      new_asset_dir: new_asset_dir,
      has_attributes: has_attributes,
      actions: actions
    }
  end

  plan
end

#rollbackObject

Rollback from backup



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/ro/migrator.rb', line 256

def rollback
  # Find most recent backup
  backup_pattern = "#{@root_path.basename}.backup.*"
  backups = @root_path.parent.glob(backup_pattern).sort.reverse

  if backups.empty?
    return { success: false, error: "No backups found" }
  end

  backup_path = backups.first
  log "Rolling back from #{backup_path}"

  unless dry_run?
    # Remove current root
    FileUtils.rm_rf(@root_path.to_s)
    # Restore from backup
    FileUtils.cp_r(backup_path.to_s, @root_path.to_s)
  end

  {
    success: true,
    restored_from: backup_path
  }
end

#validateObject

Validate the structure and return analysis



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ro/migrator.rb', line 32

def validate
  result = {
    has_old_structure: false,
    has_new_structure: false,
    old_nodes: [],
    new_nodes: [],
    collections: []
  }

  root = Root.for(@root_path)

  root.collections.each do |collection|
    collection_name = collection.name
    result[:collections] << collection_name

    # Check for new structure (metadata files at collection level)
    collection..each do |entry|
      # Only count new-structure nodes for migration purposes
      next unless entry[:type] == :new

      result[:new_nodes] << {
        collection: collection_name,
        node_id: entry[:id],
        metadata_file: entry[:path]
      }
      result[:has_new_structure] = true
    end

    # Check for old structure (ALL subdirectories, with or without attributes)
    # We need to migrate ALL nodes, even those without attributes.yml
    collection.subdirectories.each do |subdir|
      node_id = subdir.basename.to_s

      # Skip if this node already has new-structure metadata
      already_migrated = result[:new_nodes].any? { |n|
        n[:collection] == collection_name && n[:node_id] == node_id
      }
      next if already_migrated

      # Check if there's an attributes file (any format)
      attributes_file = nil
      has_attributes = false
      %w[yml yaml json toml].each do |ext|
        candidate = subdir.join("attributes.#{ext}")
        if candidate.exist?
          attributes_file = candidate
          has_attributes = true
          break
        end
      end

      result[:old_nodes] << {
        collection: collection_name,
        node_id: node_id,
        old_path: subdir,
        attributes_file: attributes_file,
        has_attributes: has_attributes
      }
      result[:has_old_structure] = true
    end
  end

  result
end

#verbose?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/ro/migrator.rb', line 23

def verbose?
  @options[:verbose]
end