arql 0.1.27 → 0.1.32

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: b7294d448effb453450c0af6a679d949918e23be34fa0323b570ac0e53f408e0
4
- data.tar.gz: b8efa518ebd003348f7d5da2659a126bf8d287ae5a6280efb35e0bf105b262ca
3
+ metadata.gz: 48ce3b7b50ccdb8ab338b73e1895b73cd7ebec18ef779c18931603353e709d12
4
+ data.tar.gz: 3a9bf2b11cdf4be30ab58590aedb81ea2944f6341ca26fd2ab584c9a490cb512
5
5
  SHA512:
6
- metadata.gz: acc67b2f93e5321bb45748d356d3eb2c2d76c13134f27d9c9b1983cd9e1d53384e961371f6b859ff08613d1d87de3425202d9fcb3911e8b1a71a286c44a74e1e
7
- data.tar.gz: cd98b20c4da880b46c052de32f69fea7bb567524ad515ee23cd4000b3ebaeb38f5d75c975f4dd22e2f7c30be5f817cd5d6b94e1f244e2479f77e47923166c459
6
+ metadata.gz: 1cb190b2e90d18b08ccc98878b5db934ebb1e008fc4aba6a850fd4e384e917d02f14837700a06aca8bc5bdc7799a8ea6ffd5c737bb38260304f60166db3a63ce
7
+ data.tar.gz: 3b9c6e813b91904cf39bd044c1ac86925802f52d6f0a4e7a7d3f10f2309db812e01cecb0476555bbcb751df73f640c941fe3e307a96c05f9101d854659e53a70
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.1.27)
4
+ arql (0.1.32)
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
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)
@@ -17,6 +25,7 @@ module Arql
17
25
  require "arql/connection"
18
26
  require "arql/definition"
19
27
  @options = options
28
+ App.env = @options.env
20
29
  Connection.open(connect_options)
21
30
  @definition = Definition.new(effective_config)
22
31
  load_initializer!
@@ -35,11 +44,12 @@ module Arql
35
44
 
36
45
  def load_initializer!
37
46
  return unless effective_config[:initializer]
38
- unless File.exists?(effective_config[:initializer])
47
+ initializer_file = File.expand_path(effective_config[:initializer])
48
+ unless File.exists?(initializer_file)
39
49
  STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}"
40
50
  exit(1)
41
51
  end
42
- load(effective_config[:initializer])
52
+ load(initializer_file)
43
53
  end
44
54
 
45
55
  def start_ssh_proxy!
@@ -55,18 +65,30 @@ module Arql
55
65
  end
56
66
 
57
67
  def config
58
- @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
59
69
  end
60
70
 
61
71
  def selected_config
62
72
  if @options.env.present? && !config[@options.env].present?
63
73
  STDERR.puts "Specified ENV `#{@options.env}' not exists"
64
74
  end
65
- config[@options.env]
75
+ if env = @options.env
76
+ config[env]
77
+ else
78
+ {}
79
+ end
66
80
  end
67
81
 
68
82
  def effective_config
69
- @@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
70
92
  end
71
93
 
72
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
@@ -31,7 +31,7 @@ module Arql
31
31
  else
32
32
  nest_level_prompt = "(#{obj}:#{nest_level})"
33
33
  end
34
- "%s#{nest_level_prompt} %s " % [Rainbow('ARQL').red, Rainbow('❯').green]
34
+ "%s#{Rainbow('@').green}%s#{nest_level_prompt} %s " % [Rainbow('ARQL').red, Rainbow(App.prompt).yellow, Rainbow('❯').green]
35
35
  end]
36
36
  end
37
37
  end
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.1.27"
2
+ VERSION = "0.1.32"
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.27
4
+ version: 0.1.32
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-02 00:00:00.000000000 Z
11
+ date: 2020-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2