sql-migrations-rails 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a91ec2443bcae96416a8d56b8dd94cd9f3cfa1cde4b4b4ef98f5b7c5860a226
4
+ data.tar.gz: efd23554c5a80ebb5388ce25a97b319f85b830c2b3336d4f3e61925723d16ee3
5
+ SHA512:
6
+ metadata.gz: '0656997a2e3ab6ca1cea7ec093072cc32f87a0e42b5254865ceb3400b8441fc159cdaa1204f1912bb81c06ad091e01c7833887b636f031b94ec0801fb22a271e'
7
+ data.tar.gz: 9063fb8ccbf613575a6278ddc5616087197dbef4b2419b22968b11f44e00e74a301e129d9480c694dffb9f2f7a7a0a6697e4ab6ed9f59fb978b600a33a8079ac
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Evgeny Peleh
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,33 @@
1
+ # Sql Migrations Rails
2
+ This gem allows you to write plain SQL migrations without Ruby code.
3
+
4
+ ## Usage
5
+ In the console type:
6
+ ```bash
7
+ $ bin/rails generate sql_migration AddPartNumberToProducts
8
+ ```
9
+
10
+ This will create two empty migration files:
11
+ ```bash
12
+ create db/migrate/20190714142718_add_part_number_to_products.up.sql
13
+ create db/migrate/20190714142718_add_part_number_to_products.down.sql
14
+ ```
15
+
16
+ Fill them with SQL code and run:
17
+ ```bash
18
+ $ bin/rails db:migrate
19
+ ```
20
+
21
+ This works along with common Ruby migrations.
22
+
23
+ ## Installation
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'sql-migrations-rails'
28
+ ```
29
+
30
+ And then execute:
31
+ ```bash
32
+ $ bundle
33
+ ```
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Sql::Migrations::Rails'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SqlMigrationGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ desc 'This generator creates sql migration files at db/migrate'
7
+ def create_sql_migration_files
8
+ say_status :invoke, :active_record, :white
9
+
10
+ raise(ActiveRecord::IllegalMigrationNameError, file_name) unless /^[_a-z0-9]+$/.match?(file_name)
11
+
12
+ timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S')
13
+ up_migration = "db/migrate/#{timestamp}_#{file_name}.up.sql"
14
+ down_migration = "db/migrate/#{timestamp}_#{file_name}.down.sql"
15
+
16
+ existing_migrations = Rails.root.join('db/migrate').glob("*_#{file_name}.{rb,{up,down}.sql}")
17
+ if existing_migrations.empty?
18
+ create_file up_migration
19
+ create_file down_migration
20
+
21
+ elsif existing_migrations.all? { |x| x.to_s.end_with?('.sql') } &&
22
+ existing_migrations.count == 2 &&
23
+ existing_migrations.map(&:read).all?(&:empty?)
24
+
25
+ say_status :identical, up_migration, :blue
26
+ say_status :identical, down_migration, :blue
27
+
28
+ elsif (ARGV & %w[--skip -s]).any?
29
+ say_status :skip, up_migration, :yellow
30
+ say_status :skip, down_migration, :yellow
31
+
32
+ elsif (ARGV & %w[--force -f]).any?
33
+ existing_migrations.each { |file| file.delete; say_status :remove, file, :green }
34
+ create_file up_migration
35
+ create_file down_migration
36
+
37
+ else
38
+ say_status :conflict, up_migration, :red
39
+ say_status :conflict, down_migration, :red
40
+ raise Thor::Error, "Another migration is already named #{file_name}: " \
41
+ "#{existing_migrations.first}. Use --force to replace this migration " \
42
+ 'or --skip to ignore conflicted file.'
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sql/migrations/rails/railtie'
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sql
4
+ module Migrations
5
+ module Rails
6
+ TMP_MIGRATIONS_FOLDER = 'tmp/sql_migrations/migrate'
7
+ LAST_UPDATE_FILE = 'tmp/sql_migrations/last_update.json'
8
+
9
+ class Railtie < ::Rails::Railtie
10
+ initializer :load_sql_migrations do
11
+ delete_extra_migrations!
12
+ generate_tmp_migrations!
13
+ update_data_file!
14
+ update_rails_migration_paths!
15
+ end
16
+
17
+ # Data about the previous files state
18
+ # This is needed for rebuild only modified temporary migrations
19
+ def last_update
20
+ @last_update ||= begin
21
+ file = ::Rails.root.join(Sql::Migrations::Rails::LAST_UPDATE_FILE).tap { |x| x.dirname.mkpath }
22
+ file.file? ? JSON.parse(file.read).transform_keys(&:to_sym) : { timestamp: '0', files: {} }
23
+ end
24
+ end
25
+
26
+ def tmp_migrations_path
27
+ @tmp_migrations_path ||= ::Rails.root.join(Sql::Migrations::Rails::TMP_MIGRATIONS_FOLDER).tap(&:mkpath)
28
+ end
29
+
30
+ def sql_migrations
31
+ @sql_migrations ||= ::Rails.root.join('db/migrate').glob('*.{up,down}.sql')
32
+ .group_by { |x| x.basename('.up.sql').basename('.down.sql') }
33
+ end
34
+
35
+ def modified_sql_migrations
36
+ @modified_sql_migrations ||= sql_migrations.select do |name, paths|
37
+ last_update[:files][name.to_s] != paths.map { |x| x.to_s.split('.')[-2] } ||
38
+ paths.map { |x| [x.mtime, x.ctime] }.flatten.map(&:utc)
39
+ .max.strftime('%Y%m%d%H%M%S') > last_update[:timestamp]
40
+ end
41
+ end
42
+
43
+ def delete_extra_migrations!
44
+ tmp_migrations_path.children.select { |x| sql_migrations.keys.exclude?(x.basename('.rb')) }.each(&:delete)
45
+ end
46
+
47
+ def generate_tmp_migrations!
48
+ template = File.read(File.join(File.dirname(__FILE__), 'templates/migration.rb.tt'))
49
+ modified_sql_migrations.each do |name, paths|
50
+ erb_hash = {
51
+ migration_class_name: name.to_s.split('_', 2).second.camelize,
52
+ up_migration: paths.find { |x| x.to_s.end_with?('.up.sql') },
53
+ down_migration: paths.find { |x| x.to_s.end_with?('.down.sql') }
54
+ }
55
+
56
+ erb_result = ERB.new(template, nil, '-').result_with_hash(erb_hash)
57
+ Pathname(tmp_migrations_path.join("#{name}.rb")).write(erb_result)
58
+ end
59
+ end
60
+
61
+ def update_data_file!
62
+ data = {
63
+ timestamp: Time.now.utc.strftime('%Y%m%d%H%M%S'),
64
+ files: sql_migrations.transform_values { |x1| x1.map { |x2| x2.to_s.split('.')[-2] } }
65
+ }.to_json
66
+
67
+ ::Rails.root.join(Sql::Migrations::Rails::LAST_UPDATE_FILE).write(data)
68
+ end
69
+
70
+ def update_rails_migration_paths!
71
+ ::Rails.application.config.paths['db/migrate'] << Sql::Migrations::Rails::TMP_MIGRATIONS_FOLDER
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,23 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def up
3
+ <%- if up_migration -%>
4
+ # <%= 'db/migrate/' + up_migration.basename.to_s %>
5
+ execute <<-SQL
6
+ <%= up_migration.read.strip.gsub(/^/, ' ' * 6) %>
7
+ SQL
8
+ <%- else -%>
9
+ raise ActiveRecord::IrreversibleMigration
10
+ <%- end -%>
11
+ end
12
+
13
+ def down
14
+ <%- if down_migration -%>
15
+ # <%= 'db/migrate/' + down_migration.basename.to_s %>
16
+ execute <<-SQL
17
+ <%= down_migration.read.strip.gsub(/^/, ' ' * 6) %>
18
+ SQL
19
+ <%- else -%>
20
+ raise ActiveRecord::IrreversibleMigration
21
+ <%- end -%>
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sql
4
+ module Migrations
5
+ module Rails
6
+ VERSION = '1.0.0'
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql-migrations-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Peleh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
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
+ description: Rails plugin. Allows you to write plain SQL migrations without Ruby code.
56
+ email:
57
+ - pelehev@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/generators/sql_migration/sql_migration_generator.rb
66
+ - lib/sql/migrations/rails.rb
67
+ - lib/sql/migrations/rails/railtie.rb
68
+ - lib/sql/migrations/rails/templates/migration.rb.tt
69
+ - lib/sql/migrations/rails/version.rb
70
+ homepage: https://github.com/epeleh/sql-migrations-rails
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.5.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Rails ActiveRecord plugin
94
+ test_files: []