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.



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

def initialize(table_info)
  @table_info = table_info
end

Class Method Details

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



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

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



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

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



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

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

#columnsObject



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

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

#compressionObject



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

def compression
  @table_info["compression"]
end

#dbObject



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

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

#db_at_tableObject



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

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

#db_dot_tableObject



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

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

#dialectObject



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

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



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

def extension
  @table_info["extension"]
end

#tableObject



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

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