phobos_db_checkpoint 0.1.1 → 0.2.0

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
  SHA1:
3
- metadata.gz: de27a32446794373ad83b3e24571a326c2d5a6e3
4
- data.tar.gz: 64e6311b8116a8c2032fb6d095fe70808489e626
3
+ metadata.gz: 5b5b26c404a1ef381a22acb11321da68ff796a07
4
+ data.tar.gz: f70ffacba27f60a03be78c40caef88af565d61b0
5
5
  SHA512:
6
- metadata.gz: b179b09d5a525b361a75790818aa51cfb4ce3c0e3314744f91b474f6c3b08e1d590139a8279dfcd3fa590e59a3636495dc6ec11d6e7a1e6d2ceeefca2a36feb1
7
- data.tar.gz: 474b3e6c693da823068125862020544651e140a2256200fa270070070071b4df7ac49a0bfbc72703a1428c981cddd86826d435133f55a0996e4519ff21a54ec4
6
+ metadata.gz: e06985bb11ffc04fb94e00dcbedb158aa423643958e50cef89645e9dbf80c03bc7af391323c1483e32f9770deec18e27b1ba48a10fd1ac4537edcd4995c68a54
7
+ data.tar.gz: f152ff0bc835ee77cb10f9ba5c238eaaef7d33abea0556ded954ed30ea65fe8fe54a727a4672aadf4084ac868c14866894c47ad541a4ba6a09633a1272bcebf5
data/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## 0.2.0 (2016-09-06)
8
+ - [feature] New CLI command to generate migrations #6
9
+ - [feature] Automatically sets database pool size based on listeners max_concurrency #2
10
+
7
11
  ## 0.1.1 (2016-09-02)
8
12
  - [bugfix] Handler is not injecting start and stop methods #4
9
13
 
data/README.md CHANGED
@@ -89,6 +89,13 @@ $ phobos_db_checkpoint copy-migrations
89
89
 
90
90
  This command has no side effects, if the migration is already present it will ignore it.
91
91
 
92
+ You can generate new migrations using the command __migration__, example:
93
+
94
+ ```sh
95
+ phobos_db_checkpoint migration add-new-column
96
+ create db/migrate/20160904200449879052_add_new_column.rb
97
+ ```
98
+
92
99
  ### <a name="handler"></a> Handler
93
100
 
94
101
  In order to use the database checkpointing, your handler should be changed to include `PhobosDBCheckpoint::Handler` instead of `Phobos::Handler`. Phobos DB Checkpoint handler uses the Phobos `around_consume` functionality, which means you need to implement a `#consume` method to handle the event.
@@ -31,11 +31,15 @@ module PhobosDBCheckpoint
31
31
  @db_config_path ||= DEFAULT_DB_CONFIG_PATH
32
32
  configs = YAML.load_file(File.expand_path(@db_config_path))
33
33
  @db_config = configs[env]
34
+
35
+ if Phobos.config
36
+ pool_size = Phobos.config.listeners.map { |listener| listener.max_concurrency || 1 }.inject(&:+)
37
+ @db_config.merge!('pool' => pool_size)
38
+ end
34
39
  end
35
40
 
36
41
  def close_db_connection
37
- connection = ActiveRecord::Base.connection
38
- connection.disconnect! if connection
42
+ ActiveRecord::Base.connection_pool.disconnect!
39
43
  rescue ActiveRecord::ConnectionNotEstablished
40
44
  end
41
45
 
@@ -53,23 +53,40 @@ module PhobosDBCheckpoint
53
53
  end
54
54
  end
55
55
 
56
+ desc 'migration NAME', 'Generates a new migration with the given name. Use underlines (_) as a separator, ex: add_new_column'
57
+ option :destination,
58
+ aliases: ['-d'],
59
+ default: 'db/migrate',
60
+ banner: 'Destination folder relative to your project'
61
+ def migration(name)
62
+ migration_name = name.gsub(/[^\w]*/, '')
63
+ @new_migration_class_name = migration_name.split('_').map(&:capitalize).join('')
64
+ file_name = "#{migration_number}_#{migration_name}.rb"
65
+ destination_fullpath = File.join(destination_root, options[:destination], file_name)
66
+ template(new_migration_template, destination_fullpath)
67
+ end
68
+
56
69
  def self.source_root
57
70
  File.expand_path(File.join(File.dirname(__FILE__), '../..'))
58
71
  end
59
72
 
60
73
  private
74
+
61
75
  def migration_exists?(list, name)
62
76
  list.find { |filename| filename =~ /#{name}/ }
63
77
  end
64
78
 
79
+ def migration_number(index = 0)
80
+ [Time.now.utc.strftime('%Y%m%d%H%M%S%6N'), '%.21d' % index].max
81
+ end
82
+
65
83
  def template_migrations_metadata
66
84
  @template_migrations_metadata ||= begin
67
85
  index = 0
68
86
  template_migrations.map do |path|
69
- number = [Time.now.utc.strftime('%Y%m%d%H%M%S%6N'), '%.21d' % index].max
70
87
  name = path.split('/').last
71
88
  index += 1
72
- {path: path, name: path.gsub(/\.erb$/, ''), number: number}
89
+ {path: path, name: path.gsub(/\.erb$/, ''), number: migration_number(index)}
73
90
  end
74
91
  end
75
92
  end
@@ -90,6 +107,10 @@ module PhobosDBCheckpoint
90
107
  def phobos_boot_template
91
108
  File.join(self.class.source_root, 'templates/phobos_boot.rb')
92
109
  end
110
+
111
+ def new_migration_template
112
+ File.join(self.class.source_root, 'templates/new_migration.rb.erb')
113
+ end
93
114
  end
94
115
  end
95
116
  end
@@ -36,6 +36,10 @@ module PhobosDBCheckpoint
36
36
  else
37
37
  instrument('db_checkpoint.event_skipped', event_metadata)
38
38
  end
39
+ ensure
40
+ # Returns any connections in use by the current thread back to the pool, and also returns
41
+ # connections to the pool cached by threads that are no longer alive.
42
+ ActiveRecord::Base.clear_active_connections!
39
43
  end
40
44
  end
41
45
  end
@@ -11,7 +11,7 @@ module PhobosDBCheckpoint
11
11
 
12
12
  task :load_config do
13
13
  PhobosDBCheckpoint.load_db_config
14
- task_db_config = Hash[PhobosDBCheckpoint.env, PhobosDBCheckpoint.db_config]
14
+ task_db_config = Hash[PhobosDBCheckpoint.env, PhobosDBCheckpoint.db_config.merge('pool' => 1)]
15
15
  ActiveRecord::Tasks::DatabaseTasks.database_configuration = task_db_config
16
16
  end
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module PhobosDBCheckpoint
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,7 @@
1
+ class <%= @new_migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def up
3
+ end
4
+
5
+ def down
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phobos_db_checkpoint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Túlio Ornelas
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2016-09-02 00:00:00.000000000 Z
16
+ date: 2016-09-06 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: bundler
@@ -232,6 +232,7 @@ files:
232
232
  - phobos_db_checkpoint.gemspec
233
233
  - templates/database.yml.example
234
234
  - templates/migrate/phobos_01_create_events.rb.erb
235
+ - templates/new_migration.rb.erb
235
236
  - templates/phobos_boot.rb
236
237
  homepage: https://github.com/klarna/phobos_db_checkpoint
237
238
  licenses:
@@ -254,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
254
255
  version: '0'
255
256
  requirements: []
256
257
  rubyforge_project:
257
- rubygems_version: 2.6.4
258
+ rubygems_version: 2.5.1
258
259
  signing_key:
259
260
  specification_version: 4
260
261
  summary: Phobos DB Checkpoint is a plugin to Phobos and is meant as a drop in replacement