bard-backup 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44a2ad21a4947347c0bfee31ab392adc09d20a338af463a331cd66d834c3a76a
4
- data.tar.gz: 52fc265e55825f3753c66bb30c411a3367b5e928bc8b639036e4e40f1cacdc48
3
+ metadata.gz: 7feba4c3d529600388a9a0922db65848ba6d7530762a46109ce0bc28760b21e5
4
+ data.tar.gz: 000f70e50d2d6db3c4205df7f74a7e9e75fee869aa57a702417f154ea876a1ec
5
5
  SHA512:
6
- metadata.gz: dc47138a8c9f787e11d9aa7cac6748e27f25a7ca5bf6334dad91f2150fc00cd598a1ddc7f9e67cffbdf9bde8346b6f3143886e315f8ab30fa2d6c7aa360c83c1
7
- data.tar.gz: 5b6a9ad1c165625f826ee05dda3d1b819e2dc6ac6df546445e46b10df082402b50e347788607f149a673a5d4e54e56737badb86470ce2698aa99956e89674dce
6
+ metadata.gz: 7811bbd0c7bba1aba36609854d703f39bb1651e95e842e8d9c00ff15ec03da380ebaa9ba285caeac3e08fd0166a00bcdf01e8b762b88965926ca94d868b96c1e
7
+ data.tar.gz: ad398f7f3d0f51f5ff6f07fad9fd7d15df702d88476029de0bfc1a1dc0fbc67e2fa7bd944cee4e44b920fefd7b3cc2a086472b1558b49c3f04f51d7190f73f4b
data/README.md CHANGED
@@ -99,7 +99,9 @@ production:
99
99
  ```
100
100
 
101
101
  The database user needs permission to create and drop the scratch database, and the server
102
- needs disk space for a second copy of the database while the check runs.
102
+ needs disk space for a second copy of the database while the check runs. When MySQL's binary
103
+ log is on, the check turns it off for its own session (which needs `SESSION_VARIABLES_ADMIN`),
104
+ so the scratch copy neither fills the binary log nor reaches replicas.
103
105
 
104
106
  ## Object Lock
105
107
 
@@ -108,7 +108,16 @@ module Bard
108
108
 
109
109
  def load(path)
110
110
  require "bard/backup/restore_check/scratch_record"
111
- Backhoe::Load.new(database, path, true).call
111
+ if database.mysql?
112
+ @binlog = with_server { |connection| connection.select_value("SELECT @@log_bin").to_i == 1 }
113
+ with_server do |connection|
114
+ connection.execute("DROP DATABASE IF EXISTS #{connection.quote_table_name(name)}")
115
+ connection.execute("CREATE DATABASE #{connection.quote_table_name(name)}")
116
+ end
117
+ Backhoe::Load.new(database, unlogged(path), false).call
118
+ else
119
+ Backhoe::Load.new(database, path, true).call
120
+ end
112
121
  row_counts
113
122
  ensure
114
123
  drop
@@ -116,8 +125,37 @@ module Bard
116
125
 
117
126
  private
118
127
 
128
+ def name = config["database"]
129
+
130
+ # A restored copy in the binary log would fill the disk for its retention period and replicate.
131
+ def unlogged(path)
132
+ return path unless @binlog
133
+ File.join(File.dirname(path), "unlogged-#{File.basename(path)}").tap do |unlogged|
134
+ File.open(unlogged, "wb") do |file|
135
+ file.write(Zlib.gzip("SET SESSION sql_log_bin = 0;\n"))
136
+ File.open(path, "rb") { |dump| IO.copy_stream(dump, file) }
137
+ end
138
+ end
139
+ end
140
+
141
+ def connection_config(overrides = {})
142
+ hash = config.merge(overrides).to_h
143
+ hash["variables"] = hash.fetch("variables", {}).merge("sql_log_bin" => 0) if @binlog
144
+ hash
145
+ end
146
+
147
+ def with_server(&block)
148
+ server = database.postgresql? ? { "database" => "postgres" } : {}
149
+ hash = connection_config(server)
150
+ hash.delete("database") if database.mysql?
151
+ ScratchRecord.establish_connection(hash)
152
+ ScratchRecord.connection_pool.with_connection(&block)
153
+ ensure
154
+ ScratchRecord.remove_connection
155
+ end
156
+
119
157
  def row_counts
120
- ScratchRecord.establish_connection(config.to_h)
158
+ ScratchRecord.establish_connection(connection_config)
121
159
  ScratchRecord.connection_pool.with_connection do |connection|
122
160
  connection.tables.sort.to_h do |table|
123
161
  [table, connection.select_value("SELECT COUNT(*) FROM #{connection.quote_table_name(table)}").to_i]
@@ -129,11 +167,7 @@ module Bard
129
167
  def drop
130
168
  ScratchRecord.remove_connection
131
169
  return if database.sqlite?
132
- server = database.postgresql? ? config.merge("database" => "postgres") : config.except("database")
133
- ScratchRecord.establish_connection(server.to_h)
134
- ScratchRecord.connection_pool.with_connection { |connection| connection.drop_database(config["database"]) }
135
- ensure
136
- ScratchRecord.remove_connection
170
+ with_server { |connection| connection.drop_database(name) }
137
171
  end
138
172
  end
139
173
  end
@@ -1,6 +1,6 @@
1
1
  module Bard
2
2
  class Backup
3
- VERSION = "0.17.0"
3
+ VERSION = "0.17.1"
4
4
  end
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bard-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel