Class: Synthra::CLI::Commands::Seed

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/cli/commands/seed.rb

Overview

CLI command for database seeding

Examples:

Seed database

$ synthra seed --schema-dir schemas/ --count 100

Constant Summary collapse

EXIT_SUCCESS =
0
EXIT_ERROR =
1

Instance Method Summary collapse

Constructor Details

#initializeSeed

Returns a new instance of Seed.



15
16
17
# File 'lib/synthra/cli/commands/seed.rb', line 15

def initialize
  # No args needed for this command
end

Instance Method Details

#call(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/synthra/cli/commands/seed.rb', line 19

def call(options)
  schema_dir = options[:schema_dir] || "schemas"
  count = options[:count] || 10
  seed_file = options[:file]

  if seed_file && File.exist?(seed_file)
    run_seed_file(seed_file)
  else
    run_interactive_seed(schema_dir, count)
  end
end

#run_interactive_seed(schema_dir, count) ⇒ Object (private)



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/synthra/cli/commands/seed.rb', line 49

def run_interactive_seed(schema_dir, count)
  unless File.directory?(schema_dir)
    $stderr.puts "✗ Schema directory not found: #{schema_dir}"
    return EXIT_ERROR
  end
  
  require_relative "../../database_seeder"
  
  puts "🌱 Interactive Database Seeding"
  puts ""
  puts "   Schema directory: #{schema_dir}"
  puts "   Default count: #{count}"
  puts ""

  # Load schemas
  registry = Registry.new
  registry.load_dir(schema_dir)

  if registry.names.empty?
    $stderr.puts "✗ No schemas found in #{schema_dir}"
    return EXIT_ERROR
  end

  puts "Available schemas:"
  registry.names.sort.each_with_index do |name, i|
    puts "   #{i + 1}. #{name}"
  end
  puts ""

  # Generate sample data for each schema
  puts "Generated sample data (#{count} records each):"
  puts ""

  registry.names.sort.each do |name|
    schema = registry.schema(name)
    data = schema.generate_many(count)
    puts "#{name}:"
    puts JSON.pretty_generate(data[0, 2]) # Show first 2
    puts "   ... and #{count - 2} more records"
    puts ""
  end

  EXIT_SUCCESS
rescue StandardError => e
  $stderr.puts "✗ Seeding failed: #{e.message}"
  EXIT_ERROR
end

#run_seed_file(seed_file) ⇒ Object (private)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/synthra/cli/commands/seed.rb', line 33

def run_seed_file(seed_file)
  require_relative "../../database_seeder"
  
  puts "🌱 Running seed file: #{seed_file}"
  puts ""

  content = File.read(seed_file)
  seeder = DatabaseSeeder.new
  seeder.instance_eval(content)

  EXIT_SUCCESS
rescue StandardError => e
  $stderr.puts "✗ Seeding failed: #{e.message}"
  EXIT_ERROR
end