Skip to content

Commit

Permalink
Merge pull request #191 from jish/josh/read_all_versions_from_schema_…
Browse files Browse the repository at this point in the history
…files

Read all versions from schema files in the migration check.
  • Loading branch information
jish committed Dec 10, 2014
2 parents 81c95c9 + 3c0dd4b commit 3dbf3fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
10 changes: 6 additions & 4 deletions lib/plugins/pre_commit/checks/migration.rb
Expand Up @@ -6,7 +6,9 @@ class Migration < Plugin
VERSION_PATTERN = /(\d{14})/

self::VersionedFile = Struct.new(:file, :version) do
alias_method :to_s, :file
def to_s
"<#{self.class.name} file=\"#{file}\" version=\"#{version}\">"
end
end

def self.aliases
Expand All @@ -26,7 +28,7 @@ def call(staged_files)
missing_versions = migration_versions - schema_files.map(&:version)
if missing_versions.any?
"You did not add the schema versions for "\
"#{migration_versions.join(', ')} to #{schema_files.join(' or ')}"
"#{migration_versions.join(', ')} to #{schema_files.map(&:file).join(' or ')}"
end
end
end
Expand All @@ -49,8 +51,8 @@ def versioned_schema_files(staged_files)
end

files.each_with_object([]) do |f, result|
if IO.read(f) =~ VERSION_PATTERN
result << VersionedFile.new(f, $1)
File.read(f).scan(VERSION_PATTERN) do |i|
result << VersionedFile.new(f, i.first)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/unit/plugins/pre_commit/checks/migration_test.rb
Expand Up @@ -36,6 +36,18 @@ def write(file, content)
end
end

it 'detects multiple versions in a structure file' do
in_new_directory do
write "db/migrate/20140718171920_foo.rb", "Yep"
write "db/foo_structure.sql", <<-STRUCTURE
INSERT INTO schema_migrations (version) VALUES ('20111115214127');
INSERT INTO schema_migrations (version) VALUES ('20140718171920');
STRUCTURE
check.call(['db/migrate/20140718171920_foo.rb', 'db/foo_structure.sql']).must_equal nil
end
end

it "succeeds if random files are changed" do
check.call(['public/javascript/foo.js', 'lib/bar.rb']).must_equal nil
end
Expand Down
1 change: 0 additions & 1 deletion test/unit/plugins/pre_commit/checks/scss_lint_test.rb
Expand Up @@ -19,6 +19,5 @@

it "fails for bad formatted code" do
check.call([fixture_file("bad.scss")]).must_match(/test\/files\/bad\.scss/)
check.call([fixture_file("bad.scss")]).must_match(/should be sorted in order/)
end
end
8 changes: 4 additions & 4 deletions test/unit/pre-commit/utils/staged_files_test.rb
Expand Up @@ -93,15 +93,15 @@
it "sets staged files from list" do
subject.staged_files.must_equal([])
subject.set_staged_files("some_file", "another_file")
subject.staged_files.must_equal(["some_file", "another_file"])
subject.staged_files.sort.must_equal(["some_file", "another_file"].sort)
end

it "sets staged files - all" do
subject.staged_files.must_equal([])
write("something.rb", "")
write("file.rb", "")
subject.set_staged_files(:all)
subject.staged_files.must_equal(["file.rb", "something.rb"])
subject.staged_files.sort.must_equal(["file.rb", "something.rb"].sort)
end

it "sets staged files - git" do
Expand All @@ -112,7 +112,7 @@
subject.staged_files = nil
subject.set_staged_files()
write("not_git.rb", "")
subject.staged_files.must_equal(["file.rb", "something.rb"])
subject.staged_files.sort.must_equal(["file.rb", "something.rb"].sort)
end

it "sets staged files - git all" do
Expand All @@ -122,7 +122,7 @@
system("git", "add", "-A")
subject.set_staged_files(:git)
write("not_git.rb", "")
subject.staged_files.must_equal(["file.rb", "something.rb"])
subject.staged_files.sort.must_equal(["file.rb", "something.rb"].sort)
end

end
Expand Down

0 comments on commit 3dbf3fe

Please sign in to comment.