arql 0.1.28 → 0.1.33

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: a60cf3e7b24cf3967d9e566fbb89011ce12b727bd6ef9f4e6e7b83aa2ef58bfc
4
- data.tar.gz: 06152be12dff3594db513cbca09e8f53d0250d634d05b4a4bf1e1532c593097a
3
+ metadata.gz: 7404f5bdf1c9e60f767884ae022d5176a120dad6cd18b2c9597ed76a88a2c89c
4
+ data.tar.gz: 7dbfe7c5c06c22a0279a1a3005a637a68974973d9bf1962a3d2fad92bd3be912
5
5
  SHA512:
6
- metadata.gz: b2a3f77a0119b6188437187fb23bc8da4d0129f038d325b2112dcc67eb4621484bed0c3cef28bcad6f7d2eb35957c3977ad923cdef667108b2ecfc15a4030296
7
- data.tar.gz: 309d4f09b2b475625e006a66faea1cef2473368bab41933188de5f47cc75bd2b6a9ee47a6c86fa3096f84e118c1cce402be18b65605bf76564dee123c092364f
6
+ metadata.gz: fd8ea1e246a0127800744706f132eefe5079e2f27d76e424896d40cf54a7f64827b0903f0cfeb3e329b57d862a158d11610660c7c93c61fb10d5859b9889ae6c
7
+ data.tar.gz: 9e9b1ff3104f01d5c4a2685eae6e50f5f1f5e1a14ed8af1991676a1beaeb9c8542cb793b3d1dbee7834e8b0824d8651749d730f4ea3ef5ac921292af60ab640e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.1.28)
4
+ arql (0.1.33)
5
5
  activerecord (~> 6.0.3)
6
6
  activesupport (~> 6.0.3)
7
7
  mysql2 (~> 0.5.3)
@@ -29,14 +29,14 @@ GEM
29
29
  tzinfo (~> 1.1)
30
30
  zeitwerk (~> 2.2, >= 2.2.2)
31
31
  byebug (11.1.3)
32
- coderay (1.1.2)
33
- concurrent-ruby (1.1.6)
34
- i18n (1.8.2)
32
+ coderay (1.1.3)
33
+ concurrent-ruby (1.1.7)
34
+ i18n (1.8.5)
35
35
  concurrent-ruby (~> 1.0)
36
36
  method_source (1.0.0)
37
37
  minitest (5.14.1)
38
38
  mysql2 (0.5.3)
39
- net-ssh (6.0.2)
39
+ net-ssh (6.1.0)
40
40
  net-ssh-gateway (2.0.0)
41
41
  net-ssh (>= 4.0.0)
42
42
  pry (0.13.1)
@@ -59,7 +59,7 @@ GEM
59
59
  thread_safe (~> 0.1)
60
60
  unicode-display_width (1.7.0)
61
61
  yard (0.9.25)
62
- zeitwerk (2.3.0)
62
+ zeitwerk (2.4.0)
63
63
 
64
64
  PLATFORMS
65
65
  ruby
@@ -4,11 +4,19 @@ module Arql
4
4
  class App
5
5
 
6
6
  class << self
7
- attr_accessor :log_io, :env
7
+ attr_accessor :log_io, :env, :prompt
8
8
 
9
9
  def config
10
10
  @@effective_config
11
11
  end
12
+
13
+ def prompt
14
+ if env
15
+ env
16
+ else
17
+ File.basename(@@effective_config[:database])
18
+ end
19
+ end
12
20
  end
13
21
 
14
22
  def initialize(options)
@@ -36,11 +44,12 @@ module Arql
36
44
 
37
45
  def load_initializer!
38
46
  return unless effective_config[:initializer]
39
- unless File.exists?(effective_config[:initializer])
47
+ initializer_file = File.expand_path(effective_config[:initializer])
48
+ unless File.exists?(initializer_file)
40
49
  STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}"
41
50
  exit(1)
42
51
  end
43
- load(effective_config[:initializer])
52
+ load(initializer_file)
44
53
  end
45
54
 
46
55
  def start_ssh_proxy!
@@ -56,18 +65,30 @@ module Arql
56
65
  end
57
66
 
58
67
  def config
59
- @config ||= YAML.load(IO.read(@options.config_file)).with_indifferent_access
68
+ @config ||= YAML.load(IO.read(File.expand_path(@options.config_file))).with_indifferent_access
60
69
  end
61
70
 
62
71
  def selected_config
63
72
  if @options.env.present? && !config[@options.env].present?
64
73
  STDERR.puts "Specified ENV `#{@options.env}' not exists"
65
74
  end
66
- config[@options.env]
75
+ if env = @options.env
76
+ config[env]
77
+ else
78
+ {}
79
+ end
67
80
  end
68
81
 
69
82
  def effective_config
70
- @@effective_config ||= selected_config.deep_merge(@options.to_h)
83
+ @@effective_config ||= nil
84
+ unless @@effective_config
85
+ @@effective_config = selected_config.deep_merge(@options.to_h)
86
+ if @@effective_config[:adapter].blank?
87
+ @@effective_config[:adapter] = 'sqlite3'
88
+ end
89
+ @@effective_config[:database] = File.expand_path(@@effective_config[:database]) if @@effective_config[:adapter] == 'sqlite3'
90
+ end
91
+ @@effective_config
71
92
  end
72
93
 
73
94
  def run!
@@ -10,10 +10,7 @@ module Arql
10
10
  end
11
11
 
12
12
  def parse_options!
13
- @options = OpenStruct.new(adapter: 'mysql2',
14
- encoding: 'utf8',
15
- pool: 5,
16
- config_file: default_config_file,
13
+ @options = OpenStruct.new(config_file: default_config_file,
17
14
  initializer: default_initializer,
18
15
  ssh: {})
19
16
 
@@ -40,7 +37,7 @@ module Arql
40
37
  end
41
38
 
42
39
  opts.on('-aDB_ADAPTER', '--db-adapter=DB_ADAPTER', 'Specify database Adapter, default is mysql2') do |db_adapter|
43
- @options.dapter = db_adapter
40
+ @options.adapter = db_adapter
44
41
  end
45
42
 
46
43
  opts.on('-hDB_HOST', '--db-host=DB_HOST', 'Specify database host') do |db_host|
@@ -2,4 +2,64 @@ module Kernel
2
2
  def sql(sql)
3
3
  ActiveRecord::Base.connection.exec_query(sql)
4
4
  end
5
+
6
+ def print_tables(format = :md)
7
+ require 'terminal-table'
8
+
9
+ tables = ActiveRecord::Base.connection.tables.map do |table_name|
10
+ {
11
+ table: table_name,
12
+ table_comment: ActiveRecord::Base.connection.table_comment(table_name) || '',
13
+ columns: ::ActiveRecord::Base.connection.columns(table_name)
14
+ }
15
+ end
16
+
17
+ outputs = tables.map do |table|
18
+ table_name = table[:table]
19
+ table_comment = table[:table_comment]
20
+ case format
21
+ when :md
22
+ "# #{table_name} #{table_comment}\n\n" +
23
+ Terminal::Table.new { |t|
24
+ t.headings = ['PK', 'Name', 'SQL Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
25
+ t.rows = table[:columns].map { |column|
26
+ pk = if column.name == ::ActiveRecord::Base.connection.primary_key(table_name)
27
+ 'Y'
28
+ else
29
+ ''
30
+ end
31
+ [pk, "`#{column.name}`", column.sql_type, column.sql_type_metadata.limit || '', column.sql_type_metadata.precision || '',
32
+ column.sql_type_metadata.scale || '', column.default || '', column.null, column.comment || '']
33
+ }
34
+ t.style = {
35
+ border_top: false,
36
+ border_bottom: false,
37
+ border_i: '|'
38
+ }
39
+ }.to_s.lines.map { |l| ' ' + l }.join
40
+ when :org
41
+ "* #{table_name} #{table_comment}\n\n" +
42
+ Terminal::Table.new { |t|
43
+ t.headings = ['PK', 'Name', 'SQL Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
44
+ t.rows = table[:columns].map { |column|
45
+ pk = if column.name == ::ActiveRecord::Base.connection.primary_key(table_name)
46
+ 'Y'
47
+ else
48
+ ''
49
+ end
50
+ [pk, "=#{column.name}=", column.sql_type, column.sql_type_metadata.limit || '', column.sql_type_metadata.precision || '',
51
+ column.sql_type_metadata.scale || '', column.default || '', column.null, column.comment || '']
52
+ }
53
+ t.style = {
54
+ border_top: false,
55
+ border_bottom: false,
56
+ }
57
+ }.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
58
+ when :sql
59
+ "-- Table: #{table_name}\n\n" + ActiveRecord::Base.connection.exec_query("show create table `#{table_name}`").rows.last.last + ';'
60
+ end
61
+ end
62
+
63
+ outputs.each { |out| puts out; puts }
64
+ end
5
65
  end
@@ -7,7 +7,7 @@ module Arql
7
7
  class Repl
8
8
  def initialize
9
9
  Pry.config.prompt = Pry::Prompt.new("", "", prompt)
10
- main_object.pry
10
+ Pry.start
11
11
  end
12
12
 
13
13
  def main_object
@@ -29,9 +29,13 @@ module Arql
29
29
  if obj == main_object && nest_level == 0
30
30
  nest_level_prompt = ''
31
31
  else
32
- nest_level_prompt = "(#{obj}:#{nest_level})"
32
+ nest_level_prompt = if nest_level.zero?
33
+ "(#{obj})"
34
+ else
35
+ "(#{obj}:#{nest_level})"
36
+ end
33
37
  end
34
- "%s#{Rainbow('@').green}%s#{nest_level_prompt} %s " % [Rainbow('ARQL').red, Rainbow(App.env).yellow, Rainbow('❯').green]
38
+ "%s#{Rainbow('@').green}%s#{nest_level_prompt} %s " % [Rainbow('ARQL').red, Rainbow(App.prompt).yellow, Rainbow('❯').green]
35
39
  end]
36
40
  end
37
41
  end
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.1.28"
2
+ VERSION = "0.1.33"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.28
4
+ version: 0.1.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2020-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2