Skip to content

Commit

Permalink
Merge pull request #4 from meri025/feature/add_logger
Browse files Browse the repository at this point in the history
Add Logger to Output Query Logs
  • Loading branch information
genya0407 authored Jul 4, 2024
2 parents 1dba90c + b8eb074 commit 93e520d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ MysqlRewinder.setup(db_configs, adapter: :mysql2)
If you want to use MysqlRewinder with ActiveRecord, do the following:

* Generate db_configs from `ActiveRecord::Base.configurations`
* Pass `ActiveRecord::SchemaMigration.new(nil).table_name` and `ActiveRecord::Base.internal_metadata_table_name` to `DatabaseRewinder.setup` as `except_tables`
* Pass `ActiveRecord::SchemaMigration.new(nil).table_name` and `ActiveRecord::Base.internal_metadata_table_name` to `MysqlRewinder.setup` as `except_tables`

```ruby
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: 'test').map(&:configuration_hash)
Expand All @@ -105,6 +105,13 @@ except_tables = [
MysqlRewinder.setup(db_configs, except_tables: except_tables)
```

### Logging

If you want to enable logging, specify `logger` for `MysqlRewinder.setup`

```ruby
MysqlRewinder.setup(db_configs, logger: Logger.new(STDOUT))

## Contributing

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).
Expand Down
11 changes: 7 additions & 4 deletions lib/mysql_rewinder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
require 'tmpdir'
require 'fileutils'
require 'forwardable'
require 'logger'

class MysqlRewinder
class << self
extend Forwardable
delegate %i[clean clean_all record_inserted_table] => :@instance

def setup(db_configs, except_tables: [], adapter: :trilogy)
@instance = new(db_configs: db_configs, except_tables: except_tables, adapter: adapter)
def setup(db_configs, except_tables: [], adapter: :trilogy, logger: nil)
@instance = new(db_configs: db_configs, except_tables: except_tables, adapter: adapter, logger: logger)
end
end

def initialize(db_configs:, except_tables:, adapter:)
def initialize(db_configs:, except_tables:, adapter:, logger:)
@initialized_pid = Process.pid
@inserted_table_record_dir = Pathname(Dir.tmpdir)
@cleaners = db_configs.map do |db_config|
Cleaner.new(
db_config.transform_keys(&:to_sym),
except_tables: except_tables,
adapter: adapter
adapter: adapter,
logger: logger
)
end
@logger = logger
reset_inserted_tables
end

Expand Down
22 changes: 19 additions & 3 deletions lib/mysql_rewinder/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ class MysqlRewinder
class Cleaner
attr_reader :db_config

def initialize(db_config, except_tables:, adapter:)
def initialize(db_config, except_tables:, adapter:, logger: nil)
@db_config = db_config
@client = Adapter.generate(adapter, db_config.transform_keys(&:to_sym))
@except_tables = except_tables
@logger = logger
end

def clean_all
Expand All @@ -20,8 +21,8 @@ def clean(tables:)
target_tables = (tables - @except_tables) & all_tables
return if target_tables.empty?

@client.execute("SET FOREIGN_KEY_CHECKS = 0;")
@client.execute(target_tables.map { |table| "DELETE FROM #{table}" }.join(';'))
log_and_execute("SET FOREIGN_KEY_CHECKS = 0;")
log_and_execute(target_tables.map { |table| "DELETE FROM #{table}" }.join(';'))
end

def all_tables
Expand All @@ -31,5 +32,20 @@ def all_tables
WHERE TABLE_SCHEMA = DATABASE()
SQL
end

private

def log_and_execute(sql)
return @client.execute(sql) unless @logger&.debug?

start_ts = Time.now
res = @client.execute(sql)
duration = (Time.now - start_ts) * 1000

name = "[MysqlRewinder] Cleaner SQL (#{duration.round(1)}ms)"
msg = "\e[1m\e[30m#{name}\e[0m \e[34m#{sql}\e[0m"
@logger.debug msg
res
end
end
end
5 changes: 3 additions & 2 deletions sig/mysql_rewinder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ class MysqlRewinder
# @inserted_table_record_dir: Pathname
@cleaners: Array[Cleaner]
@inserted_tables: Set[String]
@logger: untyped

def self.setup: (Array[Hash[Symbol,String]] db_configs, ?except_tables: Array[String], ?adapter: ::Symbol) -> void
def self.setup: (Array[Hash[Symbol,String]] db_configs, ?except_tables: Array[String], ?adapter: ::Symbol, ?logger: untyped) -> void
def self.clean_all: () -> void
def self.clean: () -> void
def self.record_inserted_table: (String sql) -> void

def initialize: (db_configs: Array[Hash[Symbol,String]], except_tables: Array[String], adapter: ::Symbol) -> untyped
def initialize: (db_configs: Array[Hash[Symbol,String]], except_tables: Array[String], adapter: ::Symbol, logger: untyped) -> untyped
def record_inserted_table: (String sql) -> void
def reset_inserted_tables: () -> void
def calculate_inserted_tables: () -> void
Expand Down
7 changes: 6 additions & 1 deletion sig/mysql_rewinder/cleaner.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ class MysqlRewinder
@client: Adapter
@except_tables: Array[String]
@all_tables: Array[String]
@logger: untyped

attr_reader db_config: Hash[Symbol,String]

def initialize: (Hash[Symbol,String] db_config, except_tables: Array[String], adapter: Symbol) -> untyped
def initialize: (Hash[Symbol,String] db_config, except_tables: Array[String], adapter: Symbol, ?logger: untyped) -> untyped
def clean_all: () -> void
def clean: (tables: untyped) -> void
def all_tables: () -> void

private

def log_and_execute: (String sql) -> void
end
end

0 comments on commit 93e520d

Please sign in to comment.