db_seed 0.1.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: 90315573c8b2905bf5789809ec4403039871330c
4
+ data.tar.gz: 467ed6fda05102422c902a39e29a50890cddb291
5
+ SHA512:
6
+ metadata.gz: b3e631e9e37e3a916ee5abaf208152e7db99790e3220155b42ba5e3472de25d3de4b3feac7680c9d3493488c642035203a73550d3f66a54d9a1c4e818de6439e
7
+ data.tar.gz: f8c1fa8c56752051ae26cf0232750e22679fe0e8d5f39ec1a668853b0fffddc28a156aee0456c23273cd7a9cbb30141c3873d868d329f91789d494b1fd18ac45
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ # rspec failure tracking
11
+ .rspec_status
data/.idea/dbseed.iml ADDED
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="RVM: ruby-2.2.0" jdkType="RUBY_SDK" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.16.4, RVM: ruby-2.2.0) [gem]" level="application" />
8
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.4.2, RVM: ruby-2.2.0) [gem]" level="application" />
9
+ </component>
10
+ </module>
@@ -0,0 +1,5 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="PROJECT_PROFILE" />
4
+ </settings>
5
+ </component>
data/.idea/misc.xml ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-2.1.2" project-jdk-type="RUBY_SDK" />
7
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/dbseed.iml" filepath="$PROJECT_DIR$/.idea/dbseed.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="PropertiesComponent">
4
+ <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
5
+ <property name="nodejs_npm_path_reset_for_default_project" value="true" />
6
+ </component>
7
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.2.0
7
+ before_install: gem install bundler -v 1.16.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}"}
4
+
5
+ # Specify your gem's dependencies in DbSeed.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 kingston.s
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,58 @@
1
+ # DbSeed
2
+
3
+ To Avoid running seeds file multiple times
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'db_seed'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install db_seed
20
+
21
+ 1. Add a `db_seeds` table to your database:
22
+
23
+ ```
24
+ bundle exec rails generate db_seed:install
25
+ ```
26
+
27
+ This will generate ``create_active_record_seeds.rb`` file in your application
28
+
29
+ Please run
30
+
31
+ ```
32
+ bundle exec rake db:migrate
33
+ ```
34
+ ## Using for existing project
35
+
36
+ This will create a record in `seeds` table.
37
+
38
+ bundle exec rake db:create_md5_checksum
39
+
40
+
41
+
42
+ ## Usage
43
+
44
+ To Avoid running seeds file multiple times, So that records won't create multiple times.
45
+
46
+ ## How it is works
47
+
48
+ Creating ``mds checksum`` based on `db/seed.rb` content, and creating one record in `db_seeds` table.
49
+
50
+ When `db:seed` task is running, creating `md5 checksum` and matching with database if its is same `md5 checksum`, I am not executing the `db:seed` task.
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jenorish/db_seed.
55
+
56
+ ## License
57
+
58
+ 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,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "DbSeed"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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
data/db_seed.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "db_seed/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "db_seed"
7
+ spec.version = DbSeed::VERSION
8
+ spec.authors = ["kingston.s"]
9
+ spec.email = ["kingston@webnish.com"]
10
+
11
+ spec.summary = %q{To Avoid running seeds file multiple times.}
12
+ spec.description = %q{To Avoid running seeds file multiple times, So that records won't create multiple times.}
13
+ spec.homepage = "http://jenorish.blogspot.com/"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/})}
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+
3
+ module DbSeed
4
+ module ActiveRecord
5
+ class Seed < ::ActiveRecord::Base
6
+ self.table_name = 'db_seeds'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ module DbSeed
2
+ class Task < Rails::Railtie
3
+ rake_tasks do
4
+ Rake::Task["db:seed"].clear
5
+ namespace :db do
6
+ task seed: [:environment, :load_config] do
7
+ md5, md5_checksum, seed_object = find_md5
8
+ if md5_checksum == md5
9
+ puts "Already db/seeds.rb loaded .."
10
+ else
11
+ Rake::Task["db:abort_if_pending_migrations"].invoke
12
+ ::ActiveRecord::Tasks::DatabaseTasks.load_seed
13
+ seed_object.update_column(:md5_checksum, md5)
14
+ end
15
+ end
16
+
17
+ task create_md5_checksum: [:environment, :load_config] do
18
+ md5, md5_checksum, seed_object = find_md5
19
+ seed_object.update_column(:md5_checksum, md5) if md5_checksum.blank?
20
+ end
21
+
22
+ def find_md5
23
+ file_path = File.expand_path("db/seeds.rb")
24
+ md5 = Digest::MD5.file(file_path).hexdigest
25
+ seed_object = DbSeed::ActiveRecord::Seed.first_or_create(md5_checksum: nil)
26
+ md5_checksum = seed_object.try(:md5_checksum)
27
+ return md5, md5_checksum, seed_object
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module DbSeed
2
+ VERSION = "0.1.0"
3
+ end
data/lib/db_seed.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "db_seed/version"
2
+ require "db_seed/active_record/seed"
3
+ require "db_seed/tasks/task"
4
+
5
+
6
+
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module DbSeed
7
+ # Installs PaperTrail in a rails app.
8
+ class InstallGenerator < ::Rails::Generators::Base
9
+ include ::Rails::Generators::Migration
10
+
11
+ # Class names of MySQL adapters.
12
+ # - `MysqlAdapter` - Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.
13
+ # - `Mysql2Adapter` - Used by `mysql2` gem.
14
+ MYSQL_ADAPTERS = %w(ActiveRecord::ConnectionAdapters::MysqlAdapter ActiveRecord::ConnectionAdapters::Mysql2Adapter).freeze
15
+
16
+ source_root File.expand_path("templates", __dir__)
17
+
18
+
19
+ desc "Generates (but does not run) a migration to add a Active Record Seeds table."
20
+
21
+ def create_migration_file
22
+ add_seed_migration("create_active_record_seed")
23
+ end
24
+
25
+ def self.next_migration_number(dirname)
26
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
27
+ end
28
+
29
+ protected
30
+
31
+ def add_seed_migration(template)
32
+ migration_dir = File.expand_path("db/migrate")
33
+ if self.class.migration_exists?(migration_dir, template)
34
+ ::Kernel.warn "Migration already exists: #{template}"
35
+ else
36
+ migration_template(
37
+ "#{template}.rb.erb",
38
+ "db/migrate/#{template}.rb",
39
+ migration_version: migration_version
40
+ )
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def migration_version
47
+ major = ::ActiveRecord::VERSION::MAJOR
48
+ if major >= 5
49
+ "[#{major}.#{::ActiveRecord::VERSION::MINOR}]"
50
+ end
51
+ end
52
+
53
+ # Even modern versions of MySQL still use `latin1` as the default character
54
+ # encoding. Many users are not aware of this, and run into trouble when they
55
+ # try to use PaperTrail in apps that otherwise tend to use UTF-8. Postgres, by
56
+ # comparison, uses UTF-8 except in the unusual case where the OS is configured
57
+ # with a custom locale.
58
+ #
59
+ # - https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html
60
+ # - http://www.postgresql.org/docs/9.4/static/multibyte.html
61
+ #
62
+ # Furthermore, MySQL's original implementation of UTF-8 was flawed, and had
63
+ # to be fixed later by introducing a new charset, `utf8mb4`.
64
+ #
65
+ # - https://mathiasbynens.be/notes/mysql-utf8mb4
66
+ # - https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
67
+ #
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ # This migration creates the `db_seeds` table, the only schema PT requires.
2
+ # All other migrations PT provides are optional.
3
+ class CreateActiveRecordSeed < ActiveRecord::Migration <%= migration_version %>
4
+
5
+ # The largest text column available in all supported RDBMS.
6
+
7
+ def change
8
+ create_table :db_seeds do |t|
9
+ t.string :md5_checksum
10
+ t.datetime :created_at
11
+ end
12
+ add_index :db_seeds, %i(md5_checksum)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_seed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kingston.s
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: To Avoid running seeds file multiple times, So that records won't create
56
+ multiple times.
57
+ email:
58
+ - kingston@webnish.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".idea/dbseed.iml"
65
+ - ".idea/inspectionProfiles/profiles_settings.xml"
66
+ - ".idea/misc.xml"
67
+ - ".idea/modules.xml"
68
+ - ".idea/workspace.xml"
69
+ - ".rspec"
70
+ - ".travis.yml"
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - db_seed.gemspec
78
+ - lib/db_seed.rb
79
+ - lib/db_seed/active_record/seed.rb
80
+ - lib/db_seed/tasks/task.rb
81
+ - lib/db_seed/version.rb
82
+ - lib/generators/db_seed/install_generator.rb
83
+ - lib/generators/db_seed/templates/create_active_record_seed.rb.erb
84
+ homepage: http://jenorish.blogspot.com/
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.14
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: To Avoid running seeds file multiple times.
108
+ test_files: []