slonik_migration 1.0.0 → 1.1.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: 1c8936286fac6f081421ce88208bf27c93a3978f
4
- data.tar.gz: 195d0f1fffca356e1603bc5a87e785e7841d8907
3
+ metadata.gz: 0a9bac5e2a100f82b528a7e361791da8861df69c
4
+ data.tar.gz: 4f0d8060ee2efc0e090468e47269dbc1fd83667a
5
5
  SHA512:
6
- metadata.gz: 86e7e0b38c967ff8a44796999adc2246094fed8e948922891d4aa9e28376f8d93a3020b35e74e0eeffbf353cf8c3a8d5bc94292c622b5c6096fbcbd58c8778ca
7
- data.tar.gz: d2bd91364e1d9b89898fcae73f5efcb5990a05abf87386a94936d2613cf6979a13b3be0c4cbe4928e8c1740483ec39a0a001da05fe83ac5c2ae4a6b4e7c1be57
6
+ metadata.gz: 96882aac71029ddd3c17ef983b3701955d0bb6eee73ed333d127b4e1c51a29ddfbcea5638855daa71536a4b07aad32b7b641ff52de2ae485bc9388ba41488412
7
+ data.tar.gz: 71141e836529b0059b2434619ecd1aa4efb9778f4e46f7c0ec055006b15f391bdde0fa6a067b5b214ffc34dabb3f523ef01fc303aff85061c35b9db6c874bae9
data/.travis.yml CHANGED
@@ -1,10 +1,13 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.4
4
- - 2.4.1
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
5
6
  gemfile:
6
7
  - gemfiles/rails50.gemfile
7
8
  - gemfiles/rails51.gemfile
8
- before_install:
9
- - gem install bundler -v 1.15.3
10
- script: bundle exec rspec
9
+ - gemfiles/rails52.gemfile
10
+ script:
11
+ - bundle exec rspec spec/slonik_migration_spec.rb
12
+ - bundle exec rspec spec/slonik_migration_task_spec.rb
13
+
data/README.md CHANGED
@@ -5,7 +5,7 @@ A rails migration gem for slony using `slonik_execute_script` command.
5
5
  ## Dependencies
6
6
 
7
7
  * ruby 2.3+
8
- * active_record 5.0+
8
+ * activerecord 5.0+
9
9
  * slony 2.2+
10
10
 
11
11
  ## Installation
@@ -30,6 +30,9 @@ Edit `config/slonik.yml`.
30
30
 
31
31
  ```yaml
32
32
  default: &default
33
+ # enable migration with slonik
34
+ enabled: true
35
+
33
36
  # slonik command. $SQL is replaced with raw SQL.
34
37
  command: slonik_execute_script -c $SQL 1 | sed "s/set id = 1,//" | slonik
35
38
 
@@ -51,13 +54,21 @@ production:
51
54
 
52
55
  ## Usage
53
56
 
54
- Migrate:
57
+ This gem enhances following rails commands:
58
+
59
+ * db:migrate
60
+ * db:migrate:up
61
+ * db:migrate:down
62
+
63
+ You can use migration command as usual:
64
+
65
+ $ bundle exec rake db:migrate RAILS_ENV=production
55
66
 
56
- $ bundle exec rake slonik:db:migrate RAILS_ENV=production
67
+ If you want to specify config file:
57
68
 
58
- Migrate with specify config file:
69
+ $ bundle exec rake db:migrate RAILS_ENV=production CONFIG=/path/to/slonik.yml
59
70
 
60
- $ bundle exec rake slonik:db:migrate RAILS_ENV=production CONFIG=/path/to/slonik.yml
71
+ ## Execution example
61
72
 
62
73
  Migration command executes `slonik_execute_command` as the following example:
63
74
 
@@ -69,7 +80,7 @@ Bug reports and pull requests are welcome at https://github.com/kanety/slonik_mi
69
80
 
70
81
  ## Reference
71
82
 
72
- This gem is inspired by the discussions [here](http://justatheory.com/computers/databases/postgresql/rails_and_slony.html).
83
+ This gem is inspired by [the discussions](http://justatheory.com/computers/databases/postgresql/rails_and_slony.html).
73
84
 
74
85
  ## License
75
86
 
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "activerecord", "~> 5.0.0"
3
+ gem "rails", "~> 5.0.0"
4
+ gem "pg", "~> 0.18"
4
5
 
5
6
  # Specify your gem's dependencies in params_keeper_rails.gemspec
6
7
  gemspec path: "../"
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "activerecord", "~> 5.1.0"
3
+ gem "rails", "~> 5.1.0"
4
+ gem "pg", "~> 0.18"
4
5
 
5
6
  # Specify your gem's dependencies in params_keeper_rails.gemspec
6
7
  gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.2.0.rc1"
4
+
5
+ # Specify your gem's dependencies in params_keeper_rails.gemspec
6
+ gemspec path: "../"
@@ -1,4 +1,7 @@
1
1
  default: &default
2
+ # enable migration with slonik
3
+ enabled: true
4
+
2
5
  # slonik command. $SQL is replaced with raw SQL.
3
6
  command: slonik_execute_script -c $SQL 1 | sed "s/set id = 1,//" | slonik
4
7
 
@@ -4,9 +4,7 @@ require 'shellwords'
4
4
  module SlonikMigration
5
5
  class Command
6
6
  def initialize
7
- config_file = ENV['CONFIG_FILE'] || 'config/slonik.yml'
8
- env = ENV['RAILS_ENV'] || 'development'
9
- @config = YAML.load(ERB.new(IO.read(config_file)).result)[env].deep_symbolize_keys
7
+ @config = SlonikMigration::Config.load
10
8
  end
11
9
 
12
10
  def execute(sql, options = {})
@@ -18,13 +16,13 @@ module SlonikMigration
18
16
  private
19
17
 
20
18
  def build(sql, target: nil, name: nil, owner: nil)
21
- sql << %Q|; ALTER #{target} "#{name}" OWNER TO #{@config[:owner]}| if target && name && @config[:owner]
22
- @config[:command].gsub(%r{\$SQL}, Shellwords.escape(sql))
23
- .gsub(%r{\$(\w+)}) { replace($1.to_sym) }
19
+ sql << %Q|; ALTER #{target} "#{name}" OWNER TO #{@config.owner}| if target && name && @config.owner
20
+ @config.command.gsub(%r{\$SQL}, Shellwords.escape(sql))
21
+ .gsub(%r{\$(\w+)}) { replace($1) }
24
22
  end
25
23
 
26
24
  def replace(key)
27
- if (var = @config.dig(:variables, key))
25
+ if (var = @config.variables[key])
28
26
  Shellwords.escape(var.to_s)
29
27
  else
30
28
  "$#{key}"
@@ -0,0 +1,25 @@
1
+ require 'ostruct'
2
+
3
+ module SlonikMigration
4
+ class Config
5
+ class << self
6
+ def load
7
+ file = config_file
8
+ env = ENV['RAILS_ENV'] || 'development'
9
+ OpenStruct.new(YAML.load(ERB.new(IO.read(file)).result)[env]).freeze
10
+ end
11
+
12
+ private
13
+
14
+ def config_file
15
+ if ENV['CONFIG_FILE']
16
+ ENV['CONFIG_FILE']
17
+ elsif defined?(Rails)
18
+ Rails.root.join('config/slonik.yml').to_s
19
+ else
20
+ File.join(Dir.pwd, 'config/slonik.yml')
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'command'
2
+
3
+ module SlonikMigration
4
+ class Extension
5
+ class << self
6
+ def overwrite
7
+ ActiveRecord::Migrator.class_eval do
8
+ private
9
+ alias_method :orig_use_transaction?, :use_transaction?
10
+ def use_transaction?(migration)
11
+ false
12
+ end
13
+ end
14
+
15
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
16
+ alias_method :orig_execute, :execute
17
+ def execute(sql, name = nil)
18
+ if sql =~ /^create (table|sequence|view) "([^"]+)"/i
19
+ ret = SlonikMigration::Command.new.execute(sql, target: $1, name: $2)
20
+ raise "failed to execute slonik command." unless ret
21
+ elsif sql =~ /^(create|alter|drop)/i
22
+ ret = SlonikMigration::Command.new.execute(sql)
23
+ raise "failed to execute slonik command." unless ret
24
+ else
25
+ orig_execute sql, name
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def restore
32
+ ActiveRecord::Migrator.class_eval do
33
+ alias_method :use_transaction?, :orig_use_transaction?
34
+ end
35
+
36
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
37
+ alias_method :execute, :orig_execute
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module SlonikMigration
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -2,48 +2,4 @@ require 'slonik_migration/version'
2
2
  require 'slonik_migration/railtie' if defined?(Rails)
3
3
 
4
4
  module SlonikMigration
5
- class << self
6
- def patch
7
- overwrite
8
- yield
9
- ensure
10
- restore
11
- end
12
-
13
- def overwrite
14
- ActiveRecord::Migrator.class_eval do
15
- private
16
- alias_method :orig_use_transaction?, :use_transaction?
17
- def use_transaction?(migration)
18
- false
19
- end
20
- end
21
-
22
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
23
- alias_method :orig_execute, :execute
24
- def execute(sql, name = nil)
25
- require 'slonik_migration/command'
26
- if sql =~ /^create (table|sequence|view) "([^"]+)"/i
27
- ret = SlonikMigration::Command.new.execute(sql, target: $1, name: $2)
28
- raise "failed to execute slonik command." unless ret
29
- elsif sql =~ /^(create|alter|drop)/i
30
- ret = SlonikMigration::Command.new.execute(sql)
31
- raise "failed to execute slonik command." unless ret
32
- else
33
- orig_execute sql, name
34
- end
35
- end
36
- end
37
- end
38
-
39
- def restore
40
- ActiveRecord::Migrator.class_eval do
41
- alias_method :use_transaction?, :orig_use_transaction?
42
- end
43
-
44
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
45
- alias_method :execute, :orig_execute
46
- end
47
- end
48
- end
49
5
  end
@@ -1,16 +1,39 @@
1
+ require 'slonik_migration/config'
2
+ require 'slonik_migration/extension'
3
+
4
+ if SlonikMigration::Config.load.enabled
5
+ namespace :slonik do
6
+ task :migration do
7
+ SlonikMigration::Extension.overwrite
8
+ end
9
+ end
10
+
11
+ Rake::Task["db:migrate"].enhance(["slonik:migration"]) do
12
+ SlonikMigration::Extension.restore
13
+ end
14
+
15
+ Rake::Task["db:migrate:up"].enhance(["slonik:migration"]) do
16
+ SlonikMigration::Extension.restore
17
+ end
18
+
19
+ Rake::Task["db:migrate:down"].enhance(["slonik:migration"]) do
20
+ SlonikMigration::Extension.restore
21
+ end
22
+ end
23
+
1
24
  namespace :slonik do
2
25
  namespace :db do
3
26
  desc "Migrate database using slonik_execute_script"
4
27
  task :migrate => :environment do
5
- SlonikMigration.patch { Rake::Task["db:migrate"].invoke }
28
+ Rake::Task["db:migrate"].invoke
6
29
  end
7
30
 
8
31
  namespace :migrate do
9
32
  task :up => :environment do
10
- SlonikMigration.patch { Rake::Task["db:migrate:up"].invoke }
33
+ Rake::Task["db:migrate:up"].invoke
11
34
  end
12
35
  task :down => :environment do
13
- SlonikMigration.patch { Rake::Task["db:migrate:down"].invoke }
36
+ Rake::Task["db:migrate:down"].invoke
14
37
  end
15
38
  end
16
39
  end
@@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "activerecord", ">= 5.0.0"
20
+ spec.add_dependency "activerecord", ">= 5.0"
21
21
 
22
- spec.add_development_dependency "rails", ">= 5.0.0"
22
+ spec.add_development_dependency "rails", ">= 5.0"
23
23
  spec.add_development_dependency "pg"
24
24
  spec.add_development_dependency "rspec"
25
25
  spec.add_development_dependency "rspec-rails"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slonik_migration
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-13 00:00:00.000000000 Z
11
+ date: 2018-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.0.0
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 5.0.0
26
+ version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 5.0.0
33
+ version: '5.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 5.0.0
40
+ version: '5.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pg
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -110,10 +110,13 @@ files:
110
110
  - Rakefile
111
111
  - gemfiles/rails50.gemfile
112
112
  - gemfiles/rails51.gemfile
113
+ - gemfiles/rails52.gemfile
113
114
  - lib/generators/slonik_migration/config_generator.rb
114
115
  - lib/generators/slonik_migration/templates/slonik.yml
115
116
  - lib/slonik_migration.rb
116
117
  - lib/slonik_migration/command.rb
118
+ - lib/slonik_migration/config.rb
119
+ - lib/slonik_migration/extension.rb
117
120
  - lib/slonik_migration/railtie.rb
118
121
  - lib/slonik_migration/version.rb
119
122
  - lib/tasks/slonik_migration.rake
@@ -137,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
140
  version: '0'
138
141
  requirements: []
139
142
  rubyforge_project:
140
- rubygems_version: 2.5.2.1
143
+ rubygems_version: 2.5.2.2
141
144
  signing_key:
142
145
  specification_version: 4
143
146
  summary: A rails migration gem for slony