Module: ArchiveMigration
- Extended by:
- ArchiveMigration
- Included in:
- ArchiveMigration
- Defined in:
- lib/archive_migration.rb,
lib/archive_migration/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #archive ⇒ Object
- #archive_by_year ⇒ Object
- #create_version_list(version_list) ⇒ Object
- #delete_by_version_list(version_list) ⇒ Object
- #recover ⇒ Object
- #run_all_migrations ⇒ Object
- #start ⇒ Object
- #tell(key, options = {}) ⇒ Object
Instance Method Details
#archive ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/archive_migration.rb', line 24 def archive tell(:creating_archive_folder) FileUtils::mkdir_p './db/archive' tell(:moving_files) version_list = archive_by_year tell(:generating_migration) data = IO.read('./db/schema.rb') 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") delete_by_version_list(version_list) if ask(:delete_from_schema_table) create_version_list(version_list) end |
#archive_by_year ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/archive_migration.rb', line 64 def archive_by_year dir = './db/migrate' files = Dir.foreach(dir).select { |x| File.file?("#{dir}/#{x}") && !x.include?("from_previous_version") } version_list = [] files.each do |file| version_list << file.split('_').first end years = get_years(version_list) years.each do |year| FileUtils::mkdir_p "./db/archive/#{year}" end Dir.glob('./db/migrate/*.rb').each do |file| file_version = file.split('/').last.split('_').first if file.split('/').last.include?("from_previous_version") FileUtils.rm file else FileUtils.mv file, "./db/archive/#{file_version[0..3]}" end end return version_list end |
#create_version_list(version_list) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/archive_migration.rb', line 39 def create_version_list(version_list) version_list_file = File.open("./db/archive/version_list.txt", "a") version_list.sort.each do |version| line = version + ',' version_list_file.write(line) end end |
#delete_by_version_list(version_list) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/archive_migration.rb', line 47 def delete_by_version_list(version_list) dir = './db/archive/version_list.txt' if version_list.any? deleting_list = version_list.clone if File.exist?(dir) list = IO.read(dir) deleting_list << list.split(/[\s,']/).max end delete_from_schema_table(deleting_list) else if File.exist?(dir) list = IO.read(dir) delete_from_schema_table(list.split(/[\s,']/)) end end end |
#recover ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/archive_migration.rb', line 89 def recover dir = './db/archive/version_list.txt' if File.exist?(dir) list = IO.read(dir) insert_into_schema_table(list.split(/[\s,']/)) end end |
#run_all_migrations ⇒ Object
103 104 105 106 |
# File 'lib/archive_migration.rb', line 103 def run_all_migrations tell(:running_migrations) system('bundle exec rake db:migrate') end |
#start ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/archive_migration.rb', line 13 def start if ask(:have_run_all_migrations) archive else if ask(:run_all_migrations) run_all_migrations archive end end end |
#tell(key, options = {}) ⇒ Object
97 98 99 100 101 |
# File 'lib/archive_migration.rb', line 97 def tell(key, = {}) = .fetch(key.to_s) = colorize() print(, ) end |