database-switcher 0.0.0 → 0.1.0
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 +4 -4
- data/lib/database-switcher.rb +35 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e46415cd8d2bd4557fa4fc44e9b364c6e512182
|
4
|
+
data.tar.gz: 275a7a8a6690064de6bb4131d1c9635c105b20a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8a08952d364d200af140279b4ba4300c8b1c232f5237235d3d74a1b2946834564d5c737f21b2efb21f152844c59e8ccba8544ca53fe7b21651e148186c68923
|
7
|
+
data.tar.gz: a5ae583cc4e2ca7c385bb76a8b117e0fa5514c4f910de48a8e52b942b471d9d4b5c3275243a43d143d3c9b385999f5f44f952c687d92921663639a02e021108d
|
data/lib/database-switcher.rb
CHANGED
@@ -1,5 +1,38 @@
|
|
1
1
|
class DatabaseSwitcher
|
2
|
-
|
3
|
-
|
2
|
+
attr_reader :database_name, :config_file_path
|
3
|
+
|
4
|
+
def initialize(database_name, config_file_path: 'config/database-switcher.yml')
|
5
|
+
@database_name = database_name
|
6
|
+
@config_file_path = config_file_path
|
7
|
+
@main_db_params = ActiveRecord::Base.configurations[Rails.env]
|
8
|
+
end
|
9
|
+
|
10
|
+
def perform(&block)
|
11
|
+
switch_db_connection
|
12
|
+
return_value = block.call
|
13
|
+
restore_main_db_connection
|
14
|
+
return_value
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def switch_db_connection
|
20
|
+
ActiveRecord::Base.establish_connection(target_db_params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def target_db_params
|
24
|
+
config[database_name]
|
25
|
+
end
|
26
|
+
|
27
|
+
def config
|
28
|
+
@config ||= YAML::load(config_file)
|
29
|
+
end
|
30
|
+
|
31
|
+
def config_file
|
32
|
+
@config_file ||= File.read(config_file_path)
|
33
|
+
end
|
34
|
+
|
35
|
+
def restore_main_db_connection
|
36
|
+
ActiveRecord::Base.establish_connection(@main_db_params)
|
4
37
|
end
|
5
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database-switcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kairui Wang
|
@@ -17,7 +17,7 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/database-switcher.rb
|
20
|
-
homepage:
|
20
|
+
homepage: https://github.com/kairuiwang/database-switcher
|
21
21
|
licenses:
|
22
22
|
- MIT
|
23
23
|
metadata: {}
|