slonik_migration 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c8936286fac6f081421ce88208bf27c93a3978f
4
+ data.tar.gz: 195d0f1fffca356e1603bc5a87e785e7841d8907
5
+ SHA512:
6
+ metadata.gz: 86e7e0b38c967ff8a44796999adc2246094fed8e948922891d4aa9e28376f8d93a3020b35e74e0eeffbf353cf8c3a8d5bc94292c622b5c6096fbcbd58c8778ca
7
+ data.tar.gz: d2bd91364e1d9b89898fcae73f5efcb5990a05abf87386a94936d2613cf6979a13b3be0c4cbe4928e8c1740483ec39a0a001da05fe83ac5c2ae4a6b4e7c1be57
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ .bundle/
2
+ .yardoc
3
+ .project
4
+ Gemfile.lock
5
+ _yardoc/
6
+ coverage/
7
+ doc/
8
+ pkg/
9
+ spec/reports/
10
+ spec/dummy/db/schema.rb
11
+ spec/dummy/log/*.log
12
+ spec/dummy/tmp/*
13
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.4
4
+ - 2.4.1
5
+ gemfile:
6
+ - gemfiles/rails50.gemfile
7
+ - gemfiles/rails51.gemfile
8
+ before_install:
9
+ - gem install bundler -v 1.15.3
10
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slonik_migration.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Yoshikazu Kaneta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # SlonikMigration
2
+
3
+ A rails migration gem for slony using `slonik_execute_script` command.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.3+
8
+ * active_record 5.0+
9
+ * slony 2.2+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'slonik_migration'
17
+ ```
18
+
19
+ Then execute:
20
+
21
+ $ bundle
22
+
23
+ Generate config file:
24
+
25
+ $ bundle exec rails g slonik_migration:config
26
+
27
+ ## Configuration
28
+
29
+ Edit `config/slonik.yml`.
30
+
31
+ ```yaml
32
+ default: &default
33
+ # slonik command. $SQL is replaced with raw SQL.
34
+ command: slonik_execute_script -c $SQL 1 | sed "s/set id = 1,//" | slonik
35
+
36
+ # slonik command for remote server.
37
+ # command: ssh -p 22 user@192.168.0.1 'slonik_execute_script -c $SQL 1 | sed "s/set id = 1,//" | slonik'
38
+
39
+ # table/sequence owner.
40
+ owner: owner
41
+
42
+ development:
43
+ <<: *default
44
+
45
+ test:
46
+ <<: *default
47
+
48
+ production:
49
+ <<: *default
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Migrate:
55
+
56
+ $ bundle exec rake slonik:db:migrate RAILS_ENV=production
57
+
58
+ Migrate with specify config file:
59
+
60
+ $ bundle exec rake slonik:db:migrate RAILS_ENV=production CONFIG=/path/to/slonik.yml
61
+
62
+ Migration command executes `slonik_execute_command` as the following example:
63
+
64
+ slonik_execute_script -c CREATE\ TABLE\ \"test_tables\"\ \(\"id\"\ bigserial\ primary\ key,\ \"title\"\ text\) 1 | sed "s/set id = 1,//" | slonik
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome at https://github.com/kanety/slonik_migration.
69
+
70
+ ## Reference
71
+
72
+ This gem is inspired by the discussions [here](http://justatheory.com/computers/databases/postgresql/rails_and_slony.html).
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> 5.0.0"
4
+
5
+ # Specify your gem's dependencies in params_keeper_rails.gemspec
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> 5.1.0"
4
+
5
+ # Specify your gem's dependencies in params_keeper_rails.gemspec
6
+ gemspec path: "../"
@@ -0,0 +1,11 @@
1
+ require 'rails/generators'
2
+
3
+ module SlonikMigration
4
+ class ConfigGenerator < Rails::Generators::Base
5
+ source_root File.join(File.dirname(__FILE__), 'templates')
6
+
7
+ def create_config_file
8
+ template 'slonik.yml', 'config/slonik.yml'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ default: &default
2
+ # slonik command. $SQL is replaced with raw SQL.
3
+ command: slonik_execute_script -c $SQL 1 | sed "s/set id = 1,//" | slonik
4
+
5
+ # slonik command for remote server.
6
+ # command: ssh -p 22 user@192.168.0.1 'slonik_execute_script -c $SQL 1 | sed "s/set id = 1,//" | slonik'
7
+
8
+ # table/sequence owner.
9
+ owner: owner
10
+
11
+ development:
12
+ <<: *default
13
+
14
+ test:
15
+ <<: *default
16
+
17
+ production:
18
+ <<: *default
@@ -0,0 +1,34 @@
1
+ require 'yaml'
2
+ require 'shellwords'
3
+
4
+ module SlonikMigration
5
+ class Command
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
10
+ end
11
+
12
+ def execute(sql, options = {})
13
+ command = build(sql, options)
14
+ puts command if ENV['VERBOSE']
15
+ system command
16
+ end
17
+
18
+ private
19
+
20
+ 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) }
24
+ end
25
+
26
+ def replace(key)
27
+ if (var = @config.dig(:variables, key))
28
+ Shellwords.escape(var.to_s)
29
+ else
30
+ "$#{key}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ module SlonikMigration
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'tasks/slonik_migration.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module SlonikMigration
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'slonik_migration/version'
2
+ require 'slonik_migration/railtie' if defined?(Rails)
3
+
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
+ end
@@ -0,0 +1,17 @@
1
+ namespace :slonik do
2
+ namespace :db do
3
+ desc "Migrate database using slonik_execute_script"
4
+ task :migrate => :environment do
5
+ SlonikMigration.patch { Rake::Task["db:migrate"].invoke }
6
+ end
7
+
8
+ namespace :migrate do
9
+ task :up => :environment do
10
+ SlonikMigration.patch { Rake::Task["db:migrate:up"].invoke }
11
+ end
12
+ task :down => :environment do
13
+ SlonikMigration.patch { Rake::Task["db:migrate:down"].invoke }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slonik_migration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slonik_migration"
8
+ spec.version = SlonikMigration::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{A rails migration gem for slony}
12
+ spec.description = %q{A rails migration gem for slony using slonik_execute_script command}
13
+ spec.homepage = "https://github.com/kanety/slonik_migration"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activerecord", ">= 5.0.0"
21
+
22
+ spec.add_development_dependency "rails", ">= 5.0.0"
23
+ spec.add_development_dependency "pg"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec-rails"
26
+ spec.add_development_dependency "simplecov"
27
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slonik_migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 5.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A rails migration gem for slony using slonik_execute_script command
98
+ email:
99
+ - kaneta@sitebridge.co.jp
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - gemfiles/rails50.gemfile
112
+ - gemfiles/rails51.gemfile
113
+ - lib/generators/slonik_migration/config_generator.rb
114
+ - lib/generators/slonik_migration/templates/slonik.yml
115
+ - lib/slonik_migration.rb
116
+ - lib/slonik_migration/command.rb
117
+ - lib/slonik_migration/railtie.rb
118
+ - lib/slonik_migration/version.rb
119
+ - lib/tasks/slonik_migration.rake
120
+ - slonik_migration.gemspec
121
+ homepage: https://github.com/kanety/slonik_migration
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.2.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A rails migration gem for slony
144
+ test_files: []