Module: MigrationGenerator

Defined in:
lib/nando/generator.rb

Class Method Summary collapse

Class Method Details

.create_baseline_file(filepath, migration_name) ⇒ Object



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
# File 'lib/nando/generator.rb', line 38

def self.create_baseline_file (filepath, migration_name)
  dir = File.dirname(filepath)

  if !File.directory?(dir)
    raise Nando::GenericError.new("No directory '#{dir}' was found")
  end

  @db_connection = NandoMigrator.instance.get_database_connection();
  results = @db_connection.exec("
    SELECT n.nspname AS function_schema,
           p.proname AS function_name,
           l.lanname AS function_language,
           CASE WHEN l.lanname = 'internal' THEN p.prosrc ELSE pg_get_functiondef(p.oid) END AS definition,
           pg_get_function_arguments(p.oid) AS function_arguments,
           t.typname AS return_type,
           p.proowner AS p_owner
      FROM pg_proc p
      LEFT JOIN pg_namespace n ON p.pronamespace = n.oid
      LEFT JOIN pg_language l ON p.prolang = l.oid
      LEFT JOIN pg_type t ON t.oid = p.prorettype
     WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
     ORDER BY function_schema, function_name
  ")

  up_method = ''
  number_of_functions = 0
  indent = '    '

  for row in results do
    up_method += "\n" + indent + "update_function <<-'SQL'\n"
    up_method += "#{row['definition']}"
    up_method += "\n" + indent + "SQL\n"
    number_of_functions += 1
  end

  new_file = File.new(filepath, 'w')

  # binding
  migration_class_name = migration_name.camelize
  migration_type = Nando::Migration.name.demodulize # TODO: atm all baseline files are create as migrations with transactions, this might change later
  migration_up_code = up_method
  migration_down_code = indent + "# #{number_of_functions} functions have been added to this baseline"

  render_to_file(File.join(File.dirname(File.expand_path(__FILE__)), 'baseline_templates/migration.rb'), binding, new_file)

  puts "Creating a new baseline: #{filepath}"
end

.create_migration_file(filepath, migration_name, migration_type) ⇒ Object

creates the actual migration file



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nando/generator.rb', line 6

def self.create_migration_file (filepath, migration_name, migration_type)
  dir = File.dirname(filepath)

  if !File.directory?(dir)
    raise Nando::GenericError.new("No directory '#{dir}' was found")
  end

  case migration_type
  when Nando::Migration.name.demodulize
    template_file_name = 'migration'
  when Nando::MigrationWithoutTransaction.name.demodulize
    template_file_name = 'migration_without_transaction'
  end

  migration_class_name = migration_name.camelize()
  file = File.new(filepath, 'w')
  # TODO: check if binding logic is correct, and if pathing changes when it's a gem
  render_to_file(File.join(File.dirname(File.expand_path(__FILE__)), "templates/#{template_file_name}.rb"), binding, file)

  puts "Creating a new migration: #{filepath}"
end

.render(template_file, context) ⇒ Object



33
34
35
36
# File 'lib/nando/generator.rb', line 33

def self.render(template_file, context)
  renderer = ERB.new(File.read(template_file), nil, nil)
  renderer.result(context)
end

.render_to_file(template_file, context, output_file) ⇒ Object

based on the template renderer from the commercial engine



29
30
31
# File 'lib/nando/generator.rb', line 29

def self.render_to_file (template_file, context, output_file)
  output_file.write render(template_file, context)
end