ca-data_store-ar 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: 9000bdba1acc0fce352fa58e7d3cf077f9339b26e6fdce73aeaef9dbdadb8d94
4
- data.tar.gz: 4d7cf74286cf189ff73a3d4b0086243099b3e91ace48cd0e16023a1f0fd9d03e
3
+ metadata.gz: b5415c75a20de2e01bf9430ca552bda0d47b88b6ae7710cb8eafe297ab3f4502
4
+ data.tar.gz: 675667a02f7c0e56a44da4ba5b4475c20cf918cbb51dfa9f2b5621c1d1b5bbc2
5
5
  SHA512:
6
- metadata.gz: 0dc057b7b6fa0b4dd873a27d082d515dba440189e60c7777a1c1191f114472f6b04c3d3417ecf73f45bef2904677e76bae6ca9a9072179f99942ffa8b4bd93b3
7
- data.tar.gz: 4c6df09287f859ee8cef2977f8b0c6051dc497441ca1a0740211bcd6c1bea869625ae570dc9f5754de0b25fbdbf5297476aab552127059a68335003abe5bfe07
6
+ metadata.gz: 6c570a7ef638ad54d7a861318702f23f60d5d2feaa940f9a95fcc77f3809e037e518605a7c9e8bd03e14adc077e0512d5f2529375c7a8d89171871a8f5d3c3ef
7
+ data.tar.gz: ca92d6164331ffed80afe3358f2d49e338690094e920a2a9bce0f47795c312db64d1bb9c40b18a154986c03652f0cf5f3126527cb298ea69193f59b4538561dc
data/.release_history.yml CHANGED
@@ -2,3 +2,5 @@
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
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.2)
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/
@@ -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)
@@ -127,7 +128,7 @@ GEM
127
128
  tzinfo (2.0.5)
128
129
  concurrent-ruby (~> 1.0)
129
130
  wisper (2.0.1)
130
- zeitwerk (2.6.0)
131
+ zeitwerk (2.6.6)
131
132
 
132
133
  PLATFORMS
133
134
  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,97 @@
1
+
2
+
3
+
4
+ module Ca
5
+ module DataStore
6
+ module Ar
7
+
8
+ class DomainConfigRegistrar
9
+ include TR::CondUtils
10
+
11
+ CONFIG_FILE_NAME = "cads_ar_domain_config.yml"
12
+
13
+ def self.load_registrar
14
+ path = registrar_storage_path
15
+ logger.debug "Load DCR from : #{path}"
16
+ res = {}
17
+ if File.exist?(path)
18
+ File.open(path, "r") do |f|
19
+ res = YAML.load(f.read)
20
+ end
21
+ end
22
+
23
+ DomainConfigRegistrar.new(res)
24
+ end
25
+
26
+ def self.registrar_storage_path
27
+ # allow caller to overwrite where to store via the env variable
28
+ # CADS_AR_DCR_PATH
29
+ val = ENV[env_key] || File.join(Dir.home,".cads_ar")
30
+ FileUtils.mkdir_p(val) if not File.exist?(val)
31
+ File.join(val, CONFIG_FILE_NAME)
32
+ end
33
+
34
+ def self.logger
35
+ if @logger.nil?
36
+ @logger = TeLogger::Tlogger.new
37
+ @logger.tag = :DCR
38
+ end
39
+ @logger
40
+ end
41
+
42
+ def self.env_key
43
+ "CADS_AR_DCR_PATH".freeze
44
+ end
45
+
46
+ def initialize(rec = { })
47
+ @reg = rec
48
+ if is_empty?(@reg)
49
+ @reg[:default] = "./config/database.yml"
50
+ end
51
+ end
52
+
53
+ def register_domain(domain, path_to_config)
54
+ raise Error, "Domain cannot be empty" if is_empty?(domain)
55
+ raise Error, "Path-to-config cannot be empty" if is_empty?(path_to_config)
56
+
57
+ # path-to-config is expected to be the typical database.yml
58
+ registrar[domain] = path_to_config
59
+ end
60
+
61
+ def is_domain_registered?(domain)
62
+ registrar.keys.include?(domain)
63
+ end
64
+
65
+ def domain_config_path(domain)
66
+ v = registrar[domain]
67
+ if is_empty?(v)
68
+ "./config/database.yml"
69
+ else
70
+ v
71
+ end
72
+ end
73
+
74
+ def save
75
+ path = self.class.registrar_storage_path
76
+ logger.debug "Store DCR to : #{path}"
77
+ File.open(path,"w") do |f|
78
+ f.write YAML.dump(registrar)
79
+ end
80
+ end
81
+
82
+ def registrar
83
+ if @reg.nil?
84
+ @reg = {}
85
+ end
86
+ @reg
87
+ end
88
+
89
+ def logger
90
+ self.class.logger
91
+ end
92
+
93
+ end # DomainConfigRegistrar
94
+
95
+ end
96
+ end
97
+ 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]
@@ -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.2"
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,61 @@ 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
50
+ def Ar.register_domain(domain, path, &block)
51
+ domain_config.register_domain(domain, path)
52
+ domain_config.save
53
+ end
58
54
 
59
- else
60
- path = File.expand_path(confPath)
61
- end
55
+ def Ar.is_domain_registered?(domain)
56
+ domain_config.is_domain_registered?(domain)
57
+ end
58
+
59
+ def Ar.domain_config_path(domain)
60
+ domain_config.domain_config_path(domain)
61
+ end
62
+
63
+ def Ar.connectdb(domain = :default, &block)
64
+ raise Ar::Error, "Domain must be given" if is_empty?(domain)
65
+
66
+ dbConfig = domain_config.domain_config_path(domain)
67
+ dbConfig = File.expand_path(dbConfig) if not_empty?(dbConfig)
62
68
 
63
- if File.exist?(path)
64
- File.open(path,"r") do |f|
69
+ teLogger.debug "dbConfig at : '#{dbConfig}'"
70
+
71
+ if File.exist?(dbConfig)
72
+ File.open(dbConfig,"r") do |f|
65
73
  @conf = YAML.load(f.read)
66
74
  end
67
- ActiveRecord::Base.establish_connection(@conf["development"])
75
+
76
+ if block
77
+ env = block.call(:env)
78
+ end
79
+ env = "development" if is_empty?(env)
80
+
81
+ teLogger.debug "db env : #{env} / #{@conf[env.to_s]}"
82
+ ActiveRecord::Base.establish_connection(@conf[env.to_s])
83
+
68
84
  else
69
- teLogger.warn "Skipped ActiveRecord connection since config file not available"
85
+ teLogger.warn "Skipped ActiveRecord connection since config file not available at '#{path}'"
70
86
  end
71
- end
72
87
 
73
- def Ar.show_log(out = nil)
74
- ActiveRecord::Base.logger = TeLogger::Tlogger.new(out)
75
88
  end
76
89
 
77
- def Ar.migration_path
78
- File.join(File.dirname(__FILE__),"..","..","..","db","migrate")
90
+ def Ar.disconnectdb
91
+ ActiveRecord::Base.remove_connection
92
+ #ActiveRecord::Base.connection_pool.active_connection?.try(:disconnect)
79
93
  end
80
94
 
81
- def Ar.root=(root)
82
- @arRoot = root
95
+ def Ar.show_log(out = nil)
96
+ ActiveRecord::Base.logger = TeLogger::Tlogger.new(out)
83
97
  end
84
98
 
85
- def Ar.root
86
- @arRoot = "." if is_empty?(@arRoot)
87
- @arRoot
99
+ private
100
+ def Ar.domain_config
101
+ if @domainConfig.nil?
102
+ @domainConfig = DomainConfigRegistrar.load_registrar
103
+ end
104
+ @domainConfig
88
105
  end
89
106
 
90
107
  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.2
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-11-12 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