Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Conor Hunt

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Ruby subversion pre-commit hook to prevent conflicting Rails migrations

This is a subversion pre-commit hook that prevents a Ruby on Rails migration being committed that has the same version as an existing migration. To install you place this in a file called pre-commit in the hooks directory of your subversion repository. You can read about hooks here: http://svnbook.red-bean.com/en/1.0/ch05s02.html

   1  
   2  #!/usr/bin/env ruby
   3  
   4  repo_path = ARGV[0]
   5  transaction = ARGV[1]
   6  svnlook = '/usr/bin/svnlook'
   7  
   8  commit_dirs_changed = `#{svnlook} dirs-changed #{repo_path} -t #{transaction}`
   9  commit_changed = `#{svnlook} changed #{repo_path} -t #{transaction}`
  10  #commit_author = `#{svnlook} author #{repo_path} -t #{transaction}`.chop
  11  commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`
  12  #commit_diff = `#{svnlook} diff #{repo_path} -t #{transaction}`
  13  #commit_date = `#{svnlook} date #{repo_path} -t #{transaction}`
  14  
  15  # ******* Migration check ********
  16  # if this is a migration then check that there is not already a migration with the same version number in the repository
  17  files = commit_changed.split(/\n/)
  18  current_migrations = nil
  19  for file in files
  20    if(file =~ /A\s*(.*?\/migrate\/)(\d+)(.*)/)
  21      migration_path = $1
  22      migration_version = $2
  23      
  24      if(current_migrations == nil)
  25        current_migrations = {}
  26        migration_files = `#{svnlook} tree #{repo_path} #{migration_path}`
  27        for migration in migration_files
  28          current_migrations[$1] = true if(migration =~ /\s*(\d+)_(.*)/)
  29        end
  30      end
  31      
  32      if(current_migrations[migration_version])
  33       STDERR.puts("The is a pre-existing migration with version #{migration_version} in #{migration_path}")
  34       exit(1)
  35      end
  36    end
  37  end
  38  
  39  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS