waterpig 0.8.1 → 0.9.0

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
  SHA1:
3
- metadata.gz: 318ff8fa9afe73198a7e40f033a80df4a7f6e756
4
- data.tar.gz: e0497a4ce25ea9f435bfcdcd51c368e1c49af4be
3
+ metadata.gz: bb3ae7798c87c5797fa48f5f879563361ac7d419
4
+ data.tar.gz: dea722855e6c224e2d976408f6e8c4491fae9db7
5
5
  SHA512:
6
- metadata.gz: ce3aa8b7b4369f071f1a70d91fdcfe07f844c5788940072ad68fdf51891795e213bcfd1e5e162e302588c420748d5cce813d7bb3fa051eeb600be3ff6385697b
7
- data.tar.gz: 275f405d9b585bbae00e209a0cece55684435d6c243d78220525efdee52d058d615c577c7cc2c660cbe27b2a9501d817b78d1fdcb428db979c19476fb5898ef6
6
+ metadata.gz: bca828861118469c32033ab0a9b657d6a13ae9c78b5e3f06a064fc79c0957bf33900ffe1659226eca983e355a11bc2acb1491418837bf335aae45b93e0f6e57b
7
+ data.tar.gz: 36b4b15b43e9f0fc048f94500c77e6eb0fa9db5661e4e31e33f5b55ce768e859d3da3320ae7f0932d5bde98b6bab4791e1c466ad7f18ab370969a9c725815934
@@ -1,16 +1,28 @@
1
1
  require 'database_cleaner'
2
2
 
3
+ require 'waterpig/template-refresh'
4
+ require 'waterpig/request-wait-middleware'
5
+
3
6
  RSpec.configure do |config|
4
7
  #config.use_transactional_fixtures = false
5
8
  DatabaseCleaner.strategy = :transaction
6
9
 
7
10
  config.add_setting :waterpig_exclude_cleaning_key, :default => :manual_database_cleaning
11
+ config.add_setting :waterpig_explicit_cleaning_method_key, :default => :clean_with
12
+
13
+ config.add_setting :waterpig_database_reset_method, :default => :truncation
8
14
  config.add_setting :waterpig_truncation_types, :default => [:feature]
15
+ config.add_setting :waterpig_reset_types, :default => [:feature]
16
+
9
17
  config.add_setting :waterpig_skip_cleaning_types, :default => []
10
18
  config.add_setting :waterpig_exclude_seeds_types, :default => []
19
+
11
20
  config.add_setting :waterpig_database_truncation_config, :default => {:except => %w[spatial_ref_sys]}
12
21
  config.add_setting :waterpig_db_seeds, :default => 'db/seeds.rb'
13
22
 
23
+ config.add_setting :waterpig_test_database_config, :default => :test
24
+ config.add_setting :waterpig_test_template_database_config, :default => :test_template
25
+
14
26
  def with_showing(show, detailed)
15
27
  begin
16
28
  old_show, old_show_detailed =
@@ -25,18 +37,57 @@ RSpec.configure do |config|
25
37
  end
26
38
  end
27
39
 
40
+ def cleaning_policy(config, metadata)
41
+ return :no_clean if metadata[config.waterpig_exclude_cleaning_key]
42
+ unless (explicit_method = metadata[config.waterpig_explicit_cleaning_method_key]).nil?
43
+ if explicit_method == :reset
44
+ return config.waterpig_database_reset_method
45
+ else
46
+ return explicit_method
47
+ end
48
+ end
49
+
50
+ return :no_clean if config.waterpig_skip_cleaning_types.include?(metadata[:type])
51
+
52
+ if config.waterpig_database_reset_method == :truncation
53
+ return :truncate if config.waterpig_truncation_types.include?(metadata[:type])
54
+ return :truncate if config.waterpig_reset_types.include?(metadata[:type])
55
+ else
56
+ return :refresh if config.waterpig_reset_types.include?(metadata[:type])
57
+ end
58
+ return :transaction
59
+ end
60
+
61
+ def use_refresh?(config, metadata)
62
+ cleaning_policy(config, metadata) == :refresh
63
+ end
64
+
28
65
  def use_truncate?(config, metadata)
29
- return false if metadata[config.waterpig_exclude_cleaning_key]
30
- return false if config.waterpig_skip_cleaning_types.include?(metadata[:type])
31
- return false unless config.waterpig_truncation_types.include?(metadata[:type])
32
- return true
66
+ cleaning_policy(config, metadata) == :truncate
33
67
  end
34
68
 
35
69
  def use_transaction?(config, metadata)
36
- return false if metadata[config.waterpig_exclude_cleaning_key]
37
- return false if use_truncate?(config, metadata)
38
- return false if config.waterpig_skip_cleaning_types.include?(metadata[:type])
39
- return true
70
+ cleaning_policy(config, metadata) == :transaction
71
+ end
72
+
73
+ last_type = nil
74
+
75
+ config.after(:all) do
76
+ last_type = self.class.metadata[:type].to_s
77
+ end
78
+
79
+ config.before(:all, :description => proc{|value, metadata| use_refresh?(config, metadata)}) do
80
+ Waterpig::RequestWaitMiddleware.wait_for_idle do
81
+ Waterpig::TemplateRefresh.refresh_database(config.waterpig_test_database_config)
82
+ end
83
+ end
84
+
85
+ #Before the first group following a refresh-group, refresh again.
86
+ #Faster than refreshing before and after all the feature groups
87
+ config.before(:all, :description => proc{|value, metadata| !use_refresh?(config, metadata) }) do
88
+ if config.waterpig_reset_types.include?(last_type)
89
+ Waterpig::TemplateRefresh.refresh_database(config.waterpig_test_database_config)
90
+ end
40
91
  end
41
92
 
42
93
  config.before :all, :description => proc{ |value, metadata| use_truncate?(config, metadata) } do
@@ -52,7 +103,6 @@ RSpec.configure do |config|
52
103
 
53
104
  config.after :all, :description => proc{ |value, metadata| use_truncate?(config, metadata) } do
54
105
  with_showing(true, true) do
55
- Rails.application.config.action_dispatch.show_detailed_exceptions = true
56
106
  DatabaseCleaner.clean_with :truncation, config.waterpig_database_truncation_config
57
107
  if config.waterpig_db_seeds?
58
108
  load config.waterpig_db_seeds
@@ -69,9 +119,17 @@ RSpec.configure do |config|
69
119
  end
70
120
 
71
121
  config.before :suite do
72
- DatabaseCleaner.clean_with :truncation, config.waterpig_database_truncation_config
73
- if config.waterpig_db_seeds?
74
- load config.waterpig_db_seeds
122
+ if config.waterpig_database_reset_method == :truncation
123
+ DatabaseCleaner.clean_with :truncation, config.waterpig_database_truncation_config
124
+ if config.waterpig_db_seeds?
125
+ load config.waterpig_db_seeds
126
+ end
127
+ elsif config.waterpig_database_reset_method == :refresh
128
+ Waterpig::TemplateRefresh.load_if_pending!(config.waterpig_test_template_database_config)
129
+ Waterpig::TemplateRefresh.commandeer_database(config.waterpig_test_database_config)
130
+ Waterpig::TemplateRefresh.refresh_database(config.waterpig_test_database_config)
131
+ else
132
+ warn "Waterpig: Unknown database reset method: #{config.waterpig_database_reset_method}"
75
133
  end
76
134
  end
77
135
  end
@@ -1,122 +1,126 @@
1
- module Waterpig
2
-
3
-
4
- # Tool to handle migrations and rebuilds on a test template database. For
5
- # explanation of what the test templated DB is and how it's used, see the
6
- # README.
7
- #
8
- # This should auto detect any needed migrations in that database. However, it
9
- # does not detect changes to db/seeds.rb, so if you have changed seeds
10
- # without adding a DB migration, you will need to drop and rebuild the
11
- # test_template.
12
- module TemplateRefresh
13
- Base = ActiveRecord::Base
14
- Migrator = ActiveRecord::Migrator
15
- DatabaseTasks = ActiveRecord::Tasks::DatabaseTasks
16
-
17
- extend self
18
-
19
- def load_config
20
- Base.configurations = DatabaseTasks.database_configuration || {}
21
- Migrator.migrations_paths = DatabaseTasks.migrations_paths
22
- end
1
+ begin
2
+ require 'activerecord'
3
+ module Waterpig
4
+
5
+
6
+ # Tool to handle migrations and rebuilds on a test template database. For
7
+ # explanation of what the test templated DB is and how it's used, see the
8
+ # README.
9
+ #
10
+ # This should auto detect any needed migrations in that database. However, it
11
+ # does not detect changes to db/seeds.rb, so if you have changed seeds
12
+ # without adding a DB migration, you will need to drop and rebuild the
13
+ # test_template.
14
+ module TemplateRefresh
15
+ Base = ActiveRecord::Base
16
+ Migrator = ActiveRecord::Migrator
17
+ DatabaseTasks = ActiveRecord::Tasks::DatabaseTasks
18
+
19
+ extend self
20
+
21
+ def load_config
22
+ Base.configurations = DatabaseTasks.database_configuration || {}
23
+ Migrator.migrations_paths = DatabaseTasks.migrations_paths
24
+ end
23
25
 
24
- def purge_env(env)
25
- DatabaseTasks.purge(config_for(env))
26
- end
26
+ def purge_env(env)
27
+ DatabaseTasks.purge(config_for(env))
28
+ end
27
29
 
28
- def with_temporary_connection(env)
29
- begin
30
- should_reconnect = Base.connection_pool.active_connection?
30
+ def with_temporary_connection(env)
31
+ begin
32
+ should_reconnect = Base.connection_pool.active_connection?
31
33
 
32
- yield
33
- ensure
34
- if should_reconnect
35
- Base.establish_connection(config_for(DatabaseTasks.env))
34
+ yield
35
+ ensure
36
+ if should_reconnect
37
+ Base.establish_connection(config_for(DatabaseTasks.env))
38
+ end
36
39
  end
37
40
  end
38
- end
39
41
 
40
- #Assumes schema_format == ruby
41
- def load_schema(env)
42
- ActiveRecord::Schema.verbose = false
43
- DatabaseTasks.load_schema_for config_for(env), :ruby, ENV['SCHEMA']
44
- end
42
+ #Assumes schema_format == ruby
43
+ def load_schema(env)
44
+ ActiveRecord::Schema.verbose = false
45
+ DatabaseTasks.load_schema_for config_for(env), :ruby, ENV['SCHEMA']
46
+ end
45
47
 
46
- def load_seed
47
- DatabaseTasks.load_seed
48
- # load('spec/test_seeds.rb')
49
- end
48
+ def load_seed
49
+ DatabaseTasks.load_seed
50
+ # load('spec/test_seeds.rb')
51
+ end
50
52
 
51
53
 
52
- def config_for(env)
53
- Base.configurations.fetch(env.to_s)
54
- end
54
+ def config_for(env)
55
+ Base.configurations.fetch(env.to_s)
56
+ end
55
57
 
56
- def connection_for(env)
57
- Base.establish_connection(env).connection
58
- end
58
+ def connection_for(env)
59
+ Base.establish_connection(env).connection
60
+ end
59
61
 
60
- def if_needs_migration(env)
61
- if Migrator.needs_migration?(connection_for(env))
62
- begin
63
- current_config = Base.connection_config
64
- Base.clear_all_connections!
62
+ def if_needs_migration(env)
63
+ if Migrator.needs_migration?(connection_for(env))
64
+ begin
65
+ current_config = Base.connection_config
66
+ Base.clear_all_connections!
65
67
 
66
- yield
68
+ yield
67
69
 
68
- ensure
69
- Base.establish_connection(current_config)
70
+ ensure
71
+ Base.establish_connection(current_config)
70
72
 
71
- ActiveRecord::Migration.check_pending!
73
+ ActiveRecord::Migration.check_pending!
74
+ end
72
75
  end
73
76
  end
74
- end
75
77
 
76
- def ensure_created(env)
77
- Base.establish_connection(env).connection
78
- rescue ActiveRecord::NoDatabaseError
79
- DatabaseTasks.create(config_for(env))
80
- end
78
+ def ensure_created(env)
79
+ Base.establish_connection(env).connection
80
+ rescue ActiveRecord::NoDatabaseError
81
+ DatabaseTasks.create(config_for(env))
82
+ end
81
83
 
82
84
 
83
- def load_if_pending!(env)
84
- ensure_created(env)
85
- if_needs_migration(env) do
86
- puts "Refreshing unmigrated test env: #{env}"
87
- purge_env(env)
88
- with_temporary_connection(env) do
89
- load_schema(env)
90
- load_seed
85
+ def load_if_pending!(env)
86
+ ensure_created(env)
87
+ if_needs_migration(env) do
88
+ puts "Refreshing unmigrated test env: #{env}"
89
+ purge_env(env)
90
+ with_temporary_connection(env) do
91
+ load_schema(env)
92
+ load_seed
93
+ end
91
94
  end
92
95
  end
93
- end
94
96
 
95
- def commandeer_database(env)
96
- config = config_for(env)
97
- connection_for(env).select_all(
98
- "select pid, pg_terminate_backend(pid) " +
99
- "from pg_stat_activity where datname='#{config['database']}' AND state='idle';")
100
- end
97
+ def commandeer_database(env)
98
+ config = config_for(env)
99
+ connection_for(env).select_all(
100
+ "select pid, pg_terminate_backend(pid) " +
101
+ "from pg_stat_activity where datname='#{config['database']}' AND state='idle';")
102
+ end
101
103
 
102
- def refresh_database(env)
103
- Rails.logger.fatal("Resetting test DB...")
104
- config = config_for(env)
104
+ def refresh_database(env)
105
+ Rails.logger.fatal("Resetting test DB...")
106
+ config = config_for(env)
105
107
 
106
- tasks = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(config)
108
+ tasks = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(config)
107
109
 
108
- start = Time.now
109
- Rails.logger.fatal("Dropping")
110
- begin
111
- tasks.drop
112
- end
113
- Rails.logger.fatal("Creating")
114
- begin
115
- tasks.create
110
+ start = Time.now
111
+ Rails.logger.fatal("Dropping")
112
+ begin
113
+ tasks.drop
114
+ end
115
+ Rails.logger.fatal("Creating")
116
+ begin
117
+ tasks.create
118
+ end
119
+ message = "Test database recopied in #{Time.now - start}s"
120
+ #puts message
121
+ Rails.logger.fatal(message)
116
122
  end
117
- message = "Test database recopied in #{Time.now - start}s"
118
- #puts message
119
- Rails.logger.fatal(message)
120
123
  end
121
124
  end
125
+ rescue LoadError
122
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waterpig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Judson Lester
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-12 00:00:00.000000000 Z
12
+ date: 2015-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
@@ -105,7 +105,7 @@ rdoc_options:
105
105
  - --main
106
106
  - doc/README
107
107
  - --title
108
- - waterpig-0.8.1 Documentation
108
+ - waterpig-0.9.0 Documentation
109
109
  require_paths:
110
110
  - lib/
111
111
  required_ruby_version: !ruby/object:Gem::Requirement