Class: DumpCleaner::Cleaners::MysqlShellTableCleaner::DumpTableInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb

Constant Summary collapse

DialectOptions =
Data.define(:lines_terminated_by, :fields_terminated_by, :fields_enclosed_by,
:fields_optionally_enclosed, :fields_escaped_by)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_info) ⇒ DumpTableInfo

Returns a new instance of DumpTableInfo.



134
135
136
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 134

def initialize(table_info)
  @table_info = table_info
end

Class Method Details

.load(db:, table:, source_dump_path:) ⇒ Object



124
125
126
127
128
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 124

def self.load(db:, table:, source_dump_path:)
  new(JSON.parse(File.read(table_info_file_path(db:, table:, source_dump_path:))))
rescue Errno::ENOENT
  raise "Table info file not found in dump for table '#{db}.#{table}'. Is the table included in the dump?"
end

.table_info_file_path(db:, table:, source_dump_path:) ⇒ Object



130
131
132
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 130

def self.table_info_file_path(db:, table:, source_dump_path:)
  "#{source_dump_path}/#{db}@#{table}.json"
end

Instance Method Details

#column_index(name) ⇒ Object



166
167
168
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 166

def column_index(name)
  columns.index(name)
end

#columnsObject



162
163
164
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 162

def columns
  @columns ||= @table_info.dig("options", "columns")
end

#compressionObject



154
155
156
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 154

def compression
  @table_info["compression"]
end

#dbObject



138
139
140
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 138

def db
  @db ||= @table_info.dig("options", "schema")
end

#db_at_tableObject



150
151
152
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 150

def db_at_table
  "#{db}@#{table}"
end

#db_dot_tableObject



146
147
148
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 146

def db_dot_table
  "#{db}.#{table}"
end

#dialectObject



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 170

def dialect
  @dialect ||= begin
    dialect_options = DialectOptions.members.each_with_object({}) do |option, options|
      lowercase_option = option.to_s.split("_").each_with_object([]) do |e, buffer|
        buffer.push(buffer.empty? ? e : e.capitalize)
      end.join
      options[option] = @table_info.dig("options", lowercase_option)
    end
    DialectOptions.new(**dialect_options)
  end
end

#extensionObject



158
159
160
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 158

def extension
  @table_info["extension"]
end

#tableObject



142
143
144
# File 'lib/dump_cleaner/cleaners/mysql_shell_table_cleaner.rb', line 142

def table
  @table ||= @table_info.dig("options", "table")
end