arql 0.3.6 → 0.3.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26a222ddbbce9305531742f9b9f160406fe36351aaac3026a1c93879de257e35
4
- data.tar.gz: f324c816f59dc993fca822d5482f0ee43cd973f1659abcd2f2fa48b7fd80acd7
3
+ metadata.gz: 3a74b566a319ba4437d0f30010fe958105d8dd0d2c129aa4f8546ca4855ec15e
4
+ data.tar.gz: 97f1813e455787ccd8ef9394cde51cc4a2459769427cae004f89386db77ba528
5
5
  SHA512:
6
- metadata.gz: 5935a72b159a97a09dbdcfa761ba57b4375c7d78a680569ba8cd88a6f95fb9543b9d70e56d62c0973dc6a99a11347d32f87bce5f8b9ea5335c35f3176b9e5ca4
7
- data.tar.gz: 84313b888ed601eafdce1b5e1867ca7580058b575e34de9291c489b4e318966ec169bff22c1d070960f3d1cf55020e8930b2212d78300a331ce122ccc868c87e
6
+ metadata.gz: a6016fbb6da3857724030da6ce875e0a5889f50207026e71af1a9cc12a867165afaf6b9c4f6465750c91b01a0bbbae7f439507bfb9b02d7f1b854227793e7ffb
7
+ data.tar.gz: d4995846d815847ad229ca4bef01ebc722fcf0c9b4575c42f77229e0480b76e78df6a47518e14b10a2ff06c6616dae9e1aa8e962060ab40ee8eaa923e22c056c
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /.idea/
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.3.6)
4
+ arql (0.3.7)
5
5
  activerecord (>= 6.0.3, < 6.2.0)
6
6
  activesupport (~> 6.0.3)
7
7
  caxlsx (~> 3.0.2)
@@ -53,7 +53,7 @@ GEM
53
53
  net-ssh (6.1.0)
54
54
  net-ssh-gateway (2.0.0)
55
55
  net-ssh (>= 4.0.0)
56
- nokogiri (1.13.0-x86_64-darwin)
56
+ nokogiri (1.13.4-x86_64-darwin)
57
57
  racc (~> 1.4)
58
58
  pry (0.13.1)
59
59
  coderay (~> 1.1)
@@ -82,7 +82,7 @@ GEM
82
82
  webrick (1.7.0)
83
83
  yard (0.9.27)
84
84
  webrick (~> 1.7.0)
85
- zeitwerk (2.5.3)
85
+ zeitwerk (2.5.4)
86
86
 
87
87
  PLATFORMS
88
88
  ruby
data/lib/arql/app.rb CHANGED
@@ -29,8 +29,14 @@ module Arql
29
29
  App.env = @options.env
30
30
  App.connect_options = connect_options
31
31
  Connection.open(App.connect_options)
32
+ print "Defining models..."
32
33
  @definition = Definition.new(effective_config)
34
+ print "\u001b[2K"
35
+ puts "\rModels defined"
36
+ print "Running initializers..."
33
37
  load_initializer!
38
+ print "\u001b[2K"
39
+ puts "\rInitializers loaded"
34
40
  App.instance = self
35
41
  end
36
42
 
@@ -2,7 +2,10 @@ module Arql
2
2
  class Connection
3
3
  class << self
4
4
  def open(options)
5
+ print "Establishing DB connection to #{options[:host]}:#{options[:port]}"
5
6
  ActiveRecord::Base.establish_connection(options)
7
+ print "\u001b[2K"
8
+ puts "\rDB connection to #{options[:host]}:#{options[:port]} established\n"
6
9
  $C = ActiveRecord::Base.connection
7
10
  $C.define_singleton_method(:dump) do |filename, no_create_db=false|
8
11
  Arql::Mysqldump.new.dump_database(filename, no_create_db)
@@ -1,5 +1,6 @@
1
1
  require 'arql/concerns'
2
2
  require 'arql/vd'
3
+
3
4
  module Arql
4
5
  module Extension
5
6
  extend ActiveSupport::Concern
@@ -107,18 +108,21 @@ module Arql
107
108
  end
108
109
  @@models = []
109
110
  ActiveRecord::Base.connection.tap do |conn|
110
- conn.tables.each do |table_name|
111
- table_comment = conn.table_comment(table_name)
112
- conn.primary_key(table_name).tap do |pkey|
111
+ tables = conn.tables
112
+ comments = conn.table_comment_of_tables(tables)
113
+ primary_keys = conn.primary_keys_of_tables(tables)
114
+ tables.each do |table_name|
115
+ table_comment = comments[table_name]
116
+ primary_keys[table_name].tap do |pkey|
113
117
  table_name.camelize.tap do |const_name|
114
118
  const_name = 'Modul' if const_name == 'Module'
115
119
  const_name = 'Clazz' if const_name == 'Class'
116
120
  Class.new(::ArqlModel) do
117
121
  include Arql::Extension
118
- if pkey.is_a?(Array)
122
+ if pkey.is_a?(Array) && pkey.size > 1
119
123
  self.primary_keys = pkey
120
124
  else
121
- self.primary_key = pkey
125
+ self.primary_key = pkey&.first
122
126
  end
123
127
  self.table_name = table_name
124
128
  self.inheritance_column = nil
@@ -185,18 +189,29 @@ module Arql
185
189
  end.t
186
190
  end
187
191
  end)
188
- conn.tables.each do |table_name|
189
- table_comment = conn.table_comment(table_name)
190
- conn.primary_key(table_name).tap do |pkey|
192
+
193
+ tables = conn.tables
194
+ if conn.adapter_name == 'Mysql2'
195
+ require 'arql/ext/active_record/connection_adapters/abstract_mysql_adapter'
196
+ comments = conn.table_comment_of_tables(tables)
197
+ primary_keys = conn.primary_keys_of_tables(tables)
198
+ else
199
+ comments = tables.map { |t| [t, conn.table_comment(t)] }.to_h
200
+ primary_keys = tables.map { |t| [t, conn.primary_keys(t)] }.to_h
201
+ end
202
+
203
+ tables.each do |table_name|
204
+ table_comment = comments[table_name]
205
+ primary_keys[table_name].tap do |pkey|
191
206
  table_name.camelize.tap do |const_name|
192
207
  const_name = 'Modul' if const_name == 'Module'
193
208
  const_name = 'Clazz' if const_name == 'Class'
194
209
  Class.new(::ArqlModel) do
195
210
  include Arql::Extension
196
- if pkey.is_a?(Array)
211
+ if pkey.is_a?(Array) && pkey.size > 1
197
212
  self.primary_keys = pkey
198
213
  else
199
- self.primary_key = pkey
214
+ self.primary_key = pkey&.first
200
215
  end
201
216
  self.table_name = table_name
202
217
  self.inheritance_column = nil
@@ -0,0 +1,53 @@
1
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class AbstractMysqlAdapter
6
+
7
+ def extract_schema_qualified_name_of_tables(table_names)
8
+ table_names.map do |string|
9
+ schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/)
10
+ schema, name = nil, schema unless name
11
+ [schema, name]
12
+ end
13
+ end
14
+
15
+ def quoted_scope_of_tables(names = nil)
16
+ extract_schema_qualified_name_of_tables(names).map do |(schema, name)|
17
+ scope = {}
18
+ scope[:schema] = schema ? quote(schema) : "database()"
19
+ scope[:name] = quote(name) if name
20
+ scope
21
+ end
22
+ end
23
+
24
+ def primary_keys_of_tables(table_names) # :nodoc:
25
+ raise ArgumentError unless table_names.present?
26
+
27
+ scopes = quoted_scope_of_tables(table_names)
28
+
29
+ res = query(<<~SQL, "SCHEMA")
30
+ SELECT table_name, column_name
31
+ FROM information_schema.statistics
32
+ WHERE index_name = 'PRIMARY'
33
+ AND (table_schema, table_name) in
34
+ (#{scopes.map { |scope| "(#{scope[:schema]}, #{scope[:name]})" }.join(', ')})
35
+ ORDER BY seq_in_index
36
+ SQL
37
+
38
+ res.group_by(&:first).map { |table, vlaues| [table, vlaues.map(&:last)] }.to_h
39
+ end
40
+
41
+ def table_comment_of_tables(table_names) # :nodoc:
42
+ scopes = quoted_scope_of_tables(table_names)
43
+
44
+ query(<<~SQL, "SCHEMA").presence.try(&:to_h)
45
+ SELECT table_name, table_comment
46
+ FROM information_schema.tables
47
+ WHERE (table_schema, table_name) in
48
+ (#{scopes.map { |scope| "(#{scope[:schema]}, #{scope[:name]})" }.join(', ')})
49
+ SQL
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module PostgreSQL
4
+ module SchemaStatements
5
+ end
6
+ end
7
+ end
8
+ end
@@ -8,9 +8,13 @@ module Arql
8
8
  attr_accessor :config, :ssh_gateway, :local_ssh_proxy_port
9
9
 
10
10
  def connect(config)
11
+ print "Establishing SSH connection to #{config[:host]}:#{config[:port]}"
11
12
  @config = config
12
13
  @ssh_gateway = Net::SSH::Gateway.new(config[:host], config[:user], config.slice(:port, :password).symbolize_keys.merge(keepalive: true, keepalive_interval: 30, loop_wait: 1))
13
14
  @local_ssh_proxy_port = @ssh_gateway.open(config[:forward_host], config[:forward_port], config[:local_port])
15
+ print "\u001b[2K"
16
+ puts "\rSSH connection to #{config[:host]}:#{config[:port]} established"
17
+ @local_ssh_proxy_port
14
18
  end
15
19
 
16
20
  def reconnect
data/lib/arql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
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.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-27 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -252,6 +252,8 @@ files:
252
252
  - lib/arql/connection.rb
253
253
  - lib/arql/definition.rb
254
254
  - lib/arql/ext.rb
255
+ - lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
256
+ - lib/arql/ext/active_record/connection_adapters/postgresql/schema_statements.rb
255
257
  - lib/arql/ext/array.rb
256
258
  - lib/arql/ext/hash.rb
257
259
  - lib/arql/ext/kernel.rb