simple_data_migrations 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a4275c5b1c6f081913056bd954e294c64db0affb8b3d673c0e8e54794dd35e5
4
+ data.tar.gz: 37b99f86b67e80b8c176a1b401a8a7fbeaf6a266b55cc4661544c070006ca65e
5
+ SHA512:
6
+ metadata.gz: 5731d1a108dcd09d60ca1d9749fe3a449cb08bf6711c5bb3b9662e539135dbf4db8f11e14f5f9705e6d3b0bb81940248699e8818981edf44eac4a55bd9d88b73
7
+ data.tar.gz: da8566add2d9d89fc44cf52b62913ef318902a8dba6fec2373ac128b193d358dedc1d7e826d28726131ec822ac582ea272b2fa4de926c908f029f0e1e7795461
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-01-06
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in simple_data_migrations.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Wojciech Wnętrzak
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,38 @@
1
+ # Simple Data Migrations
2
+
3
+ Inspired by [data-migrate](https://github.com/ilyakatz/data-migrate) but simplified.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "simple_data_migrations"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Install by creating db migration file:
16
+
17
+ `bin/rails generate simple_data_migrations:install` and then run `bin/rails db:migrate`
18
+
19
+ Generate sample data migration file by `bin/rails generate simple_data_migrations:template`
20
+
21
+ Status can be displayed by `bin/rails data:migrate:status`
22
+ And executed by `bin/rails data:migrate`
23
+
24
+ ## Development
25
+
26
+ TODO: add configuration
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tiramizoo/simple_data_migrations.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "simple_data_migrations"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module SimpleDataMigrations
7
+ class InstallGenerator < Rails::Generators::Base
8
+ include ActiveRecord::Generators::Migration
9
+
10
+ source_paths << File.expand_path("../templates", __FILE__)
11
+
12
+ def create_migration_file
13
+ migration_template "install.rb.erb", "db/migrate/create_simple_data_migrations_table.rb"
14
+ end
15
+
16
+ private
17
+
18
+ def migration_version
19
+ "[#{ActiveRecord::VERSION::STRING.to_f}]"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../simple_data_migrations/utils"
4
+
5
+ module SimpleDataMigrations
6
+ class TemplateGenerator < Rails::Generators::NamedBase
7
+ def create_template_file
8
+ version = Time.now.utc.strftime("%Y%m%d%H%M%S") # Rails db migration like
9
+ path = SimpleDataMigrations::Utils.root.join("#{version}_#{file_name}.rb")
10
+
11
+ create_file path do
12
+ <<-TEMPLATE
13
+ ApplicationRecord.transaction do
14
+ # Your code
15
+
16
+ SimpleDataMigrations::Entry.create!(version: #{version})
17
+ end
18
+ TEMPLATE
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateSimpleDataMigrationsTable < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :data_migrations, primary_key: "version", id: :string
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/base"
4
+
5
+ module SimpleDataMigrations
6
+ class Entry < ActiveRecord::Base
7
+ self.table_name = "data_migrations"
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleDataMigrations
4
+ class Railtie < ::Rails::Railtie
5
+
6
+ rake_tasks do
7
+ load File.expand_path("../tasks/data.rake", __dir__)
8
+ end
9
+
10
+ generators do
11
+ require File.expand_path("../generators/simple_data_migrations/template_generator", __dir__)
12
+ require File.expand_path("../generators/simple_data_migrations/install_generator", __dir__)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleDataMigrations
4
+ module Utils
5
+ def self.script_files
6
+ Dir.children(root).sort
7
+ end
8
+
9
+ def self.ran_versions
10
+ Entry.pluck(:version)
11
+ end
12
+
13
+ def self.root
14
+ Rails.root.join("db/data")
15
+ end
16
+
17
+ def self.version(filename)
18
+ filename.split("_", 2).first
19
+ end
20
+
21
+ def self.humanize_filename(filename)
22
+ return "********** NO FILE **********" unless filename
23
+
24
+ filename.delete_prefix(version(filename)).delete_suffix(".rb").humanize
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleDataMigrations
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "benchmark"
4
+ require "thor/shell/basic"
5
+
6
+ require_relative "simple_data_migrations/version"
7
+ require_relative "simple_data_migrations/entry"
8
+ require_relative "simple_data_migrations/utils"
9
+ require_relative "simple_data_migrations/railtie" if defined?(Rails::Railtie)
10
+
11
+ module SimpleDataMigrations
12
+ def self.status
13
+ ran_versions = Utils.ran_versions
14
+ files = Utils.script_files
15
+ all_versions = (ran_versions | files).map { |filename| Utils.version(filename) }
16
+
17
+ result = all_versions.sort.map do |version|
18
+ filename = files.find { |file| file.start_with?(version) }
19
+
20
+ [ran_versions.include?(version) ? "up" : "down", version, Utils.humanize_filename(filename)]
21
+ end
22
+
23
+ result.prepend(["Status", "Version", "Name"])
24
+ Thor::Shell::Basic.new.print_table(result)
25
+ end
26
+
27
+ def self.run
28
+ ran_versions = Utils.ran_versions
29
+ non_run_scripts = Utils.script_files.select do |filename|
30
+ !ran_versions.include?(Utils.version(filename))
31
+ end
32
+
33
+ if ENV["VERSION"]
34
+ non_run_scripts.select! { |filename| Utils.version(filename) == ENV["VERSION"] }
35
+ end
36
+
37
+ non_run_scripts.each do |filename|
38
+ Thor::Shell::Basic.new.say("Running data migration: #{filename}")
39
+ time = Benchmark.measure do
40
+ load Utils.root.join(filename)
41
+ end
42
+ measure = "%.4fs" % time.real
43
+ Thor::Shell::Basic.new.say("Finished in #{measure}")
44
+ end
45
+ end
46
+
47
+ def self.bootstrap
48
+ attributes = Utils.script_files.map { |filename| {version: Utils.version(filename)} }
49
+ Entry.insert_all!(attributes)
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :data do
4
+ desc "Run data migration scripts. Provide VERSION env to run specific one"
5
+ task migrate: :environment do
6
+ require "simple_data_migrations"
7
+
8
+ SimpleDataMigrations.run
9
+ end
10
+
11
+ namespace :migrate do
12
+ desc "Display status of data migration scripts"
13
+ task status: :environment do
14
+ require "simple_data_migrations"
15
+
16
+ SimpleDataMigrations.status
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/simple_data_migrations/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simple_data_migrations"
7
+ spec.version = SimpleDataMigrations::VERSION
8
+ spec.authors = ["Wojciech Wnętrzak"]
9
+ spec.email = ["w.wnetrzak@gmail.com"]
10
+
11
+ spec.summary = "Data migrations"
12
+ spec.description = "Data migrations (mainly for Rails)"
13
+ spec.homepage = "https://github.com/tiramizoo/simple_data_migrations"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/tiramizoo/simple_data_migrations"
19
+ spec.metadata["changelog_uri"] = "https://github.com/tiramizoo/simple_data_migrations/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "activerecord", ">= 6.0"
33
+ spec.add_dependency "thor", ">= 1.0"
34
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_data_migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wojciech Wnętrzak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-06 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: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Data migrations (mainly for Rails)
42
+ email:
43
+ - w.wnetrzak@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/generators/simple_data_migrations/install_generator.rb
56
+ - lib/generators/simple_data_migrations/template_generator.rb
57
+ - lib/generators/simple_data_migrations/templates/install.rb.erb
58
+ - lib/simple_data_migrations.rb
59
+ - lib/simple_data_migrations/entry.rb
60
+ - lib/simple_data_migrations/railtie.rb
61
+ - lib/simple_data_migrations/utils.rb
62
+ - lib/simple_data_migrations/version.rb
63
+ - lib/tasks/data.rake
64
+ - simple_data_migrations.gemspec
65
+ homepage: https://github.com/tiramizoo/simple_data_migrations
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://github.com/tiramizoo/simple_data_migrations
70
+ source_code_uri: https://github.com/tiramizoo/simple_data_migrations
71
+ changelog_uri: https://github.com/tiramizoo/simple_data_migrations/CHANGELOG.md
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.7.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.3.4
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Data migrations
91
+ test_files: []