migratrix 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,18 @@
1
+ require 'active_support/concern'
1
2
  module Migratrix
2
3
  module Loggable
4
+ extend ActiveSupport::Concern
5
+
3
6
  module ClassMethods
4
7
  def logger
5
- ::Migratrix::Logger.logger
8
+ ::Migratrix::Migratrix.logger
6
9
  end
7
10
  end
8
11
 
9
- def logger
10
- ::Migratrix::Logger.logger
12
+ module InstanceMethods
13
+ def logger
14
+ ::Migratrix::Migratrix.logger
15
+ end
11
16
  end
12
-
13
17
  end
14
18
  end
@@ -3,7 +3,6 @@ module Migratrix
3
3
  # loaded migration inherits from this class, but hey, duck typing.
4
4
  class Migration
5
5
  include ::Migratrix::Loggable
6
- extend ::Migratrix::Loggable::ClassMethods
7
6
 
8
7
  attr_accessor :options
9
8
 
@@ -1,7 +1,8 @@
1
1
  # Main "App" or Driver class for Migrating. Responsible for loading
2
2
  # and integrating all the parts of a migration.
3
-
4
3
  module Migratrix
4
+ include ::Migratrix::Loggable
5
+
5
6
  def self.migrate!(name, options={})
6
7
  ::Migratrix::Migratrix.migrate(name, options)
7
8
  end
@@ -11,12 +12,11 @@ module Migratrix
11
12
  end
12
13
 
13
14
  def self.logger=(new_logger)
14
- ::Migratrix::Migratrix.logger= new_logger
15
+ ::Migratrix::Migratrix.logger = new_logger
15
16
  end
16
17
 
17
18
  class Migratrix
18
19
  include ::Migratrix::Loggable
19
- extend ::Migratrix::Loggable::ClassMethods
20
20
 
21
21
  def initialize
22
22
  end
@@ -62,6 +62,35 @@ module Migratrix
62
62
  %w(limit where)
63
63
  end
64
64
 
65
+ # ----------------------------------------------------------------------
66
+ # Logger singleton; tries to hook into Rails.logger if it exists (it
67
+ # won't if you log anything during startup because Migratrix is
68
+ # loaded before Rails). To fix this, after rails start up call
69
+ # Migratrix::Migratrix.logger = Rails.logger
70
+ def self.create_logger(stream)
71
+ logger = Logger.new(stream)
72
+ logger.formatter = proc { |severity, datetime, progname, msg|
73
+ "#{severity[0]} #{datetime.strftime('%F %H:%M:%S')}: #{msg}\n"
74
+ }
75
+ logger
76
+ end
77
+
78
+ def self.init_logger
79
+ return Rails.logger if Rails.logger
80
+ @@logger = create_logger($stdout)
81
+ end
82
+
83
+ def self.logger
84
+ @@logger ||= self.init_logger
85
+ end
86
+
87
+ def self.logger=(new_logger)
88
+ @@logger = new_logger
89
+ end
90
+ # ----------------------------------------------------------------------
91
+
92
+
93
+
65
94
  # ----------------------------------------------------------------------
66
95
  # Candidate for exract class? MigrationRegistry?
67
96
  def loaded?(name)
data/lib/migratrix.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'pathname'
2
2
  require 'forwardable'
3
+ require 'logger'
3
4
 
4
5
  module Migratrix
5
6
  APP=Pathname.new(__FILE__).dirname + "migratrix"
@@ -12,7 +13,6 @@ module Migratrix
12
13
  require EXT + 'string_ext'
13
14
  require EXT + 'object_ext'
14
15
  require EXT + 'andand'
15
- require APP + 'logger'
16
16
  require APP + 'loggable'
17
17
  require APP + 'exceptions'
18
18
  require APP + 'migration'
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ # class SomeLoggableThing
4
+ # include Migratrix::Loggable
5
+ # end
6
+
7
+ shared_examples_for "loggable" do
8
+ # let(:loggable) { SomeLoggableThing.new }
9
+ let(:buffer) { StringIO.new }
10
+ let(:logger) { Migratrix::Migratrix.create_logger(buffer) }
11
+
12
+ before do
13
+ Timecop.freeze(Time.local(2011, 6, 28, 3, 14, 15))
14
+ end
15
+
16
+ after do
17
+ Timecop.return
18
+ end
19
+
20
+ it "is loggable" do
21
+ loggable.class.ancestors.should include(Migratrix::Loggable)
22
+ end
23
+
24
+ describe "instance" do
25
+ it "can log" do
26
+ with_logger(logger) do
27
+ loggable.logger.info("This is a test")
28
+ end
29
+ buffer.string.should == "I 2011-06-28 03:14:15: This is a test\n"
30
+ end
31
+ end
32
+ end
@@ -3,10 +3,12 @@ require 'spec_helper'
3
3
  # This migration is embedded in migration_spec.rb to allow testing of
4
4
  # the class methods that specialize subclasses.
5
5
  class Migratrix::TestMigration < Migratrix::Migration
6
-
7
6
  end
8
7
 
9
8
  describe Migratrix::Migration do
9
+ let(:loggable) { Migratrix::TestMigration.new }
10
+ it_should_behave_like "loggable"
11
+
10
12
  describe ".new" do
11
13
  it "does not modify given options hash" do
12
14
  conditions = ["id=? AND approved=?", 42, true]
@@ -99,12 +99,6 @@ describe Migratrix::Migratrix do
99
99
  end
100
100
  end
101
101
 
102
- describe ".logger" do
103
- it "sets up a default logger to stdout" do
104
- migratrix.logger.class.should == ::Migratrix::Logger
105
- end
106
- end
107
-
108
102
  describe ".logger=" do
109
103
  let (:migration) { migratrix.create_migration :marbles }
110
104
  let (:buffer) { StringIO.new }
@@ -115,11 +109,14 @@ describe Migratrix::Migratrix do
115
109
  end
116
110
 
117
111
  it "sets logger globally across all Migratrices, the Migratrix module, Migrators and Models" do
118
- with_logger_streaming_to(buffer) do
119
- Migratrix.logger.stream.should == buffer
120
- Migratrix::Migratrix.logger.stream.should == buffer
121
- migratrix.logger.stream.should == buffer
122
- migration.logger.stream.should == buffer
112
+ logger = Migratrix::Migratrix.create_logger(buffer)
113
+ Migratrix::Migratrix.logger = logger
114
+ with_logger(logger) do
115
+ Migratrix.logger.should == logger
116
+ Migratrix::Migratrix.logger.should == logger
117
+ migratrix.logger.should == logger
118
+ migration.logger.should == logger
119
+ Migratrix::MarblesMigration.logger.should == logger
123
120
  end
124
121
  end
125
122
  end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,7 @@ require 'ruby-debug'
8
8
  require 'rails'
9
9
  require 'timecop'
10
10
  require 'logger'
11
+ require 'active_support/concern'
11
12
 
12
13
  # Requires supporting ruby files with custom matchers and macros, etc,
13
14
  # in spec/support/ and its subdirectories.
@@ -41,22 +42,18 @@ def reset_migratrix!(migratrix)
41
42
  migratrix.registered_migrations.clear
42
43
  end
43
44
 
44
- # Redirect singleton logger to stream and level of our choice, then
45
- # release it after the spec finishes or crashes.
46
- def with_logger_streaming_to(stream, level=Migratrix::Logger::INFO, &block)
45
+ # Redirect singleton logger to logger of our choice, then release it
46
+ # after the spec finishes or crashes.
47
+ def with_logger(logger, &block)
47
48
  begin
48
- old_stream, old_level = Migratrix::Logger.logger.stream, Migratrix::Logger.logger.level
49
- Migratrix::Logger.set_logger(stream, level)
49
+ old_logger = Migratrix::Migratrix.logger
50
+ Migratrix::Migratrix.logger = logger
50
51
  yield
51
52
  ensure
52
- Migratrix::Logger.set_logger old_stream, old_level
53
+ Migratrix::Migratrix.logger = old_logger
53
54
  end
54
55
  end
55
56
 
56
-
57
-
58
-
59
-
60
57
  RSpec.configure do |config|
61
58
  # == Mock Framework
62
59
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: migratrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-14 00:00:00.000000000Z
12
+ date: 2011-10-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: trollop
16
- requirement: &2161683820 !ruby/object:Gem::Requirement
16
+ requirement: &2157814860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2161683820
24
+ version_requirements: *2157814860
25
25
  description: Migratrix, a Rails legacy database migration tool supporting multiple
26
26
  strategies, including arbitrary n-ary migrations (1->n, n->1, n->m), arbitrary inputs
27
27
  and outputs (ActiveRecord, bare SQL, CSV) and migration logging
@@ -39,14 +39,13 @@ files:
39
39
  - lib/migratrix/active_record_migration_helpers.rb
40
40
  - lib/migratrix/exceptions.rb
41
41
  - lib/migratrix/loggable.rb
42
- - lib/migratrix/logger.rb
43
42
  - lib/migratrix/migration.rb
44
43
  - lib/migratrix/migratrix.rb
45
44
  - lib/patches/andand.rb
46
45
  - lib/patches/object_ext.rb
47
46
  - lib/patches/string_ext.rb
48
47
  - spec/fixtures/migrations/marbles_migration.rb
49
- - spec/lib/logger_spec.rb
48
+ - spec/lib/loggable_spec.rb
50
49
  - spec/lib/migration_spec.rb
51
50
  - spec/lib/migrator_spec.rb
52
51
  - spec/lib/migratrix_spec.rb
@@ -1,56 +0,0 @@
1
- module Migratrix
2
- class Logger
3
- attr_accessor :level, :stream
4
-
5
- FATAL = 0
6
- ERROR = 1
7
- WARN = 2
8
- DEBUG = 3
9
- INFO = 4
10
-
11
- @@singleton_instance = new
12
-
13
- def self.logger
14
- @@singleton_instance
15
- end
16
-
17
- def self.set_logger(stream=$stdout, level=INFO)
18
- self.logger.stream=stream
19
- self.logger.level=level
20
- end
21
-
22
- def initialize(stream, level)
23
- # :nocov: SimpleCov can't see into this method.
24
- @stream, @level = stream, level
25
- end
26
-
27
- def fatal(msg)
28
- log(msg, FATAL)
29
- end
30
-
31
- def error(msg)
32
- log(msg, ERROR)
33
- end
34
-
35
- def warn(msg)
36
- log(msg, WARN)
37
- end
38
-
39
- def debug(msg)
40
- log(msg, DEBUG)
41
- end
42
-
43
- def info(msg)
44
- log(msg, INFO)
45
- end
46
-
47
- def log(msg, level)
48
- if level <= @level
49
- @stream.puts "%c %s: %s" % ["FEWDI"[level], Time.now.strftime("%F %T"), msg]
50
- @stream.flush
51
- end
52
- end
53
-
54
- private_class_method :new
55
- end
56
- end
@@ -1,89 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Migratrix::Logger do
4
- let(:logger) { Migratrix::Logger.logger }
5
-
6
- describe "sanity check kitty" do
7
- it "is sanity checked" do
8
- Migratrix::Logger.should_not be_nil
9
- Migratrix::Logger.class.should == Class
10
- end
11
- end
12
-
13
- describe "singleton-ness" do
14
- it "is singleton-y" do
15
- Migratrix::Logger.logger.class.should == Migratrix::Logger
16
- end
17
-
18
- it "cannot be new'ed" do
19
- lambda { Migratrix::Logger.new }.should raise_error(NoMethodError)
20
- end
21
- end
22
-
23
- describe "default" do
24
- it "logs to $stdout" do
25
- logger.stream == $stdout
26
- end
27
- end
28
-
29
-
30
- describe "logging" do
31
- let(:buffer) { StringIO.new }
32
-
33
- before do
34
- Timecop.freeze(Time.local(2011, 6, 28, 3, 14, 15))
35
- end
36
-
37
- after do
38
- Timecop.return
39
- end
40
-
41
- it "formats info message with level and timestamp" do
42
- with_logger_streaming_to(buffer) do
43
- logger.info("Test Message")
44
- buffer.string.should == "I 2011-06-28 03:14:15: Test Message\n"
45
- end
46
- end
47
-
48
- it "formats debug message with level and timestamp" do
49
- with_logger_streaming_to(buffer) do
50
- logger.debug("Test Message")
51
- buffer.string.should == "D 2011-06-28 03:14:15: Test Message\n"
52
- end
53
- end
54
-
55
- it "formats warning message with level and timestamp" do
56
- with_logger_streaming_to(buffer) do
57
- logger.warn("Test Message")
58
- buffer.string.should == "W 2011-06-28 03:14:15: Test Message\n"
59
- end
60
- end
61
-
62
- it "formats error message with level and timestamp" do
63
- with_logger_streaming_to(buffer) do
64
- logger.error("Test Message")
65
- buffer.string.should == "E 2011-06-28 03:14:15: Test Message\n"
66
- end
67
- end
68
-
69
- it "formats fatal message with level and timestamp" do
70
- with_logger_streaming_to(buffer) do
71
- logger.fatal("Test Message")
72
- buffer.string.should == "F 2011-06-28 03:14:15: Test Message\n"
73
- end
74
- end
75
-
76
- it "rejects messages below logger level" do
77
- with_logger_streaming_to(buffer, Migratrix::Logger::ERROR) do
78
- logger.info("Test Message")
79
- logger.debug("Test Message")
80
- logger.warn("Test Message")
81
- buffer.size.should == 0
82
- logger.error("Test Error")
83
- logger.fatal("Test Fatal")
84
- buffer.string.should == "E 2011-06-28 03:14:15: Test Error\nF 2011-06-28 03:14:15: Test Fatal\n"
85
- end
86
- end
87
- end
88
-
89
- end