ca-data_store-ar 0.1.1 → 0.1.5

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: 9000bdba1acc0fce352fa58e7d3cf077f9339b26e6fdce73aeaef9dbdadb8d94
4
- data.tar.gz: 4d7cf74286cf189ff73a3d4b0086243099b3e91ace48cd0e16023a1f0fd9d03e
3
+ metadata.gz: 96b72413fe17fd59be9e12cbbfc78bc60ba6c045d3a64a3e6b49473241299369
4
+ data.tar.gz: 34162704c41248c6229e0dcf633dd4a9be1d81f3b85c5e877f891e4c0b77f09e
5
5
  SHA512:
6
- metadata.gz: 0dc057b7b6fa0b4dd873a27d082d515dba440189e60c7777a1c1191f114472f6b04c3d3417ecf73f45bef2904677e76bae6ca9a9072179f99942ffa8b4bd93b3
7
- data.tar.gz: 4c6df09287f859ee8cef2977f8b0c6051dc497441ca1a0740211bcd6c1bea869625ae570dc9f5754de0b25fbdbf5297476aab552127059a68335003abe5bfe07
6
+ metadata.gz: 52eba9ca7040ab4914c428533ae624687b9f8b30242b78e4eea07b761c21e2b275e14a519f747fa40ed95c166a9c984393aa523c5c316180ce01afec1a2cc65f
7
+ data.tar.gz: 9556b9ca0cc92954118ca79dfb1e19145553789370752cd95507b7d9346ab4dc025517c595176384d4a7a14e723a53c24426a4f5515f016e632001a4b5de37e3
data/.release_history.yml CHANGED
@@ -2,3 +2,11 @@
2
2
  ca-data_store-ar:
3
3
  - :version: 0.1.0
4
4
  :timestamp: 1664513882.3107193
5
+ - :version: 0.1.1
6
+ :timestamp: 1664551040.4916718
7
+ - :version: 0.1.2
8
+ :timestamp: 1668260793.5370069
9
+ - :version: 0.1.3
10
+ :timestamp: 1668267700.4040802
11
+ - :version: 0.1.4
12
+ :timestamp: 1668428550.0305433
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ca-data_store-ar (0.1.1)
4
+ ca-data_store-ar (0.1.4)
5
5
  activerecord
6
6
  activesupport
7
7
  ca-data_store
8
8
  standalone_migrations
9
9
  teLogger
10
10
  toolrack
11
+ tty-prompt
11
12
 
12
13
  GEM
13
14
  remote: https://rubygems.org/
@@ -37,12 +38,12 @@ GEM
37
38
  tzinfo (~> 2.0)
38
39
  base58 (0.2.3)
39
40
  builder (3.2.4)
40
- ca-data_store (0.1.0)
41
+ ca-data_store (0.1.1)
41
42
  teLogger
42
43
  toolrack
43
44
  concurrent-ruby (1.1.10)
44
45
  crass (1.0.6)
45
- devops_assist (0.3.0)
46
+ devops_assist (0.3.1)
46
47
  git_cli
47
48
  git_cli_prompt
48
49
  gvcs
@@ -68,7 +69,7 @@ GEM
68
69
  nokogiri (>= 1.5.9)
69
70
  method_source (1.0.0)
70
71
  minitest (5.16.3)
71
- nokogiri (1.13.8-x86_64-linux)
72
+ nokogiri (1.13.9-x86_64-linux)
72
73
  racc (~> 1.4)
73
74
  pastel (0.8.0)
74
75
  tty-color (~> 0.5)
@@ -90,30 +91,28 @@ GEM
90
91
  thor (~> 1.0)
91
92
  zeitwerk (~> 2.5)
92
93
  rake (13.0.6)
93
- rspec (3.11.0)
94
- rspec-core (~> 3.11.0)
95
- rspec-expectations (~> 3.11.0)
96
- rspec-mocks (~> 3.11.0)
97
- rspec-core (3.11.0)
98
- rspec-support (~> 3.11.0)
99
- rspec-expectations (3.11.0)
94
+ rspec (3.12.0)
95
+ rspec-core (~> 3.12.0)
96
+ rspec-expectations (~> 3.12.0)
97
+ rspec-mocks (~> 3.12.0)
98
+ rspec-core (3.12.0)
99
+ rspec-support (~> 3.12.0)
100
+ rspec-expectations (3.12.0)
100
101
  diff-lcs (>= 1.2.0, < 2.0)
101
- rspec-support (~> 3.11.0)
102
- rspec-mocks (3.11.1)
102
+ rspec-support (~> 3.12.0)
103
+ rspec-mocks (3.12.0)
103
104
  diff-lcs (>= 1.2.0, < 2.0)
104
- rspec-support (~> 3.11.0)
105
- rspec-support (3.11.0)
106
- sqlite3 (1.4.4)
105
+ rspec-support (~> 3.12.0)
106
+ rspec-support (3.12.0)
107
+ sqlite3 (1.5.3-x86_64-linux)
107
108
  standalone_migrations (7.1.0)
108
109
  activerecord (>= 4.2.7, < 7.1.0, != 5.2.3.rc1, != 5.2.3)
109
110
  railties (>= 4.2.7, < 7.1.0, != 5.2.3.rc1, != 5.2.3)
110
111
  rake (>= 10.0)
111
112
  teLogger (0.2.0)
112
113
  thor (1.2.1)
113
- tlogger (0.26.3)
114
- toolrack (0.18.6)
114
+ toolrack (0.19.1)
115
115
  base58
116
- tlogger
117
116
  tty-color (0.6.0)
118
117
  tty-cursor (0.7.1)
119
118
  tty-prompt (0.23.1)
@@ -127,7 +126,7 @@ GEM
127
126
  tzinfo (2.0.5)
128
127
  concurrent-ruby (~> 1.0)
129
128
  wisper (2.0.1)
130
- zeitwerk (2.6.0)
129
+ zeitwerk (2.6.6)
131
130
 
132
131
  PLATFORMS
133
132
  x86_64-linux
@@ -40,6 +40,8 @@ Gem::Specification.new do |spec|
40
40
 
41
41
  spec.add_dependency 'ca-data_store'
42
42
 
43
+ spec.add_dependency 'tty-prompt'
44
+
43
45
  spec.add_development_dependency "sqlite3"
44
46
  spec.add_development_dependency "devops_assist"
45
47
 
@@ -0,0 +1,87 @@
1
+
2
+
3
+
4
+ module Ca
5
+ module DataStore
6
+ module Ar
7
+
8
+ class DomainConfigRegistrar
9
+ include TR::CondUtils
10
+
11
+ include TeLogger::TeLogHelper
12
+ teLogger_tag :DomConfReg
13
+
14
+ CONFIG_FILE_NAME = "cads_ar_domain_config.yml"
15
+ CONFIG_FOLDER = ".cads_ar"
16
+ ENV_KEY = "CADS_AR_DCR_PATH".freeze
17
+
18
+ def self.load_registrar
19
+ path = registrar_storage_path
20
+ teLogger.debug "Load DCR from : #{path}"
21
+ res = {}
22
+ if File.exist?(path)
23
+ File.open(path, "r") do |f|
24
+ res = YAML.load(f.read)
25
+ end
26
+ end
27
+
28
+ DomainConfigRegistrar.new(res)
29
+ end
30
+
31
+ def self.registrar_storage_path
32
+ # allow caller to overwrite where to store via the env variable
33
+ # CADS_AR_DCR_PATH
34
+ if @storePath.nil?
35
+ val = ENV.fetch(ENV_KEY) { File.join(Dir.home, CONFIG_FOLDER) }
36
+ FileUtils.mkdir_p(val) if not File.exist?(val)
37
+ @storePath = File.join(val, CONFIG_FILE_NAME)
38
+ end
39
+ @storePath
40
+ end
41
+
42
+ def initialize(rec = { })
43
+ @reg = rec
44
+ if is_empty?(@reg)
45
+ @reg[:default] = "./config/database.yml"
46
+ end
47
+ end
48
+
49
+ def register_domain(domain, path_to_config)
50
+ if not_empty?(domain) and not_empty?(path_to_config)
51
+ # path-to-config is expected to be the typical database.yml
52
+ registrar[domain] = path_to_config
53
+ end
54
+ end
55
+
56
+ def deregister_domain(domain)
57
+ registrar.delete(domain) if not_empty?(domain)
58
+ end
59
+
60
+ def is_domain_registered?(domain)
61
+ registrar.keys.include?(domain)
62
+ end
63
+
64
+ def domain_config_path(domain)
65
+ registrar.fetch(domain) { "./config/database.yml" }
66
+ end
67
+
68
+ def save
69
+ path = self.class.registrar_storage_path
70
+ teLogger.debug "Store DCR to : #{path}"
71
+ File.open(path,"w") do |f|
72
+ f.write YAML.dump(registrar)
73
+ end
74
+ end
75
+
76
+ def registrar
77
+ if @reg.nil?
78
+ @reg = {}
79
+ end
80
+ @reg
81
+ end
82
+
83
+ end # DomainConfigRegistrar
84
+
85
+ end
86
+ end
87
+ end
@@ -9,11 +9,6 @@ module Ca
9
9
  include TeLogger::TeLogHelper
10
10
  teLogger_tag :ar_mig
11
11
 
12
- def default_migration_path
13
- ActiveRecord::Migrator.migrations_paths.first.freeze
14
- #'./db/migrate'.freeze
15
- end
16
-
17
12
  def create_db(conf)
18
13
  ActiveRecord::Base.establish_connection(conf)
19
14
 
@@ -21,19 +16,35 @@ module Ca
21
16
  end
22
17
 
23
18
  def migrate(conf)
19
+ teLogger.debug "Migrating : #{conf}"
24
20
  ActiveRecord::Base.establish_connection(conf)
25
21
 
26
- migPath = conf[:migration_spec_path] || File.expand_path(default_migration_path)
27
- raise MigrateError, "Given migration spec path '#{migPath}' does not exist." if not File.exist?(migPath)
28
-
29
- teLogger.debug "Migration spec path : #{migPath}"
30
22
  # for AR <= 4
31
23
  #ActiveRecord::Migrator.migrate(migPath)
32
24
  # for AR >= 5
33
- ActiveRecord::Base.connection.migration_context.migrate
25
+ ctx = ActiveRecord::Base.connection.migration_context
26
+ # only keep the one in the config file
27
+ if not_empty?(conf["migration_path"])
28
+ ctx.migrations_paths.clear
29
+ ctx.migrations_paths << conf["migration_path"]
30
+ end
31
+
32
+ teLogger.debug "Migrations Paths : #{ctx.migrations_paths.inspect}"
33
+ ctx.migrate
34
+ #ActiveRecord::Base.connection.migration_context.migrate
34
35
  dump_schema(conf)
35
36
  end
36
37
 
38
+ # rails has done too many customization on top of
39
+ # ActiverRecord such as supporting engine.
40
+ # Short cuting that is not a wise choice
41
+ def migrate_for_rails(conf)
42
+ # detacting if Rails is present
43
+ if defined?(Rails::Application)
44
+
45
+ end
46
+ end
47
+
37
48
  def dump_schema(conf)
38
49
  ActiveRecord::Base.establish_connection(conf)
39
50
  require 'active_record/schema_dumper'
@@ -52,11 +63,11 @@ module Ca
52
63
 
53
64
  raise MigrateError, "Migration name is required" if is_empty?(name)
54
65
 
55
- #name = "create_#{name.pluralize}"
56
-
57
- migPath = conf[:migration_spec_path] || File.expand_path(default_migration_path)
66
+ migPath = conf["migration_path"] || File.expand_path("./db/migrate")
58
67
  FileUtils.mkdir_p(migPath) if not File.exist?(migPath)
59
68
 
69
+ teLogger.debug "migPath : #{migPath}"
70
+
60
71
  timestamp = Time.now.strftime("%Y%m%d%H%M%S%6N")
61
72
  # rails only take 14 characters as timestamp
62
73
  timestamp = timestamp[0...14]
@@ -10,7 +10,13 @@ module Ca
10
10
  # which in Ruby is totally up to developer
11
11
  #
12
12
  class Model < ActiveRecord::Base
13
- primary_abstract_class
13
+ #p defined?(Rails)
14
+ # remark this will tame rails in production mode
15
+ # BUT error when accessing model derive from here
16
+ # Leaving it here is ok in dev mode
17
+ #if not defined?(Rails)
18
+ primary_abstract_class
19
+ #end
14
20
  end
15
21
 
16
22
  end
@@ -3,7 +3,7 @@
3
3
  module Ca
4
4
  module DataStore
5
5
  module Ar
6
- VERSION = "0.1.1"
6
+ VERSION = "0.1.5"
7
7
  end
8
8
  end
9
9
  end
@@ -20,6 +20,8 @@ require_relative 'ar/migration/migration_helper'
20
20
 
21
21
  require_relative 'ar/model_base'
22
22
 
23
+ require_relative 'ar/domain_config_registrar'
24
+
23
25
  module Ca
24
26
  module DataStore
25
27
  module Ar
@@ -45,46 +47,76 @@ module Ca
45
47
  end
46
48
  end
47
49
 
48
- def Ar.connect_database(confPath = "./config/database.yml")
49
- # check if we are in Rails context
50
- teLogger.debug "Root : #{root}"
51
- if not_empty?(root)
52
- railsConf = File.join(root, "config","database.yml")
53
- if File.exist?(railsConf)
54
- path = File.expand_path(railsConf)
55
- else
56
- path = File.expand_path(confPath)
57
- end
58
-
59
- else
60
- path = File.expand_path(confPath)
50
+ def Ar.register_domain(domain, path, &block)
51
+ if not is_domain_registered?(domain)
52
+ domain_config.register_domain(domain, path)
53
+ domain_config.save
61
54
  end
55
+ end
56
+
57
+ def Ar.update_registered_domain(domain, path, &block)
58
+ domain_config.register_domain(domain, path)
59
+ domain_config.save
60
+ end
61
+
62
+ def Ar.is_domain_registered?(domain)
63
+ domain_config.is_domain_registered?(domain)
64
+ end
65
+
66
+ def Ar.domain_config_path(domain)
67
+ domain_config.domain_config_path(domain)
68
+ end
69
+
70
+ def Ar.connectdb(domain = :default, &block)
71
+ raise Ar::Error, "Domain must be given" if is_empty?(domain)
72
+
73
+ dbConfig = domain_config.domain_config_path(domain)
74
+ dbConfig = File.expand_path(dbConfig) if not_empty?(dbConfig)
62
75
 
63
- if File.exist?(path)
64
- File.open(path,"r") do |f|
76
+ teLogger.debug "dbConfig at : '#{dbConfig}'"
77
+
78
+ if File.exist?(dbConfig)
79
+ File.open(dbConfig,"r") do |f|
65
80
  @conf = YAML.load(f.read)
66
81
  end
67
- ActiveRecord::Base.establish_connection(@conf["development"])
82
+
83
+ if @conf.is_a?(Hash)
84
+
85
+ if block
86
+ env = block.call(:env)
87
+ end
88
+ env = "development" if is_empty?(env)
89
+
90
+ # this assumed the content of the config file is like Rails style
91
+ teLogger.debug "db env : #{env} / #{@conf[env.to_s]}"
92
+ ActiveRecord::Base.establish_connection(@conf[env.to_s])
93
+
94
+ else
95
+ teLogger.debug "Config: #{@conf}"
96
+ ActiveRecord::Base.establish_connection(@conf)
97
+ end
98
+
68
99
  else
69
- teLogger.warn "Skipped ActiveRecord connection since config file not available"
100
+ teLogger.warn "Skipped ActiveRecord connection since config file not available at '#{path}'"
70
101
  end
71
- end
72
102
 
73
- def Ar.show_log(out = nil)
74
- ActiveRecord::Base.logger = TeLogger::Tlogger.new(out)
75
103
  end
76
104
 
77
- def Ar.migration_path
78
- File.join(File.dirname(__FILE__),"..","..","..","db","migrate")
105
+ def Ar.disconnectdb
106
+ ActiveRecord::Base.remove_connection
107
+ #ActiveRecord::Base.connection_pool.active_connection?.try(:disconnect)
79
108
  end
80
109
 
81
- def Ar.root=(root)
82
- @arRoot = root
110
+ def Ar.show_log(out = nil)
111
+ ActiveRecord::Base.logger = TeLogger::Tlogger.new(out)
83
112
  end
84
113
 
85
- def Ar.root
86
- @arRoot = "." if is_empty?(@arRoot)
87
- @arRoot
114
+ private
115
+ def Ar.domain_config
116
+ if @domainConfig.nil?
117
+ @domainConfig = DomainConfigRegistrar.load_registrar
118
+ end
119
+ @domainConfig
88
120
  end
89
121
 
90
122
  end
@@ -4,6 +4,8 @@ require_relative '../lib/ca/data_store/ar'
4
4
 
5
5
  require 'yaml'
6
6
 
7
+ require 'tty/prompt'
8
+
7
9
  class MigHelper
8
10
  extend Ca::DataStore::Ar::MigrationHelper
9
11
  end
@@ -26,22 +28,48 @@ namespace :cads_db_ar do
26
28
  desc "Migrate the database"
27
29
  task :migrate do
28
30
 
29
- confPath = './db/config.yml'
31
+ target = ""
32
+ reg = Ca::DataStore::Ar::DomainConfigRegistrar.load_registrar
33
+ #if reg.length > 1
34
+ begin
35
+
36
+ tty = TTY::Prompt.new
37
+ target = tty.select("Target domain : ") do |m|
38
+ reg.registrar.each do |k,v|
39
+ m.choice k, v
40
+ end
41
+ end
42
+
43
+ rescue TTY::Reader::InputInterrupt
44
+ STDOUT.puts "\nHave a nice day!\n"
45
+ exit(0)
46
+ end
47
+
48
+ #elsif reg.size == 0
49
+ # # default follow rails
50
+ # target = "./config/database.yml"
51
+ #else
52
+ # target = reg.values.first
53
+ #end
54
+
55
+ STDOUT.puts "Loading target from : #{target}"
30
56
 
31
- if not File.exist?(confPath)
57
+ if not File.exist?(target)
32
58
  STDOUT.print "Please provide config file path : "
33
- path = STDIN.gets
34
- confPath = File.expand_path(path)
35
- raise("Given config file '#{confPath}' does not exist") if not File.exist?(confPath)
59
+ path = STDIN.gets.strip
60
+ target = File.expand_path(path)
61
+ raise("Given config file '#{target}' does not exist") if not File.exist?(target)
36
62
  end
37
63
 
38
- env = ENV['AR-ENV'] || 'development'
39
-
40
- File.open(confPath,"r") do |f|
64
+ File.open(target,"r") do |f|
41
65
  @conf = YAML.load(f.read)
42
66
  end
43
67
 
44
- p @conf
68
+ env = tty.select("Environment : ") do |m|
69
+ @conf.keys.each do |k|
70
+ m.choice k, k
71
+ end
72
+ end
45
73
 
46
74
  MigHelper.migrate(@conf[env])
47
75
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ca-data_store-ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-30 00:00:00.000000000 Z
11
+ date: 2022-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: teLogger
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: tty-prompt
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: sqlite3
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -142,11 +156,11 @@ files:
142
156
  - exe/ar-migrate
143
157
  - lib/ca/data_store/ar.rb
144
158
  - lib/ca/data_store/ar/console_helper.rb
159
+ - lib/ca/data_store/ar/domain_config_registrar.rb
145
160
  - lib/ca/data_store/ar/migration/migration_helper.rb
146
161
  - lib/ca/data_store/ar/model_base.rb
147
162
  - lib/ca/data_store/ar/provider.rb
148
163
  - lib/ca/data_store/ar/version.rb
149
- - spec_test.db
150
164
  - tasks/cads_db_ar.rake
151
165
  homepage: ''
152
166
  licenses: []
data/spec_test.db DELETED
File without changes