18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/railbow/demo/status_demo.rb', line 18
def run
formatter = Railbow::Formatters::Base.new
migrations = Fixtures.migrations
puts "\n#{formatter.emoji(:status)} Database: #{formatter.cyan("db/development.sqlite3")}"
puts
date_format = "full"
tables_enabled = true
diff_enabled = true
git_email = Fixtures::DEMO_USER_EMAIL
git_name = Fixtures::DEMO_USER_NAME
latest_version = migrations.last[:version]
latest_mig_date = parse_version_date(latest_version)
has_landed_tags = migrations.any? { |m|
next false unless m[:landed_date]
mig_date = parse_version_date(m[:version])
mig_date && (m[:landed_date] - mig_date) > 7
}
needs_name_truncation = tables_enabled || diff_enabled || has_landed_tags
name_col_width = needs_name_truncation ? 60 : nil
table_columns = [
Railbow::Table::Column.new(label: "Status", max_width: 6, sticky: true, accent: true),
Railbow::Table::Column.new(label: "Migration ID", sticky: true),
Railbow::Table::Column.new(label: "Created At"),
Railbow::Table::Column.new(label: "Migration Name",
max_width: name_col_width,
truncate: needs_name_truncation)
]
tables_truncate_fn = ->(cell_raw, max_w) { formatter.table_tags_fitted(cell_raw, max_w) }
table_columns << Railbow::Table::Column.new(label: "Tables", truncate: true, truncate_fn: tables_truncate_fn)
highlight_rows = Set.new
down_rows = Set.new
rows = migrations.each_with_index.map do |m, idx|
down_rows << idx if m[:status] == "down"
colored_status = case m[:status]
when "up" then formatter.green_bold("up")
when "down" then formatter.yellow_bold("down")
else m[:status]
end
display_name = m[:name]
diff_tag = m[:branch] ? formatter.diff_tag_branch(m[:branch]) : nil
landed_tag = nil
if m[:landed_date]
mig_date = parse_version_date(m[:version])
if mig_date && (m[:landed_date] - mig_date) > 7
fresh = latest_mig_date && m[:landed_date] >= latest_mig_date
landed_tag = formatter.landed_tag(m[:landed_date], fresh: fresh)
end
end
tags = [landed_tag, diff_tag].compact.join(" ")
if !tags.empty? && name_col_width
tags_width = formatter.display_width(formatter.strip_ansi(tags))
available = name_col_width - tags_width - 2
display_name = formatter.truncate_str(display_name, available)
name_width = formatter.display_width(formatter.strip_ansi(display_name))
padding = name_col_width - name_width - tags_width
display_name = "#{display_name}#{" " * [padding, 2].max}#{tags}"
elsif !tags.empty?
display_name = "#{display_name} #{tags}"
end
created_at = formatter.format_date(m[:version], date_format)
if m[:author_email] == git_email ||
(git_name && m[:author_name] && m[:author_name].downcase == git_name.downcase)
highlight_rows << idx
end
table_tags = formatter.table_tags(m[:tables])
[colored_status, m[:version], created_at, display_name, table_tags]
end
calendar = Railbow::Calendar.build(
migrations.map { |m| m[:version] },
weeks: Railbow::Params.calendar_wdividers?,
ticks: Railbow::Params.calendar_wticks?,
counts: Railbow::Params.calendar_counts?,
month_label: Railbow::Params.calendar_label,
week_label: Railbow::Params.calendar_week_label
)
aliases = Railbow::Config.table_aliases
renderer = Railbow::Table::Renderer.new(
columns: table_columns,
theme: Railbow::Table::Themes::WALLS,
compact: {oneline: false, dense: false, noheader: false, maxw: nil, hidden_columns: []},
aliases: aliases
)
puts renderer.render(rows,
separators: calendar.separators,
highlight_rows: highlight_rows, dim_rows: down_rows,
tick_rows: calendar.tick_rows, tick_col: 2)
end
|