Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logger to Output Query Logs #4

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


```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
25 changes: 22 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,11 @@ 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(';'))
disable_foreign_key_checks = "SET FOREIGN_KEY_CHECKS = 0;"
delete_sql = target_tables.map { |table| "DELETE FROM #{table}" }.join(';')

log_and_execute(disable_foreign_key_checks)
log_and_execute(delete_sql)
meri025 marked this conversation as resolved.
Show resolved Hide resolved
end

def all_tables
Expand All @@ -31,5 +35,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
genya0407 marked this conversation as resolved.
Show resolved Hide resolved

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