archive_migration 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/archive_migration.gemspec +39 -0
- data/bin/archive_migration +56 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/archive_migration.rb +244 -0
- data/lib/archive_migration/messages.yml +13 -0
- data/lib/archive_migration/version.rb +3 -0
- data/spec/archive_migration_spec.rb +9 -0
- data/spec/spec_helper.rb +14 -0
- metadata +193 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 574e643abf83cd597ce41af7c55f80b84127ecec
|
4
|
+
data.tar.gz: dda5f2301a13ce6fdf2c5882ae4e6f62823981b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec46451ddee94ec5480571eeead5779e724f7b7ca5240b78a1212ecbdbc2b330ba5e104d63fccb44275a9de5c49c44ae6c767e0820e80e1b8f631a81d2ff3534
|
7
|
+
data.tar.gz: 72ae943849f601b5ffe2c56d1a0995a9ca592547a7bae58d6bcb43a1995e59dbe1224eda4c0a01cb7764f57916ace974e935ccc46bb7c31029c493d2f5367612
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at yibing.shi@snapsheet.me. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
archive_migration (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
binding_of_caller (0.8.0)
|
10
|
+
debug_inspector (>= 0.0.1)
|
11
|
+
coderay (1.1.2)
|
12
|
+
debug_inspector (0.0.3)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
interception (0.5)
|
15
|
+
method_source (0.8.2)
|
16
|
+
mysql2 (0.5.1)
|
17
|
+
pry (0.10.4)
|
18
|
+
coderay (~> 1.1.0)
|
19
|
+
method_source (~> 0.8.1)
|
20
|
+
slop (~> 3.4)
|
21
|
+
pry-nav (0.2.4)
|
22
|
+
pry (>= 0.9.10, < 0.11.0)
|
23
|
+
pry-rails (0.3.6)
|
24
|
+
pry (>= 0.10.4)
|
25
|
+
pry-rescue (1.4.5)
|
26
|
+
interception (>= 0.5)
|
27
|
+
pry
|
28
|
+
pry-stack_explorer (0.4.9.2)
|
29
|
+
binding_of_caller (>= 0.7)
|
30
|
+
pry (>= 0.9.11)
|
31
|
+
rake (10.5.0)
|
32
|
+
rspec (3.7.0)
|
33
|
+
rspec-core (~> 3.7.0)
|
34
|
+
rspec-expectations (~> 3.7.0)
|
35
|
+
rspec-mocks (~> 3.7.0)
|
36
|
+
rspec-core (3.7.1)
|
37
|
+
rspec-support (~> 3.7.0)
|
38
|
+
rspec-expectations (3.7.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.7.0)
|
41
|
+
rspec-mocks (3.7.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.7.0)
|
44
|
+
rspec-support (3.7.1)
|
45
|
+
slop (3.6.0)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
ruby
|
49
|
+
|
50
|
+
DEPENDENCIES
|
51
|
+
archive_migration!
|
52
|
+
bundler (~> 1.16)
|
53
|
+
mysql2
|
54
|
+
pry
|
55
|
+
pry-nav
|
56
|
+
pry-rails
|
57
|
+
pry-rescue
|
58
|
+
pry-stack_explorer
|
59
|
+
rake (~> 10.0)
|
60
|
+
rspec (~> 3.0)
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 nathanshi
|
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,47 @@
|
|
1
|
+
# ArchiveMigration
|
2
|
+
|
3
|
+
Archive ruby migration files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'archive_migration', :git => 'git@github.com:NathanShi/archive-migration.git'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Use rails console to run following code and follow instructions:
|
20
|
+
```
|
21
|
+
ArchiveMigration.archive
|
22
|
+
```
|
23
|
+
|
24
|
+
## Next step
|
25
|
+
|
26
|
+
- add logic to let user run command in bash instead of rails console
|
27
|
+
- add logic to let user archive by year or create zip file to archive
|
28
|
+
- remove hard-coded database selection to make gem more generic
|
29
|
+
- publish gem on gem.org so don't have to include git link
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/archive_migration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
44
|
+
|
45
|
+
## Code of Conduct
|
46
|
+
|
47
|
+
Everyone interacting in the ArchiveMigration project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/archive_migration/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "archive_migration/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "archive_migration"
|
8
|
+
spec.version = ArchiveMigration::VERSION
|
9
|
+
spec.authors = ["yibingshi"]
|
10
|
+
spec.email = ["yibing.shi@snapsheet.me"]
|
11
|
+
|
12
|
+
spec.summary = "To archive migration files to another folder"
|
13
|
+
spec.homepage = "https://github.com/NathanShi/archive-migration"
|
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"] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files`.split($/)
|
26
|
+
# spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
spec.add_development_dependency "mysql2"
|
34
|
+
spec.add_development_dependency "pry"
|
35
|
+
spec.add_development_dependency "pry-rails"
|
36
|
+
spec.add_development_dependency "pry-nav"
|
37
|
+
spec.add_development_dependency "pry-rescue"
|
38
|
+
spec.add_development_dependency "pry-stack_explorer"
|
39
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'optparse'
|
7
|
+
require 'archive_migration'
|
8
|
+
|
9
|
+
options = {}
|
10
|
+
parser = OptionParser.new do |config|
|
11
|
+
config.banner = 'Supported options:'
|
12
|
+
|
13
|
+
config.on('-y', 'already run all migrations') do
|
14
|
+
options[:start] = true
|
15
|
+
end
|
16
|
+
|
17
|
+
config.on('-m', '--migrate', 'run migrations and then archive') do
|
18
|
+
options[:migrate] = true
|
19
|
+
end
|
20
|
+
|
21
|
+
config.on('-d', '--delete', 'delete version list from schema_migrations table') do
|
22
|
+
options[:delete] = true
|
23
|
+
end
|
24
|
+
|
25
|
+
config.on('-r', '--recover', 'redo all archive actions (in progress)') do
|
26
|
+
options[:recover] = true
|
27
|
+
end
|
28
|
+
|
29
|
+
config.on('-z', '--zip', 'zip archive folder (next version)') do
|
30
|
+
options[:zip] = true
|
31
|
+
end
|
32
|
+
|
33
|
+
config.on('-p', '--push', 'push archive folder to S3 (next version)') do
|
34
|
+
options[:push] = true
|
35
|
+
end
|
36
|
+
|
37
|
+
config.on('-v', '--version', 'show gem version') do
|
38
|
+
options[:version] = true
|
39
|
+
end
|
40
|
+
|
41
|
+
config.on('-h', '--help', 'show this message') do
|
42
|
+
options[:help] = true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
parser.parse!
|
46
|
+
|
47
|
+
ArchiveMigration.archive if options[:start]
|
48
|
+
if options[:migrate]
|
49
|
+
ArchiveMigration.run_all_migrations
|
50
|
+
ArchiveMigration.archive
|
51
|
+
end
|
52
|
+
ArchiveMigration.delete_by_version_list([]) if options[:delete]
|
53
|
+
ArchiveMigration.recover if options[:recover]
|
54
|
+
puts ArchiveMigration::VERSION if options[:version]
|
55
|
+
puts parser if options[:help] || options[:push] || options[:zip]
|
56
|
+
ArchiveMigration.start if !options.any?
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "archive_migration"
|
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,244 @@
|
|
1
|
+
require 'archive_migration/version'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'mysql2'
|
4
|
+
require 'pry'
|
5
|
+
require 'set'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
module ArchiveMigration
|
9
|
+
extend self
|
10
|
+
|
11
|
+
autoload :VERSION, 'archive_migration/version'
|
12
|
+
|
13
|
+
def start
|
14
|
+
if ask(:have_run_all_migrations)
|
15
|
+
archive
|
16
|
+
else
|
17
|
+
if ask(:run_all_migrations)
|
18
|
+
run_all_migrations
|
19
|
+
archive
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def archive
|
25
|
+
tell(:creating_archive_folder)
|
26
|
+
FileUtils::mkdir_p './db/archive'
|
27
|
+
|
28
|
+
tell(:moving_files)
|
29
|
+
version_list = archive_by_year
|
30
|
+
|
31
|
+
tell(:generating_migration)
|
32
|
+
data = IO.read('./db/schema.rb')
|
33
|
+
File.write("./db/migrate/#{version_list.max}_from_previous_version.rb", "class FromPreviousVersion < ActiveRecord::Migration\n\tdef change\n" + data[data.index('do')+3..data.rindex('end')-1] + "\tend\nend\n")
|
34
|
+
|
35
|
+
delete_by_version_list(version_list) if ask(:delete_from_schema_table)
|
36
|
+
create_version_list(version_list)
|
37
|
+
end
|
38
|
+
|
39
|
+
def create_version_list(version_list)
|
40
|
+
version_list_file = File.open("./db/archive/version_list.txt", "a")
|
41
|
+
version_list.sort.each do |version|
|
42
|
+
line = version + ','
|
43
|
+
version_list_file.write(line)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def delete_by_version_list(version_list)
|
48
|
+
dir = './db/archive/version_list.txt'
|
49
|
+
if version_list.any?
|
50
|
+
deleting_list = version_list.clone
|
51
|
+
if File.exist?(dir)
|
52
|
+
list = IO.read(dir)
|
53
|
+
deleting_list << list.split(/[\s,']/).max
|
54
|
+
end
|
55
|
+
delete_from_schema_table(deleting_list)
|
56
|
+
else
|
57
|
+
if File.exist?(dir)
|
58
|
+
list = IO.read(dir)
|
59
|
+
delete_from_schema_table(list.split(/[\s,']/))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def archive_by_year
|
65
|
+
dir = './db/migrate'
|
66
|
+
files = Dir.foreach(dir).select { |x| File.file?("#{dir}/#{x}") && !x.include?("from_previous_version") }
|
67
|
+
version_list = []
|
68
|
+
files.each do |file|
|
69
|
+
version_list << file.split('_').first
|
70
|
+
end
|
71
|
+
|
72
|
+
years = get_years(version_list)
|
73
|
+
years.each do |year|
|
74
|
+
FileUtils::mkdir_p "./db/archive/#{year}"
|
75
|
+
end
|
76
|
+
|
77
|
+
Dir.glob('./db/migrate/*.rb').each do |file|
|
78
|
+
file_version = file.split('/').last.split('_').first
|
79
|
+
if file.split('/').last.include?("from_previous_version")
|
80
|
+
FileUtils.rm file
|
81
|
+
else
|
82
|
+
FileUtils.mv file, "./db/archive/#{file_version[0..3]}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
return version_list
|
87
|
+
end
|
88
|
+
|
89
|
+
def recover
|
90
|
+
dir = './db/archive/version_list.txt'
|
91
|
+
if File.exist?(dir)
|
92
|
+
list = IO.read(dir)
|
93
|
+
insert_into_schema_table(list.split(/[\s,']/))
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def tell(key, options = {})
|
98
|
+
message = messages.fetch(key.to_s)
|
99
|
+
message = colorize(message)
|
100
|
+
print(message, options)
|
101
|
+
end
|
102
|
+
|
103
|
+
def run_all_migrations
|
104
|
+
tell(:running_migrations)
|
105
|
+
system('bundle exec rake db:migrate')
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
COLORS = ['red', 'green', 'yellow', 'blue'].each_with_index.inject({}) { |r, (k, i)| r.merge!(k => "03#{ i + 1 }") }
|
111
|
+
|
112
|
+
def get_default_database
|
113
|
+
dir = './config/sample_database.yml'
|
114
|
+
dir2 = './config/database.yml'
|
115
|
+
if File.exist?(dir)
|
116
|
+
database_hash = YAML.load_file(dir)
|
117
|
+
elsif File.exist?(dir2)
|
118
|
+
database_hash = YAML.load_file(dir2)
|
119
|
+
end
|
120
|
+
return database_hash ? database_hash["development"] : nil
|
121
|
+
end
|
122
|
+
|
123
|
+
def insert_into_schema_table(inserting_list)
|
124
|
+
begin
|
125
|
+
tell(:connecting_mysql)
|
126
|
+
client = Mysql2::Client.new(:host => "127.0.0.1", :username => "root")
|
127
|
+
databases = get_databases(client)
|
128
|
+
default_database = get_default_database
|
129
|
+
if default_database.any? && databases.include?(default_database["database"])
|
130
|
+
print("Default Database: " + default_database["database"])
|
131
|
+
if ask(:connecting_database)
|
132
|
+
client.select_db(default_database["database"])
|
133
|
+
else
|
134
|
+
selection = ask_selection(:selection, databases).strip
|
135
|
+
client.select_db(selection)
|
136
|
+
end
|
137
|
+
else
|
138
|
+
selection = ask_selection(:selection, databases).strip
|
139
|
+
client.select_db(selection)
|
140
|
+
end
|
141
|
+
inserting_list.each do |version|
|
142
|
+
if !version.nil? && version != inserting_list.max
|
143
|
+
client.query("INSERT INTO schema_migrations(version) VALUES (#{version})")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
rescue Mysql2::Error => e
|
148
|
+
puts e.errno
|
149
|
+
puts e.error
|
150
|
+
ensure
|
151
|
+
client.close if client
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def delete_from_schema_table(deleting_list)
|
156
|
+
begin
|
157
|
+
tell(:connecting_mysql)
|
158
|
+
client = Mysql2::Client.new(:host => "127.0.0.1", :username => "root")
|
159
|
+
databases = get_databases(client)
|
160
|
+
default_database = get_default_database
|
161
|
+
if default_database.any? && databases.include?(default_database["database"])
|
162
|
+
print("Default Database: " + default_database["database"])
|
163
|
+
if ask(:connecting_database)
|
164
|
+
client.select_db(default_database["database"])
|
165
|
+
else
|
166
|
+
selection = ask_selection(:selection, databases).strip
|
167
|
+
client.select_db(selection)
|
168
|
+
end
|
169
|
+
else
|
170
|
+
selection = ask_selection(:selection, databases).strip
|
171
|
+
client.select_db(selection)
|
172
|
+
end
|
173
|
+
tell(:cleaning)
|
174
|
+
rs = client.query("SELECT version FROM schema_migrations")
|
175
|
+
exist_versions = rs.map { |v| v['version'] }
|
176
|
+
deleting_list.each do |version|
|
177
|
+
if exist_versions.include?(version) && !version.nil? && version != deleting_list.max
|
178
|
+
client.query("DELETE FROM schema_migrations where version = #{version}")
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
rescue Mysql2::Error => e
|
183
|
+
puts e.errno
|
184
|
+
puts e.error
|
185
|
+
ensure
|
186
|
+
client.close if client
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def get_years(version_list)
|
191
|
+
years = Set.new
|
192
|
+
version_list.each do |version|
|
193
|
+
years << version[0..3]
|
194
|
+
end
|
195
|
+
return years
|
196
|
+
end
|
197
|
+
|
198
|
+
def get_databases(conn)
|
199
|
+
rows = []
|
200
|
+
rs = conn.query("show databases")
|
201
|
+
rs.each do |x|
|
202
|
+
rows<<x["Database"] if !x.nil?
|
203
|
+
end
|
204
|
+
|
205
|
+
return rows
|
206
|
+
end
|
207
|
+
|
208
|
+
def print(message, options = {})
|
209
|
+
if options.empty?
|
210
|
+
puts message % options
|
211
|
+
else
|
212
|
+
puts message
|
213
|
+
puts options
|
214
|
+
end
|
215
|
+
puts ''
|
216
|
+
end
|
217
|
+
|
218
|
+
def ask(*args)
|
219
|
+
tell(*args)
|
220
|
+
$stdin.gets[0].downcase == 'y'
|
221
|
+
end
|
222
|
+
|
223
|
+
def ask_selection(*args, databases)
|
224
|
+
tell(*args, databases)
|
225
|
+
selection = $stdin.gets
|
226
|
+
while !databases.include?(selection.strip)
|
227
|
+
tell(:wrong_inputs)
|
228
|
+
tell(*args, databases)
|
229
|
+
selection = $stdin.gets
|
230
|
+
end
|
231
|
+
|
232
|
+
return selection.strip
|
233
|
+
end
|
234
|
+
|
235
|
+
def messages
|
236
|
+
require 'yaml'
|
237
|
+
path = File.join(File.dirname(__FILE__), 'archive_migration/messages.yml')
|
238
|
+
@messages = YAML.load(File.open(path))
|
239
|
+
end
|
240
|
+
|
241
|
+
def colorize(message)
|
242
|
+
message.gsub(/\:(\w+)\<([^>]+)\>/) { |_| "\033[#{ COLORS[$1] }m#{ $2 }\033[039m" }
|
243
|
+
end
|
244
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
archive_by_year: "Would you like to archive all files by year (:green<y> / :red<n>)?"
|
2
|
+
creating_archive_folder: "Creating archive folder...\n"
|
3
|
+
moving_files: "Moving migration files...\n"
|
4
|
+
generating_migration: "Generating migration with :green<latest version number>...\n"
|
5
|
+
delete_from_schema_table: "Do you want to clean your schema version table (:red<y>/:green<n>)?"
|
6
|
+
connecting_mysql: "Connecting to mysql as :red<root> user...\n"
|
7
|
+
connecting_database: "Connecting to default database (:green<y> / :red<n>)"
|
8
|
+
selection: "Please select the correct database:\n"
|
9
|
+
cleaning: "Cleaning the schema migration table...\n"
|
10
|
+
wrong_inputs: ":red<Please enter valid option...>\n"
|
11
|
+
have_run_all_migrations: "Please make sure you have run all migrations.. (:green<y> / :red<n>)"
|
12
|
+
run_all_migrations: "Would you like to run all migrations? (:green<y> / :red<n>)"
|
13
|
+
running_migrations: "Running all migrations..."
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "archive_migration"
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
# Enable flags like --only-failures and --next-failure
|
6
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
7
|
+
|
8
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
9
|
+
config.disable_monkey_patching!
|
10
|
+
|
11
|
+
config.expect_with :rspec do |c|
|
12
|
+
c.syntax = :expect
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: archive_migration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yibingshi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-27 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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mysql2
|
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: pry
|
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: pry-rails
|
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
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry-nav
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-rescue
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry-stack_explorer
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- yibing.shi@snapsheet.me
|
142
|
+
executables:
|
143
|
+
- archive_migration
|
144
|
+
- console
|
145
|
+
- setup
|
146
|
+
extensions: []
|
147
|
+
extra_rdoc_files: []
|
148
|
+
files:
|
149
|
+
- ".gitignore"
|
150
|
+
- ".rspec"
|
151
|
+
- ".travis.yml"
|
152
|
+
- CODE_OF_CONDUCT.md
|
153
|
+
- Gemfile
|
154
|
+
- Gemfile.lock
|
155
|
+
- LICENSE.txt
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- archive_migration.gemspec
|
159
|
+
- bin/archive_migration
|
160
|
+
- bin/console
|
161
|
+
- bin/setup
|
162
|
+
- db/schema.rb
|
163
|
+
- lib/archive_migration.rb
|
164
|
+
- lib/archive_migration/messages.yml
|
165
|
+
- lib/archive_migration/version.rb
|
166
|
+
- spec/archive_migration_spec.rb
|
167
|
+
- spec/spec_helper.rb
|
168
|
+
homepage: https://github.com/NathanShi/archive-migration
|
169
|
+
licenses:
|
170
|
+
- MIT
|
171
|
+
metadata:
|
172
|
+
allowed_push_host: https://rubygems.org
|
173
|
+
post_install_message:
|
174
|
+
rdoc_options: []
|
175
|
+
require_paths:
|
176
|
+
- lib
|
177
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
requirements: []
|
188
|
+
rubyforge_project:
|
189
|
+
rubygems_version: 2.2.2
|
190
|
+
signing_key:
|
191
|
+
specification_version: 4
|
192
|
+
summary: To archive migration files to another folder
|
193
|
+
test_files: []
|