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
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
11 commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`
12
13
14
15
16
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