Class: DT3MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/typo3/dt3_mysql.rb

Class Method Summary collapse

Class Method Details

.create_exclude_string(excludelist) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 97

def self.create_exclude_string(excludelist)
  s = ''
  excludelist.each {|extab|
    if(s.length>0)
      s += " "
    end
    s += "--ignore-table=#{fetch(:dbname)}.#{extab}"
  }
  return s
end

.create_mysql_base_command(exec = 'mysql') ⇒ Object



89
90
91
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 89

def self.create_mysql_base_command(exec='mysql')
  return self.create_mysql_base_command_with(fetch(:dbuser),fetch(:dbhost),fetch(:dbpass),fetch(:dbname),exec)
end

.create_mysql_base_command_with(user, host, password, db, exec = 'mysql') ⇒ Object



28
29
30
31
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 28

def self.create_mysql_base_command_with(user,host,password,db,exec='mysql')
  cmd = "#{self.mysql_executable_dir}/#{exec} -u#{user} -h#{host} -p#{password} #{db}"
  return cmd
end

.db_image_listObject



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
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 33

def self.db_image_list
  images_arr = []
  idx = 0

  Dir.glob("#{TYPO3_DB_DUMP_DIR}/*.sql").sort.each {|sql|
    image = Hash.new
    if File.extname(sql) == '.sql'
      idx = idx+1
      image['index'] = idx

      image['filesize (Mb)'] = '%.2f' % (File.size(sql).to_f / 2**20)


      if(sql.split('.').count == 3)
        image['version'] = sql.split('.')[1]
        image['name'] = File.basename(sql.split('.')[0])
      elsif(sql.split('-').count == 2)
        image['version'] = sql.split('-')[1].split('.')[0]
        image['name'] = File.basename(sql.split('-')[0])
      else
        image['version'] = '[MASTER]'
        image['name'] = File.basename(sql,'.*')
      end
      image['time'] = File.mtime(sql).strftime("%Y-%m-%d %H:%M")
      image['filename'] = sql

      images_arr << image
    end
  }
  return images_arr
end

.dump_db_version(table_exclude_list = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 65

def self.dump_db_version(table_exclude_list=nil)

  filename =''
  numbers = []

  Dir.foreach(TYPO3_DB_DUMP_DIR) {|sql|
    tmpname = sql.split('.')
    if(tmpname.count == 3)
      numbers << tmpname[1].to_i
    end
  }
  if(numbers.count > 0)
    version = (numbers.max + 1)
  else
    version = 1
  end

  branch = `git rev-parse --abbrev-ref HEAD`.gsub("\n",'')
  filename = File.join(TYPO3_DB_DUMP_DIR,"#{fetch(:dbname)}-#{branch}.#{version.to_s}.sql")
  print "new image:#{fetch(:dbname)} version:#{version}\n"
  DT3MySQL::mysqldump_to(filename,table_exclude_list)
end

.flush_tablesObject



11
12
13
14
15
16
17
18
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 11

def self.flush_tables
  tablelist = `#{self.create_mysql_base_command} -e "show tables" | grep -v Tables_in | grep -v "+"`
  dropsql = ''
  tablelist.split("\n").each {|table|
    dropsql +="drop table #{table};"
  }
  self.mysql_execute(dropsql)
end

.mysql_executable_dirObject



3
4
5
6
7
8
9
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 3

def self.mysql_executable_dir
  if fetch(:rake_mysql_exec_dir)
    return fetch(:rake_mysql_exec_dir)
  else
    return '/usr/bin'
  end
end

.mysql_execute(sql) ⇒ Object



93
94
95
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 93

def self.mysql_execute(sql)
  "#{self.create_mysql_base_command} -e \"#{sql}\""
end

.mysql_import(insqlfile) ⇒ Object



119
120
121
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 119

def self.mysql_import(insqlfile)
  "#{create_mysql_base_command} < #{insqlfile}"
end

.mysqldump_to(outputfile, excludelist = nil, no_schema = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 108

def self.mysqldump_to(outputfile,excludelist=nil,no_schema=nil)

  if(not excludelist.nil?)
    excludestring = self.create_exclude_string(excludelist)
  else
    excludestring = ''
  end

  "#{create_mysql_base_command('mysqldump')} #{excludestring} > #{outputfile}"
end

.show_tablesObject



20
21
22
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 20

def self.show_tables
  return self.mysql_execute('show tables')
end

.truncate_table(table) ⇒ Object



24
25
26
# File 'lib/capistrano/typo3/dt3_mysql.rb', line 24

def self.truncate_table(table)
  self.mysql_execute("TRUNCATE TABLE #{table};")
end