redash_backup 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: c0cead584b386318481ca5959092d07db19071cd30aac6d49ab6ee2db046370e
4
- data.tar.gz: 32a1b925ef5a0ea7c7fc59cf7f5189e6bcf1cacc9a68a5f9f89d6f3bcd7a812b
3
+ metadata.gz: c8d3bca0628ca753fa535fc28d092e356afad31b6af555a1cf5c3fc9f81266aa
4
+ data.tar.gz: 82d03079e01fcff6efe875e94498f9bee3a0e20a6db56371ae870784cdaeb663
5
5
  SHA512:
6
- metadata.gz: 909cf334f0ea9c301d6dd2d1dbd75e56ab51e5aabab8c2bbae400f9a7161ab572de27501d1ae693ac6fd2b8d324f9690dfc04e919cdbac4356b30320ec424fae
7
- data.tar.gz: a4d37a4a180fdfea844df2755cd0de816cc36b31fc12f1fe332acecf15afdd726d9c375ca288d137b7d9fbfbdb266a8b7b4e9b74fe6142afa2e401c14cb80453
6
+ metadata.gz: 8f9cc4b37d8cd6ca1766ededa603e7afcaad8c24fcc5e97fd6e9a8feb79f53c55c119bae3864a4990add5fe0304dbfc1c8c108da4a510e14754ed45e362563e9
7
+ data.tar.gz: 82bf693e88a6fe29229b1d7c02da7a17a50467acaa38c710819418523ecaa40b838beae0dfdd96efcd3687f71f7fa05acc4f6e3aa20399b189688c919004c44d
data/CHANGELOG.md CHANGED
@@ -1,2 +1,5 @@
1
1
  # 0.1.1
2
- - init🐣
2
+ - init🐣
3
+
4
+ # 0.1.2
5
+ - export unused queries
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redash_backup (0.1.1)
4
+ redash_backup (0.1.2)
5
5
  activerecord (~> 6.0.2.1)
6
6
  fileutils (~> 0.7.2)
7
7
  pg (~> 1.2.2)
@@ -27,7 +27,7 @@ GEM
27
27
  concurrent-ruby (~> 1.0)
28
28
  minitest (5.14.0)
29
29
  pg (1.2.2)
30
- rake (10.5.0)
30
+ rake (13.0.1)
31
31
  rspec (3.9.0)
32
32
  rspec-core (~> 3.9.0)
33
33
  rspec-expectations (~> 3.9.0)
@@ -54,7 +54,7 @@ DEPENDENCIES
54
54
  bundler (~> 1.17)
55
55
  fileutils (~> 0.7.2)
56
56
  pg (>= 0.18, < 2.0)
57
- rake (~> 10.0)
57
+ rake (~> 13.0)
58
58
  redash_backup!
59
59
  rspec (~> 3.0)
60
60
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # RedashBackup
2
2
 
3
+ Simple Redash's query backup tool
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's Gemfile:
@@ -19,5 +21,18 @@ Or install it yourself as:
19
21
  ## Usage
20
22
 
21
23
  ```
24
+ require 'redash_backup'
22
25
  RedashBackup.new('YOUR REDEDASH DATABASE_URL').execute('EXPORT DIR NAME')
26
+ #
27
+ # export...
28
+ #
29
+ # ./queries/dashboard1/hoge.sql
30
+ # ./queries/dashboard1/page.sql
31
+ # ./queries/dashboard2/poko.sql
32
+ # ./queries/dashboard2/pako.sql
33
+ # ./queries/unused/maru.sql
34
+ # ./queries/unused/batu.sql
35
+ #
36
+ # ※ default export path: './queries'
37
+ #
23
38
  ```
@@ -0,0 +1,58 @@
1
+ class RedashBackup::Base
2
+ def self.export!(dir)
3
+ new(dir).export
4
+ end
5
+
6
+ def initialize(dir)
7
+ @dir = dir
8
+ require 'redash_backup/model'
9
+ end
10
+
11
+ def export
12
+ FileUtils.rm_r(@dir) if Dir.exist? @dir
13
+ Dir.mkdir(@dir)
14
+ boards = Dashboard.active.all.find_each
15
+ boards.each { |board| export_by_board!(board) }
16
+ export_unused_queries!
17
+ end
18
+
19
+ private
20
+
21
+ def export_by_board!(board)
22
+ Dir.mkdir(@dir + '/' + board.name)
23
+ board.widgets.each do |widget|
24
+ query = widget.visualization&.query
25
+ next unless query
26
+
27
+ text = <<~TEXT
28
+ -- #{board.name}
29
+ -- #{query.name}
30
+ -- #{Time.now.strftime('%Y/%m/%d %H:%M')}
31
+
32
+ #{query.query}
33
+ TEXT
34
+ File.open(@dir + "/#{board.name}/#{query.name.gsub('/', '_')}.sql", 'w') do |file|
35
+ file.puts text
36
+ end
37
+ end
38
+ puts "Completed export to #{@dir}/#{board.name}/*.sql"
39
+ end
40
+
41
+ # unused: queries not attach to dashboard
42
+ def export_unused_queries!
43
+ Dir.mkdir(@dir + '/unused')
44
+ Query.inactive.all.find_each do |query|
45
+ text = <<~TEXT
46
+ -- ※ Not attached to dashboard
47
+ -- is_draft: #{query.is_draft}
48
+ -- #{Time.now.strftime('%Y/%m/%d %H:%M')}
49
+
50
+ #{query.query}
51
+ TEXT
52
+ File.open(@dir + "/unused/#{query.name.gsub('/', '_')}.sql", 'w') do |file|
53
+ file.puts text
54
+ end
55
+ end
56
+ puts "Completed export to #{@dir}/unused/*.sql"
57
+ end
58
+ end
@@ -13,6 +13,14 @@ end
13
13
 
14
14
  class Query < Connector
15
15
  scope :active, -> { where(is_draft: false, is_archived: false) }
16
+ scope :inactive, -> do
17
+ left_joins(visualizations: { widgets: :dashboard })
18
+ .where('
19
+ queries.is_archived = false
20
+ AND dashboards.is_draft = false
21
+ AND dashboards.is_archived = false
22
+ ')
23
+ end
16
24
  has_many :visualizations
17
25
  end
18
26
 
@@ -1,3 +1,3 @@
1
1
  class RedashBackup
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/redash_backup.rb CHANGED
@@ -1,35 +1,14 @@
1
1
  require "redash_backup/version"
2
+ require "redash_backup/base"
2
3
  require 'fileutils'
3
4
 
4
5
  class RedashBackup
5
6
  def initialize(database_url)
6
7
  ENV['DATABASE_URL'] = database_url
7
- require "redash_backup/model"
8
8
  end
9
9
 
10
10
  def execute(dir = './queries')
11
- boards = Dashboard.active.all
12
- FileUtils.rm_r(dir) if Dir.exist? dir
13
- Dir.mkdir(dir)
14
- boards.each do |board|
15
- Dir.mkdir(dir + '/' + board.name)
16
- board.widgets.each do |widget|
17
- query = widget.visualization&.query
18
- next unless query
19
-
20
- text = <<~TEXT
21
- -- #{board.name}
22
- -- #{query.name}
23
- -- #{Time.now.strftime('%Y/%m/%d %H:%M')}
24
-
25
- #{query.query}
26
- TEXT
27
- File.open(dir + "/#{board.name}/#{query.name.gsub('/', '_')}.sql", 'w') do |file|
28
- file.puts text
29
- end
30
- end
31
- puts "Completed export to #{dir}/#{board.name}/*.sql"
32
- end
11
+ RedashBackup::Base.export!(dir)
33
12
  end
34
13
 
35
14
  class Error < StandardError; end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Tashiro"]
10
10
  spec.email = ["g4160hc@gmail.com"]
11
11
 
12
- spec.summary = 'cli for back up redash queries'
12
+ spec.summary = 'Simple Redash\'s query backup tool'
13
13
  spec.description = 'cli for back up redash queries'
14
14
  spec.homepage = 'https://github.com/tashirosota/ruby-redash_backup'
15
15
  spec.license = "MIT"
@@ -38,6 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.add_dependency 'fileutils', '~> 0.7.2'
39
39
 
40
40
  spec.add_development_dependency "bundler", "~> 1.17"
41
- spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rake", "~> 13.0"
42
42
  spec.add_development_dependency "rspec", "~> 3.0"
43
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redash_backup
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
  - Tashiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-02 00:00:00.000000000 Z
11
+ date: 2020-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '13.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '13.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -114,6 +114,7 @@ files:
114
114
  - bin/console
115
115
  - bin/setup
116
116
  - lib/redash_backup.rb
117
+ - lib/redash_backup/base.rb
117
118
  - lib/redash_backup/model.rb
118
119
  - lib/redash_backup/version.rb
119
120
  - redash_backup.gemspec
@@ -142,5 +143,5 @@ requirements: []
142
143
  rubygems_version: 3.0.1
143
144
  signing_key:
144
145
  specification_version: 4
145
- summary: cli for back up redash queries
146
+ summary: Simple Redash's query backup tool
146
147
  test_files: []