caster 0.9.4 → 0.9.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.
data/bin/cast CHANGED
@@ -22,7 +22,8 @@ class Cast < Thor
22
22
  method_option :db, :aliases => '-d'
23
23
  method_option :version, :aliases => '-v'
24
24
  def up path = '.'
25
- Migrator.new(@metadata).migrate_in_dir path, options[:db], options[:version]
25
+ metadata = (Caster.config[:metadata][:database] != nil)? MetadataDatabase.new : MetadataDocument.new
26
+ Migrator.new(metadata).migrate_in_dir path, options[:db], options[:version]
26
27
  puts 'Done.'
27
28
  end
28
29
  end
@@ -20,6 +20,15 @@ module Caster
20
20
  @valid_config_keys = @config.keys
21
21
 
22
22
  @logger = Logger.new STDOUT
23
+ @logger.level = Logger.const_get((@config.has_key?(:log_level))? @config[:log_level].upcase : 'INFO')
24
+
25
+ def self.config
26
+ @config
27
+ end
28
+
29
+ def self.log
30
+ @logger
31
+ end
23
32
 
24
33
  def self.configure opts = {}
25
34
  opts.each do |k, v|
@@ -29,24 +38,14 @@ module Caster
29
38
 
30
39
  def self.configure_with path_to_yaml_file
31
40
  begin
32
- config = YAML.load_file path_to_yaml_file
41
+ configure(YAML.load_file path_to_yaml_file)
33
42
  rescue Errno::ENOENT
34
- puts "YAML configuration file couldn't be found.. using defaults."
35
- return
43
+ Caster.log.warn { "YAML configuration file couldn't be found.. using defaults" }
36
44
  rescue Psych::SyntaxError
37
- puts "YAML configuration file contains invalid syntax.. using defaults."
38
- return
45
+ Caster.log.warn { "YAML configuration file contains invalid syntax.. using defaults" }
39
46
  end
40
- configure(config)
47
+ Caster.log.info { "using configuration: #{@config.inspect}" }
41
48
  end
42
49
 
43
50
  self.configure_with 'caster.yml'
44
-
45
- def self.config
46
- @config
47
- end
48
-
49
- def self.log
50
- @logger
51
- end
52
51
  end
@@ -17,6 +17,7 @@ module Caster
17
17
  @db = db
18
18
  @view = view
19
19
  @query = query
20
+ @ref_docs_cache = {}
20
21
  @block = block
21
22
  end
22
23
 
@@ -47,27 +48,28 @@ module Caster
47
48
 
48
49
  def from scope, query = {}
49
50
  if scope.scan('/').length == 1
50
- Reference.new @db, scope, query
51
+ view = scope
52
+ db = @db
51
53
  else
52
54
  database_name, view = scope.split('/', 2)
53
55
  db = CouchRest.database "http://#{Caster.config[:host]}:#{Caster.config[:port]}/#{database_name}"
54
- Reference.new db, view, query
55
56
  end
57
+ key = [scope, query]
58
+ @ref_docs_cache[key] = db_query(db, view, query)['rows'].map { |rdoc| rdoc['doc'] || rdoc['value'] } unless @ref_docs_cache.has_key? key
59
+ Reference.new @ref_docs_cache[key]
56
60
  end
57
61
 
58
62
  def execute
59
- Caster.log.info "executing query on '#{@db.name}' over '#{@view}' with params #{@query.inspect}"
60
-
61
63
  limit = @query['limit'] || 1.0/0.0
62
64
  if Caster.config[:batch_size] == nil or limit < Caster.config[:batch_size]
63
- execute_batch @db.view(@view, @query)['rows']
65
+ execute_batch db_query(@db, @view, @query)['rows']
64
66
  return
65
67
  end
66
68
 
67
69
  @query['limit'] = Caster.config[:batch_size] + 1
68
70
  saved_docs = 0
69
71
  while saved_docs < limit do
70
- docs = @db.view(@view, @query)['rows']
72
+ docs = db_query(@db, @view, @query)['rows']
71
73
  return if docs.length == 0
72
74
 
73
75
  @query['startkey_docid'] = docs.last['id']
@@ -88,6 +90,11 @@ module Caster
88
90
  end
89
91
 
90
92
  private
93
+ def db_query db, view, params
94
+ Caster.log.info { "fetching from '#{db.name}' over '#{view}' with #{params.inspect}" }
95
+ db.view view, params
96
+ end
97
+
91
98
  def execute_batch docs
92
99
  db_docs_map = Hash.new { |k, v| k[v] = [] }
93
100
 
@@ -102,6 +109,7 @@ module Caster
102
109
  end
103
110
  db_docs_map.each do |db, db_docs|
104
111
  db.bulk_save db_docs
112
+ Caster.log.info { "wrote #{db_docs.length} doc#{(db_docs.length > 1)? 's' : '' } to #{db.name}" }
105
113
  end
106
114
  end
107
115
 
@@ -3,6 +3,10 @@ module Caster
3
3
 
4
4
  class MetadataDatabase
5
5
 
6
+ def desc
7
+ 'external database'
8
+ end
9
+
6
10
  def get_db_version database
7
11
  db = CouchRest.database! "http://#{Caster.config[:host]}:#{Caster.config[:port]}/#{Caster.config[:metadata][:database]}"
8
12
  begin
@@ -4,6 +4,10 @@ module Caster
4
4
 
5
5
  class MetadataDocument
6
6
 
7
+ def desc
8
+ 'source database'
9
+ end
10
+
7
11
  def get_db_version database
8
12
  db = CouchRest.database! "http://#{Caster.config[:host]}:#{Caster.config[:port]}/#{(Caster.config[:metadata][:database] || database)}"
9
13
  begin
@@ -9,6 +9,7 @@ module Caster
9
9
  def initialize metadata
10
10
  @dbs = {}
11
11
  @metadata = metadata
12
+ Caster.log.info { "using #{metadata.desc} for storing metadata documents." }
12
13
  end
13
14
 
14
15
  def migrate_in_dir path, migrate_database = nil, max_version = nil
@@ -16,7 +17,10 @@ module Caster
16
17
  all_migrations = {}
17
18
 
18
19
  path = path.sub /(\/)+$/, ''
19
- Dir["#{path}/*.cast"].map do |file|
20
+ cast_files = Dir["#{path}/*.cast"]
21
+ Caster.log.info { "found #{cast_files.length} migration scripts under #{path}:\n#{cast_files.to_yaml}" }
22
+
23
+ cast_files.map do |file|
20
24
  migration_version, database = File.basename(file, '.cast').split '.'
21
25
 
22
26
  @dbs[database] = CouchRest.database! "http://#{Caster.config[:host]}:#{Caster.config[:port]}/#{database}" if @dbs[database] == nil
@@ -31,10 +35,13 @@ module Caster
31
35
  end
32
36
 
33
37
  all_migrations.each_pair do |database, migrations|
38
+ Caster.log.info { "#{'<' * 10} starting migrations for database #{database}" }
39
+ Caster.log.info { "current database version: #{migrations.first[:current_version] || 'none' }" }
34
40
 
35
41
  db_filtered = (migrate_database == nil)? migrations.map : migrations.map do |migration|
36
42
  migration if migrate_database == database
37
43
  end.compact
44
+ Caster.log.info { "no migration scripts found for database #{migrate_database}" } if migrate_database != nil and db_filtered.length == 0
38
45
 
39
46
  min_version_filtered = db_filtered.map do |migration|
40
47
  migration if migration[:current_version] == nil or migration[:version] > migration[:current_version]
@@ -47,15 +54,20 @@ module Caster
47
54
  filtered_and_sorted_files = max_version_filtered.map do |migration|
48
55
  migration[:filepath]
49
56
  end.sort
57
+ Caster.log.info {( (filtered_and_sorted_files.length > 0)? "using migration scripts:\n#{ filtered_and_sorted_files.map{ |f| File.basename(f) }.to_yaml }" : "no applicable scripts found for database #{database}" )}
50
58
 
51
59
  filtered_and_sorted_files.each do |file|
52
60
  migrate_file file
53
61
  end
62
+
63
+ Caster.log.info { (max_version_filtered.length > 0)? "#{'>' * 10} finished migrating database #{database} to version #{max_version_filtered.last[:version]}\n" : "did not run any migration on database #{database}\n" }
54
64
  end
55
65
 
56
66
  end
57
67
 
58
68
  def migrate_file path
69
+ Caster.log.info { "#{'<' * 5} executing script #{File.basename(path)}" }
70
+
59
71
  filename = File.basename path, '.cast'
60
72
  version, database = filename.split '.'
61
73
  current_version = @metadata.get_db_version database
@@ -5,13 +5,12 @@ module Caster
5
5
 
6
6
  # to enable passing over method calls on the reference through method_missing
7
7
  Object.instance_methods.each do |m|
8
- undef_method m unless ['__send__', '__id__', 'object_id', 'is_a?'].include? m.to_s
8
+ undef_method m unless %w(__send__ __id__ object_id is_a?).include? m.to_s
9
9
  end
10
10
 
11
- def initialize db_handle, view, query
12
- rdocs = db_handle.view(view, query)['rows']
13
- @docs = rdocs.map { |rdoc| rdoc['doc'] or rdoc['value'] }
14
- @post_eval_calls = []
11
+ def initialize docs
12
+ @docs = docs
13
+ @method_chain = []
15
14
  @accessor = Accessor.new
16
15
  end
17
16
 
@@ -21,7 +20,7 @@ module Caster
21
20
  end
22
21
 
23
22
  def method_missing method_name, *args
24
- @post_eval_calls << [method_name, args]
23
+ @method_chain << [method_name, args]
25
24
  self
26
25
  end
27
26
 
@@ -29,7 +28,7 @@ module Caster
29
28
  @docs.each do |doc|
30
29
  if @predicate.call doc
31
30
  value = @accessor.get doc, @value_field
32
- @post_eval_calls.each do |args|
31
+ @method_chain.each do |args|
33
32
  value = value.send args[0], *args[1]
34
33
  end
35
34
  return value
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caster
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 49
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 4
10
- version: 0.9.4
9
+ - 5
10
+ version: 0.9.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Manohar Akula
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-13 00:00:00 Z
18
+ date: 2013-01-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: couchrest