Class: Edgestitch::Mysql::Dump

Inherits:
Object
  • Object
show all
Defined in:
lib/edgestitch/mysql/dump.rb

Overview

Wrapper for the mysqldump tool to dump specific tables and migration data

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Dump

Returns a new instance of Dump.

Parameters:

  • config (ActiveRecord::DatabaseConfigurations::DatabaseConfig)

    rails database configuration



34
35
36
37
38
39
40
41
42
43
# File 'lib/edgestitch/mysql/dump.rb', line 34

def initialize(config)
  hash = config.respond_to?(:configuration_hash) ? config.configuration_hash : config.config
  @database = hash["database"] || hash[:database]
  @config = {
    "-h" => hash["host"] || hash[:host],
    "-u" => hash["username"] || hash[:username],
    "-p" => hash["password"] || hash[:password],
    "--port=" => hash["port"] || hash[:port],
  }
end

Class Method Details

.sanitize_sql(sql) ⇒ Object

Sanitizes a DDL code with some opinionated preferences:

  • Constraints starting with ‘_fk` will start with `fk`

  • Clear empty lines (with empty spaces even)

  • Reorder constraints (@see Edgestitch::Mysql::StructureConstraintOrderMunger)

Parameters:

  • sql (String)

    the DDL code to sanitize

Returns:

  • String the same DDL sanitized



21
22
23
24
25
26
27
28
29
30
# File 'lib/edgestitch/mysql/dump.rb', line 21

def self.sanitize_sql(sql)
  comment_instructions_regex = %r{^/\*![0-9]{5}\s+[^;]+;\s*$}

  cleanups = sql.gsub(/\s+AUTO_INCREMENT=\d+/, "")
                .gsub(/CONSTRAINT `_+fk_/, "CONSTRAINT `fk_")
                .gsub(comment_instructions_regex, "")
                .gsub(/\n\s*\n\s*\n/, "\n\n")
                .strip
  ::Edgestitch::Mysql::StructureConstraintOrderMunger.munge(cleanups)
end

Instance Method Details

#export_migrations(migrations) ⇒ Object

Exports INSERT statements for the given migration names.

The INSERT statements are in groups of 50 migrations per multi-insert statement.

Notice: this does not export the creation of the schema_migrations table.

Parameters:

  • migrations (Array<Integer,String>)

    migration ids/timestamps

Returns:

  • String the INSERT statements.



66
67
68
69
70
71
72
73
74
75
# File 'lib/edgestitch/mysql/dump.rb', line 66

def export_migrations(migrations)
  migrations.in_groups_of(50, false).map do |versions|
    execute(
      "--compact", "--skip-lock-tables", "--single-transaction", "--set-gtid-purged=OFF",
      "--no-create-info", "--column-statistics=0",
      "schema_migrations",
      "-w", "version IN (#{versions.join(',')})"
    )
  end.join.gsub(/VALUES /, "VALUES\n").gsub(/,/, ",\n")
end

#export_tables(tables) ⇒ Object

Exports DDL for the given tables in a mysql compatible way

Parameters:

  • tables (Array<String>)

    table names

Returns:

  • String the DDL for the given tables



49
50
51
52
53
54
55
56
# File 'lib/edgestitch/mysql/dump.rb', line 49

def export_tables(tables)
  return if tables.empty?

  self.class.sanitize_sql(
    execute("--compact", "--skip-lock-tables", "--single-transaction", "--no-data", "--set-gtid-purged=OFF",
            "--column-statistics=0", *tables)
  )
end