Skip to content

Commit

Permalink
show branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed Jan 14, 2024
1 parent 91bec40 commit 8e1736b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/actual_db_schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "active_record/migration"
require "CSV"
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
52 changes: 46 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,58 @@ 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
(8 + 14 + branch_column_width + 2 + "Migration File".length)
end

def header
[
"Status".center(8),
"Migration ID".ljust(14),
"Branch".ljust(branch_column_width),
"Migration File"
]
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(14),
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 + 2
end
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
49 changes: 49 additions & 0 deletions lib/actual_db_schema/store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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, "ab") do |csv|
csv << [
version,
Time.current.iso8601,
`git rev-parse --abbrev-ref HEAD`.strip
]
end
end

def current_branch
`git rev-parse --abbrev-ref HEAD`.strip
rescue Errno::ENOENT
"unknown"
end

def folder
ActualDbSchema.migrated_folder
end

def store_file
folder.join("metadata.csv")
end
end
end

0 comments on commit 8e1736b

Please sign in to comment.