flatten_migrations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in flatten_migrations.gemspec
4
+ gemspec
@@ -0,0 +1,31 @@
1
+ Flatten migrations
2
+ ===================
3
+
4
+ A gem that adds db:flatten_migrattions to rake tasks in your rails project.
5
+
6
+ Requirements
7
+ ------------
8
+
9
+ You have to ensure that your **schema format is set to :ruby** in configuration of
10
+ your project.
11
+ So in your *project*/config/environments/*.rb you should have a line:
12
+
13
+ ```
14
+ config.active_record.schema_format = :ruby
15
+ ```
16
+
17
+ How does it work
18
+ ----------------
19
+
20
+ The task steps are as follows:
21
+
22
+ 1. run pending migrations (rake db:migrate)
23
+ 2. wipe out *project*/db/migrate direcotry
24
+ 3. generate initial migration from existing database schema file
25
+ initial migration is assumed to be the one which was last executed
26
+ 4. create auxiliary migration which adjusts the schema_migrations table
27
+ 5. run the new migration
28
+
29
+ **Both new migrations (*initial* and *auxiliary*) are also irreversible.**
30
+
31
+ Your database is safe and it should not erase any data except the schema_migrations table.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "flatten_migrations/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "flatten_migrations"
7
+ s.version = FlattenMigrations::VERSION
8
+ s.authors = ["Krzysztof Sakwerda"]
9
+ s.email = ["ksakwerda@gmail.com"]
10
+ s.homepage = "https://github.com/simplificator/flatten_migrations"
11
+ s.summary = "Migration flattener for rails applications"
12
+ s.description = "Adds rake db:flatten_migrations task which deletes existing migrations and generates initial one directly from schema.rb file."
13
+
14
+ s.rubyforge_project = "flatten_migrations"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "rake"
22
+ s.add_dependency "rails", ">= 3.0"
23
+ end
@@ -0,0 +1,9 @@
1
+ require "flatten_migrations/version"
2
+
3
+ module FlattenMigrations
4
+ class FlattenMigrationsLoader < Rails::Railtie
5
+ rake_tasks do
6
+ Dir[ File.join File.dirname(__FILE__), "tasks", "*.rake" ].each{ |ext| load ext }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module FlattenMigrations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,76 @@
1
+ namespace :db do
2
+ desc "Delete all migrations from db/migrate directory and create initial migration from schema"
3
+ task :flatten_migrations do
4
+ migrate_db
5
+ check_schema
6
+ delete_migrations
7
+ create_initial
8
+ create_auxiliary
9
+ migrate_db true
10
+ end
11
+
12
+ private
13
+ def check_schema
14
+ schema_path = File.join "db", "schema.rb"
15
+ begin
16
+ File.open( File.join( Rails.root.to_s, schema_path ), 'r')
17
+ rescue
18
+ puts "\033[0;91mIt seems that database schema file #{schema_path} is not present.\033[0m"
19
+ puts "Check if you have set schema format to :ruby in your configuration file."
20
+ puts "If yes try to run \033[0;93mrake db:schema:dump.\033[0m"
21
+ exit
22
+ end
23
+ end
24
+
25
+ def migrate_db( force = false )
26
+ puts "\033[0;95mRunning #{force ? "initial" : "pending "}migration#{force ? "" : "s"}.\033[0m"
27
+ if force then Rake::Task[ "db:migrate" ].reenable end
28
+ Rake::Task[ "db:migrate" ].invoke
29
+ puts "\033[0;92mDone.\033[0m"
30
+ end
31
+
32
+ def delete_migrations
33
+ puts "\033[0;95mDeleting existing migrations.\033[0m"
34
+ Dir[ File.join Rails.root.to_s, "db", "migrate", "*" ].each{ |f| File.delete f }
35
+ puts "\033[0;92mDone.\033[0m"
36
+ end
37
+
38
+ def create_initial
39
+ puts "\033[0;95mCreating initial migration from schema.\033[0m"
40
+
41
+ initial_file = File.open( File.join( Rails.root.to_s, "db", "migrate", "#{ActiveRecord::Migrator.current_version.to_s}_initial.rb" ), 'w' )
42
+ initial_file.write(
43
+ "class Initial < ActiveRecord::Migration\n" + \
44
+ "\tdef self.up\n" )
45
+
46
+ no_write_flag = true
47
+ File.open( File.join( Rails.root.to_s, "db", "schema.rb" ), 'r').each_line do |line|
48
+ initial_file.write "\t#{line.gsub( /,(\s*):force(\s*)=>(\s*)true/, "" )}" unless no_write_flag
49
+ no_write_flag = !( line.starts_with? "ActiveRecord::Schema.define" ) unless !no_write_flag
50
+ end
51
+
52
+ initial_file.write(
53
+ "\n" + \
54
+ "\tdef self.down\n" + \
55
+ "\t\traise ActiveRecord::IrreversibleMigration\n" + \
56
+ "\tend\n" + \
57
+ "end\n" )
58
+ initial_file.close
59
+ puts "\033[0;92mDone.\033[0m"
60
+ end
61
+
62
+ def create_auxiliary
63
+ puts "\033[0;95mCreating auxiliary migration that will fix schema_migrations table.\033[0m"
64
+ File.open( File.join( Rails.root.to_s, "db", "migrate", "#{Time.now.strftime "%Y%m%d%H%M%S"}_auxiliary.rb" ), 'w' ) do |f|
65
+ f.puts "class Auxiliary < ActiveRecord::Migration"
66
+ f.puts "\tdef self.up\n"
67
+ f.puts "\t\texecute \"TRUNCATE schema_migrations;\""
68
+ f.puts "\t\texecute \"INSERT INTO schema_migrations VALUES ('#{ActiveRecord::Migrator.current_version.to_s}');\""
69
+ f.puts "\tend\n"
70
+ f.puts "\tdef self.down\n"
71
+ f.puts "\t\traise ActiveRecord::IrreversibleMigration"
72
+ f.puts "\tend"
73
+ f.puts "end\n"
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flatten_migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Sakwerda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2011-07-28 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "3.0"
34
+ version:
35
+ description: Adds rake db:flatten_migrations task which deletes existing migrations and generates initial one directly from schema.rb file.
36
+ email:
37
+ - ksakwerda@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.markdown
48
+ - Rakefile
49
+ - flatten_migrations.gemspec
50
+ - lib/flatten_migrations.rb
51
+ - lib/flatten_migrations/version.rb
52
+ - lib/tasks/flatten_migrations.rake
53
+ has_rdoc: true
54
+ homepage: https://github.com/simplificator/flatten_migrations
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: flatten_migrations
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Migration flattener for rails applications
81
+ test_files: []
82
+