Skip to content

Commit

Permalink
Remember branch name (#39)
Browse files Browse the repository at this point in the history
* ignore schema.rb

* delete schema.rb

* show branch name

* stub git

* do not use binary mode
  • Loading branch information
ka8725 authored Jan 18, 2024
1 parent 0723cdf commit 0ab2a03
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
/tmp/
/test/dummy_app/tmp/
/test/dummy_app/db/migrate/*.rb
/test/dummy_app/db/schema.rb
.ruby-version
.ruby-gemset
3 changes: 3 additions & 0 deletions lib/actual_db_schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require "active_record/migration"
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"
require_relative "actual_db_schema/patches/migrator"
Expand Down
53 changes: 47 additions & 6 deletions lib/actual_db_schema/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,59 @@ def preambule
puts "Below is a list of irrelevant migrations executed in unmerged branches."
puts "To bring your database schema up to date, the migrations marked as \"up\" should be rolled back."
puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.database}\n\n"
puts %(#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration File)
puts "-" * 50
puts header.join(" ")
puts "-" * separator_width
end

def separator_width
header.map(&:length).sum + (header.size - 1) * 2
end

def header
@header ||=
[
"Status".center(8),
"Migration ID".ljust(14),
"Branch".ljust(branch_column_width),
"Migration File".ljust(16)
]
end

def table
context.migrations_status.each do |status, version|
migration = indexed_phantom_migrations[version]
next unless migration

puts %(#{status.center(8)} #{version.to_s.ljust(14)} #{migration.filename.gsub("#{Rails.root}/", "")})
line = line_for(status, version)
puts line if line
end
end

def line_for(status, version)
migration = indexed_phantom_migrations[version]
return unless migration

[
status.center(8),
version.to_s.ljust(14),
branch_for(version).ljust(branch_column_width),
migration.filename.gsub("#{Rails.root}/", "")
].join(" ")
end

def branch_for(version)
metadata.fetch(version, {})[:branch] || "unknown"
end

def metadata
@metadata ||= ActualDbSchema::Store.instance.read
end

def longest_branch_name
@longest_branch_name ||=
metadata.values.map { |v| v[:branch] }.compact.max_by(&:length) || "unknown"
end

def branch_column_width
longest_branch_name.length
end
end
end
end
12 changes: 12 additions & 0 deletions lib/actual_db_schema/git.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/actual_db_schema/patches/migration_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Patches
module MigrationProxy
def migrate(direction)
super(direction)
FileUtils.copy(filename, ActualDbSchema.migrated_folder.join(basename)) if direction == :up
ActualDbSchema::Store.instance.write(filename) if direction == :up
end
end
end
Expand Down
43 changes: 43 additions & 0 deletions lib/actual_db_schema/store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module ActualDbSchema
# Stores the migrated files into the tmp folder
class Store
include Singleton

Item = Struct.new(:version, :timestamp, :branch)

def write(filename)
basename = File.basename(filename)
FileUtils.copy(filename, folder.join(basename))
record_metadata(filename)
end

def read
return {} unless File.exist?(store_file)

CSV.read(store_file).map { |line| Item.new(*line) }.index_by(&:version)
end

private

def record_metadata(filename)
version = File.basename(filename).scan(/(\d+)_.*\.rb/).first.first
CSV.open(store_file, "a") do |csv|
csv << [
version,
Time.current.iso8601,
Git.current_branch
]
end
end

def folder
ActualDbSchema.migrated_folder
end

def store_file
folder.join("metadata.csv")
end
end
end
14 changes: 0 additions & 14 deletions test/dummy_app/db/schema.rb

This file was deleted.

14 changes: 8 additions & 6 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0ab2a03

Please sign in to comment.