From 9ef72a950e2faa99427d9a45f924426cd3f1e523 Mon Sep 17 00:00:00 2001 From: Andrei Kaleshka Date: Sun, 14 Jan 2024 17:58:40 +0100 Subject: [PATCH] stub git --- lib/actual_db_schema.rb | 3 ++- lib/actual_db_schema/commands/list.rb | 19 ++++++++++--------- lib/actual_db_schema/git.rb | 12 ++++++++++++ lib/actual_db_schema/store.rb | 8 +------- test/rake_task_test.rb | 14 ++++++++------ 5 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 lib/actual_db_schema/git.rb diff --git a/lib/actual_db_schema.rb b/lib/actual_db_schema.rb index 9f37eae..549b649 100644 --- a/lib/actual_db_schema.rb +++ b/lib/actual_db_schema.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require "active_record/migration" -require "CSV" +require "csv" +require_relative "actual_db_schema/git" require_relative "actual_db_schema/store" require_relative "actual_db_schema/version" require_relative "actual_db_schema/patches/migration_proxy" diff --git a/lib/actual_db_schema/commands/list.rb b/lib/actual_db_schema/commands/list.rb index 0f7cf99..482ccbb 100644 --- a/lib/actual_db_schema/commands/list.rb +++ b/lib/actual_db_schema/commands/list.rb @@ -25,16 +25,17 @@ def preambule end def separator_width - (8 + 14 + branch_column_width + 2 + "Migration File".length) + header.map(&:length).sum + (header.size - 1) * 2 end def header - [ - "Status".center(8), - "Migration ID".ljust(14), - "Branch".ljust(branch_column_width), - "Migration File" - ] + @header ||= + [ + "Status".center(8), + "Migration ID".ljust(14), + "Branch".ljust(branch_column_width), + "Migration File".ljust(16) + ] end def table @@ -51,7 +52,7 @@ def line_for(status, version) [ status.center(8), version.to_s.ljust(14), - branch_for(version).ljust(14), + branch_for(version).ljust(branch_column_width), migration.filename.gsub("#{Rails.root}/", "") ].join(" ") end @@ -70,7 +71,7 @@ def longest_branch_name end def branch_column_width - longest_branch_name.length + 2 + longest_branch_name.length end end end diff --git a/lib/actual_db_schema/git.rb b/lib/actual_db_schema/git.rb new file mode 100644 index 0000000..5be4b7e --- /dev/null +++ b/lib/actual_db_schema/git.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module ActualDbSchema + # Git helper + class Git + def self.current_branch + `git rev-parse --abbrev-ref HEAD`.strip + rescue Errno::ENOENT + "unknown" + end + end +end diff --git a/lib/actual_db_schema/store.rb b/lib/actual_db_schema/store.rb index c4a9c65..ba93405 100644 --- a/lib/actual_db_schema/store.rb +++ b/lib/actual_db_schema/store.rb @@ -27,17 +27,11 @@ def record_metadata(filename) csv << [ version, Time.current.iso8601, - `git rev-parse --abbrev-ref HEAD`.strip + Git.current_branch ] end end - def current_branch - `git rev-parse --abbrev-ref HEAD`.strip - rescue Errno::ENOENT - "unknown" - end - def folder ActualDbSchema.migrated_folder end diff --git a/test/rake_task_test.rb b/test/rake_task_test.rb index c60d682..ced50fb 100644 --- a/test/rake_task_test.rb +++ b/test/rake_task_test.rb @@ -143,11 +143,13 @@ def run_task end it "shows the list of phantom migrations" do - prepare_phantom_migrations - run_task - assert_match(/ Status Migration ID Migration File/, TestingState.output) - assert_match(/--------------------------------------------------/, TestingState.output) - assert_match(%r{ up 20130906111511 tmp/migrated/20130906111511_first.rb}, TestingState.output) - assert_match(%r{ up 20130906111512 tmp/migrated/20130906111512_second.rb}, TestingState.output) + ActualDbSchema::Git.stub(:current_branch, "fix-bug") do + prepare_phantom_migrations + run_task + assert_match(/ Status Migration ID Branch Migration File/, TestingState.output) + assert_match(/---------------------------------------------------/, TestingState.output) + assert_match(%r{ up 20130906111511 fix-bug tmp/migrated/20130906111511_first.rb}, TestingState.output) + assert_match(%r{ up 20130906111512 fix-bug tmp/migrated/20130906111512_second.rb}, TestingState.output) + end end end