From 8e1736bb4ab0cec4af458e4cd6f3464a0f881b9a Mon Sep 17 00:00:00 2001 From: Andrei Kaleshka Date: Sun, 14 Jan 2024 17:41:19 +0100 Subject: [PATCH] show branch name --- lib/actual_db_schema.rb | 2 + lib/actual_db_schema/commands/list.rb | 52 ++++++++++++++++--- .../patches/migration_proxy.rb | 2 +- lib/actual_db_schema/store.rb | 49 +++++++++++++++++ 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 lib/actual_db_schema/store.rb diff --git a/lib/actual_db_schema.rb b/lib/actual_db_schema.rb index abc6d5e..9f37eae 100644 --- a/lib/actual_db_schema.rb +++ b/lib/actual_db_schema.rb @@ -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" diff --git a/lib/actual_db_schema/commands/list.rb b/lib/actual_db_schema/commands/list.rb index 3176cda..0f7cf99 100644 --- a/lib/actual_db_schema/commands/list.rb +++ b/lib/actual_db_schema/commands/list.rb @@ -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 diff --git a/lib/actual_db_schema/patches/migration_proxy.rb b/lib/actual_db_schema/patches/migration_proxy.rb index 60bd32a..d1bc4fa 100644 --- a/lib/actual_db_schema/patches/migration_proxy.rb +++ b/lib/actual_db_schema/patches/migration_proxy.rb @@ -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 diff --git a/lib/actual_db_schema/store.rb b/lib/actual_db_schema/store.rb new file mode 100644 index 0000000..c4a9c65 --- /dev/null +++ b/lib/actual_db_schema/store.rb @@ -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