Module: DbStructureExt::MysqlDumpMethods

Included in:
MysqlAdapter
Defined in:
lib/db_structure_ext/mysql_dump_methods.rb

Instance Method Summary collapse

Instance Method Details

#routines_dumpObject

dump mysql routines: procedures and functions



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/db_structure_ext/mysql_dump_methods.rb', line 74

def routines_dump
  structure = ""
  return structure unless supports_routines?

  sql = "SELECT ROUTINE_TYPE as type, ROUTINE_NAME as name FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA='#{current_database}'"

  select_all(sql).each do |row|
    structure << select_one("SHOW CREATE #{row['type']} #{row['name']}")["Create #{row['type'].capitalize}"]
    structure.gsub!(/^(CREATE).+(PROCEDURE|FUNCTION)/, '\1 \2')
    structure << ";\n\n"
  end

  structure
end

#structure_dumpObject



89
90
91
92
93
94
95
96
# File 'lib/db_structure_ext/mysql_dump_methods.rb', line 89

def structure_dump
  structure = ""
  structure << tables_dump
  structure << views_dump
  structure << triggers_dump
  structure << routines_dump
  structure
end

#tables_dumpObject

Dump mysql base tables



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/db_structure_ext/mysql_dump_methods.rb', line 5

def tables_dump
  structure = ""

  sql = supports_views? ?
    "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'" : "SHOW TABLES"

  table_names = select_all(sql).map { |table|
    table.delete('Table_type')
    table.to_a.first.last
  }

  table_names.each do |table_name|
    structure << select_one("SHOW CREATE TABLE `#{table_name}`")["Create Table"]
    structure << ";\n\n"
  end

  structure
end

#triggers_dumpObject

dump mysql triggers



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/db_structure_ext/mysql_dump_methods.rb', line 59

def triggers_dump
  structure = ""
  return structure unless supports_triggers?

  sql = "SHOW TRIGGERS"

  select_all(sql).each do |row|
    structure << "CREATE TRIGGER #{row['Trigger']} #{row['Timing']} #{row['Event']} ON #{row['Table']} FOR EACH ROW #{row['Statement']}"
    structure << ";\n\n"
  end

  structure
end

#views_dumpObject

Dump mysql views



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
# File 'lib/db_structure_ext/mysql_dump_methods.rb', line 25

def views_dump
  structure = ""
  return structure unless supports_views?

  table_names = select_all("SHOW FULL TABLES WHERE Table_type = 'VIEW'").map { |table|
    table.delete('Table_type')
    table.to_a.first.last
  }

  # Temporary structure
  table_names.each do |table_name|
    fields = select_all("SHOW FIELDS FROM #{table_name}")
    structure << "CREATE TABLE #{table_name} (\n"
    structure << fields.map { |field| "  #{quote_column_name(field['Field'])} #{field['Type']}" }.join(",\n")
    structure << "\n) ENGINE=MyISAM"
    structure << ";\n\n"
  end

  # Final structure
  table_names.each do |table_name|
    structure << "DROP TABLE IF EXISTS #{table_name}" 
    structure << ";\n\n"
    structure << "DROP VIEW IF EXISTS #{table_name}"
    structure << ";\n\n"
    structure << select_one("SHOW CREATE VIEW #{table_name}")["Create View"] 
    structure << ";\n\n"
  end

  structure.gsub!(/^(CREATE).+(VIEW)/, '\1 \2')

  structure
end