mysql_rewinder 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc9e1df489b72666d2df9e54a9d3467e008c33729cf9a2cc674a70e258570842
4
- data.tar.gz: d470cfd7cfb5867a21c79c0b45f40480d2d34dbdbe048b78cfcbb751b7a97222
3
+ metadata.gz: 4a2e247f212ee2a165527969e69188036a554e5e812f156adb4f13136b302fbe
4
+ data.tar.gz: 9ca4cf5ad37d8ab1343882e0012ea2c350c4fd6305d043fc2dde901a3ae068dd
5
5
  SHA512:
6
- metadata.gz: ba1ee1138a2d318fd6ebbc1376cb06cd43ea0962a3305c09b59e4e0cd82de72fa7001dc7146bb85cda1bade4fa1b82df0f11a10870e76b1ac8c867edc3ccabc2
7
- data.tar.gz: 93c60a029034ad464c93c36ce7a3bc3fe292cca146e9fbaaad041a7c08b8ccd7093c0324edcff443b449cef197881f4f1bcbaf2375a2b925f67bfbfca6168d55
6
+ metadata.gz: dfe1906efd4de0b8ee5c3eafcb5e102d7fda767f24c9f16da594a19deb081f515281b1c9aec8f6646ae49023271eeb730876f48a6b725f4bbe724d91749e15f2
7
+ data.tar.gz: d7a6f5a4e43799a49270f1b65412a3aebb627e2d313be5ff7842ae0d19db70744a7768607aad8255214cad377d1229cf731d8897e088ad4094b3ec898fab3dbf
data/README.md CHANGED
@@ -89,7 +89,7 @@ MysqlRewinder.setup(db_configs, adapter: :mysql2)
89
89
  If you want to use MysqlRewinder with ActiveRecord, do the following:
90
90
 
91
91
  * Generate db_configs from `ActiveRecord::Base.configurations`
92
- * Pass `ActiveRecord::SchemaMigration.new(nil).table_name` and `ActiveRecord::Base.internal_metadata_table_name` to `DatabaseRewinder.setup` as `except_tables`
92
+ * Pass `ActiveRecord::SchemaMigration.new(nil).table_name` and `ActiveRecord::Base.internal_metadata_table_name` to `MysqlRewinder.setup` as `except_tables`
93
93
 
94
94
  ```ruby
95
95
  db_configs = ActiveRecord::Base.configurations.configs_for(env_name: 'test').map(&:configuration_hash)
@@ -105,6 +105,14 @@ except_tables = [
105
105
  MysqlRewinder.setup(db_configs, except_tables: except_tables)
106
106
  ```
107
107
 
108
+ ### Logging
109
+
110
+ If you want to enable logging, specify `logger` for `MysqlRewinder.setup`
111
+
112
+ ```ruby
113
+ MysqlRewinder.setup(db_configs, logger: Logger.new(STDOUT))
114
+ ```
115
+
108
116
  ## Contributing
109
117
 
110
118
  Bug reports and pull requests are welcome on GitHub at https://github.com/DeNA/mysql_rewinder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/DeNA/mysql_rewinder/blob/trunk/CODE_OF_CONDUCT.md).
@@ -6,10 +6,11 @@ class MysqlRewinder
6
6
  class Cleaner
7
7
  attr_reader :db_config
8
8
 
9
- def initialize(db_config, except_tables:, adapter:)
9
+ def initialize(db_config, except_tables:, adapter:, logger: nil)
10
10
  @db_config = db_config
11
11
  @client = Adapter.generate(adapter, db_config.transform_keys(&:to_sym))
12
12
  @except_tables = except_tables
13
+ @logger = logger
13
14
  end
14
15
 
15
16
  def clean_all
@@ -20,8 +21,8 @@ class MysqlRewinder
20
21
  target_tables = (tables - @except_tables) & all_tables
21
22
  return if target_tables.empty?
22
23
 
23
- @client.execute("SET FOREIGN_KEY_CHECKS = 0;")
24
- @client.execute(target_tables.map { |table| "DELETE FROM #{table}" }.join(';'))
24
+ log_and_execute("SET FOREIGN_KEY_CHECKS = 0;")
25
+ log_and_execute(target_tables.map { |table| "DELETE FROM #{table}" }.join(';'))
25
26
  end
26
27
 
27
28
  def all_tables
@@ -31,5 +32,20 @@ class MysqlRewinder
31
32
  WHERE TABLE_SCHEMA = DATABASE()
32
33
  SQL
33
34
  end
35
+
36
+ private
37
+
38
+ def log_and_execute(sql)
39
+ return @client.execute(sql) unless @logger&.debug?
40
+
41
+ start_ts = Time.now
42
+ res = @client.execute(sql)
43
+ duration = (Time.now - start_ts) * 1000
44
+
45
+ name = "[MysqlRewinder] Cleaner SQL (#{duration.round(1)}ms)"
46
+ msg = "\e[1m\e[30m#{name}\e[0m \e[34m#{sql}\e[0m"
47
+ @logger.debug msg
48
+ res
49
+ end
34
50
  end
35
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MysqlRewinder
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -6,27 +6,30 @@ require 'set'
6
6
  require 'tmpdir'
7
7
  require 'fileutils'
8
8
  require 'forwardable'
9
+ require 'logger'
9
10
 
10
11
  class MysqlRewinder
11
12
  class << self
12
13
  extend Forwardable
13
14
  delegate %i[clean clean_all record_inserted_table] => :@instance
14
15
 
15
- def setup(db_configs, except_tables: [], adapter: :trilogy)
16
- @instance = new(db_configs: db_configs, except_tables: except_tables, adapter: adapter)
16
+ def setup(db_configs, except_tables: [], adapter: :trilogy, logger: nil)
17
+ @instance = new(db_configs: db_configs, except_tables: except_tables, adapter: adapter, logger: logger)
17
18
  end
18
19
  end
19
20
 
20
- def initialize(db_configs:, except_tables:, adapter:)
21
+ def initialize(db_configs:, except_tables:, adapter:, logger:)
21
22
  @initialized_pid = Process.pid
22
23
  @inserted_table_record_dir = Pathname(Dir.tmpdir)
23
24
  @cleaners = db_configs.map do |db_config|
24
25
  Cleaner.new(
25
26
  db_config.transform_keys(&:to_sym),
26
27
  except_tables: except_tables,
27
- adapter: adapter
28
+ adapter: adapter,
29
+ logger: logger
28
30
  )
29
31
  end
32
+ @logger = logger
30
33
  reset_inserted_tables
31
34
  end
32
35
 
@@ -4,12 +4,17 @@ class MysqlRewinder
4
4
  @client: Adapter
5
5
  @except_tables: Array[String]
6
6
  @all_tables: Array[String]
7
+ @logger: untyped
7
8
 
8
9
  attr_reader db_config: Hash[Symbol,String]
9
10
 
10
- def initialize: (Hash[Symbol,String] db_config, except_tables: Array[String], adapter: Symbol) -> untyped
11
+ def initialize: (Hash[Symbol,String] db_config, except_tables: Array[String], adapter: Symbol, ?logger: untyped) -> untyped
11
12
  def clean_all: () -> void
12
13
  def clean: (tables: untyped) -> void
13
14
  def all_tables: () -> void
15
+
16
+ private
17
+
18
+ def log_and_execute: (String sql) -> void
14
19
  end
15
20
  end
@@ -5,13 +5,14 @@ class MysqlRewinder
5
5
  # @inserted_table_record_dir: Pathname
6
6
  @cleaners: Array[Cleaner]
7
7
  @inserted_tables: Set[String]
8
+ @logger: untyped
8
9
 
9
- def self.setup: (Array[Hash[Symbol,String]] db_configs, ?except_tables: Array[String], ?adapter: ::Symbol) -> void
10
+ def self.setup: (Array[Hash[Symbol,String]] db_configs, ?except_tables: Array[String], ?adapter: ::Symbol, ?logger: untyped) -> void
10
11
  def self.clean_all: () -> void
11
12
  def self.clean: () -> void
12
13
  def self.record_inserted_table: (String sql) -> void
13
14
 
14
- def initialize: (db_configs: Array[Hash[Symbol,String]], except_tables: Array[String], adapter: ::Symbol) -> untyped
15
+ def initialize: (db_configs: Array[Hash[Symbol,String]], except_tables: Array[String], adapter: ::Symbol, logger: untyped) -> untyped
15
16
  def record_inserted_table: (String sql) -> void
16
17
  def reset_inserted_tables: () -> void
17
18
  def calculate_inserted_tables: () -> void
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_rewinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Sangenya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-29 00:00:00.000000000 Z
11
+ date: 2024-07-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -33,7 +33,6 @@ files:
33
33
  - lib/mysql_rewinder/ext/mysql2_client.rb
34
34
  - lib/mysql_rewinder/ext/trilogy.rb
35
35
  - lib/mysql_rewinder/version.rb
36
- - mysql_rewinder.gemspec
37
36
  - sig/mysql_rewinder.rbs
38
37
  - sig/mysql_rewinder/cleaner.rbs
39
38
  - sig/mysql_rewinder/cleaner/adapter.rbs
@@ -63,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
62
  - !ruby/object:Gem::Version
64
63
  version: '0'
65
64
  requirements: []
66
- rubygems_version: 3.5.3
65
+ rubygems_version: 3.3.26
67
66
  signing_key:
68
67
  specification_version: 4
69
68
  summary: Simple, stable, and fast database cleaner for mysql
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/mysql_rewinder/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "mysql_rewinder"
7
- spec.version = MysqlRewinder::VERSION
8
- spec.authors = ["Yusuke Sangenya"]
9
- spec.email = ["longinus.eva@gmail.com"]
10
-
11
- spec.summary = "Simple, stable, and fast database cleaner for mysql"
12
- spec.homepage = "https://github.com/genya0407/mysql_rewinder"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = ">= 3.0.0"
15
-
16
- spec.files = Dir.chdir(__dir__) do
17
- `git ls-files -z`.split("\x0").reject do |f|
18
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
19
- end
20
- end
21
- spec.require_paths = ["lib"]
22
- end