5
6
7
8
9
10
11
12
13
14
15
16
17
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
|
# File 'lib/core/sql_update.rb', line 5
def self.run_sql_update(branch_to_run_on = nil, skip_first_confirm = false)
@git = App::Git::new
branch_to_run_on = @git.current_branch_for_repo(App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_DB)) if branch_to_run_on.nil?
jira_number = @git.resolve_jira_for_branch(branch_to_run_on)
unless skip_first_confirm
unless App::Terminal::prompt_yes_no("You're about to run the SQL update script for #{App::Terminal::format_highlight("BP-#{jira_number}")} on branch #{App::Terminal::format_branch(@git.current_branch_for_repo(App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_DB)))}", ["Please note this script is still in #{App::Terminal::format_highlight('BETA')} and will only work under certain (assumed) circumstances."])
App::Terminal::abort
end
end
@git = App::Git::new
update_performed = false
fail_message = ["An automatic SQL update #{App::Terminal::format_invalid('could not', true)}\x1B[38;5;240m be performed.", "You can either do this #{App::Terminal::format_action('manually')} or exit the script."]
fail_confirmation = "Have you #{App::Terminal::format_highlight('made altered the necessary files')}\x1B[38;5;89m and #{App::Terminal::format_action('committed')}\x1B[38;5;89m the changes?"
unless jira_number =~ /\d{4,5}/i
unless App::Terminal::prompt_yes_no("No #{App::Terminal::format_highlight('jira number')} could be resolved for branch #{App::Terminal::format_branch(branch_to_run_on)}", fail_message, fail_confirmation)
App::Terminal::abort
end
return
end
sandpit_files = get_sandpit_files(jira_number)
unless sandpit_files.any?
unless App::Terminal::prompt_yes_no("No #{App::Terminal::format_highlight('sandpit files')} found for jira number: #{App::Terminal::format_action(jira_number)}", fail_message, fail_confirmation)
App::Terminal::abort
end
return
end
sandpit_files.each do |file|
new_file_contents = []
new_file_contents_output = []
script_run_line_found = false
latest_sql_index = get_latest_unused_sql_index
file_contents = App::UtilsFiles::read_file(file)
file_contents.each do |line|
if line =~ /\A\s*(#+|-{2,}).*(sql_script_run|leave\s+.+;|end\s+if\s*;)/i
new_file_contents_output << "\x1B[38;5;196m- #{line.gsub("\n", '')}\x1B[0m"
if line =~ /\A\s*#+/i
new_line = line.sub(/#+/i, '')
elsif line =~ /\A\s*-{2,}/i
new_line = line.sub(/-{2,}/i, '')
else
new_line = line
end
if new_line =~ /script_index\s*=\s*('|")\S*('|")/i
new_line = new_line.sub(/script_index\s*=\s*('|")\S*('|")/i, "script_index = '#{get_latest_unused_sql_index}'")
elsif new_line =~ /sql_script_run\s+values\s+\(('|")\S*('|")\s*,/i
new_line = new_line.sub(/sql_script_run\s+values\s+\(('|")\S*('|")\s*,/i, "sql_script_run VALUES ('#{get_latest_unused_sql_index}',")
end
new_file_contents_output << "\x1B[38;5;10m+ #{new_line.gsub("\n", '')}\x1B[0m"
new_file_contents << new_line.gsub("\n", '')
else
new_file_contents_output << "\x1B[38;5;240m. #{line.gsub("\n", '')}\x1B[0m"
new_file_contents << line.gsub("\n", '')
end
if line =~ /insert\s+into\s+sql_script_run\s+values\s+\(/i
script_run_line_found = true
end
end
if script_run_line_found == false
sql_index_insert_statement = "INSERT INTO sql_script_run VALUES ('#{latest_sql_index}', NOW(), 1);"
new_file_contents_output << "\x1B[38;5;10m+ #{sql_index_insert_statement}\x1B[0m"
new_file_contents << sql_index_insert_statement
end
new_sql_filename = "#{latest_sql_index}.sql"
new_sql_filename_full = "#{App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_DB)}/brightpearl/structure/#{new_sql_filename}"
if App::Terminal::prompt_yes_no('SQL changes', new_file_contents_output, "Would you like to #{App::Terminal::format_action('apply')}\x1B[38;5;89m these changes and create #{App::Terminal::format_directory(new_sql_filename)} ?")
App::Terminal::output("\x1B[38;5;46mCREATING: #{new_sql_filename_full}\x1B[0m")
App::UtilsFiles::write_file(new_sql_filename_full, new_file_contents)
App::Terminal::command("git add brightpearl/structure/#{new_sql_filename}", App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_DB))
App::Terminal::output("\x1B[38;5;196mDELETING: #{file}\x1B[0m")
App::UtilsFiles::delete_file(file)
update_performed = true
else
App::Terminal::warning('SQL update not processed', ["Sandpit file found but not processed: #{App::Terminal::format_directory(file)}", "Either this is your intention or it will need to be done #{App::Terminal::format_action('manually')}."])
App::Terminal::any_key_to_continue(" Press any key to #{App::Terminal::format_highlight('acknowledge you understand')}...")
end
end
if update_performed
@git.ask_to_commit_to_git("BP-#{jira_number} SQL Update [auto-script].", App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_DB))
end
end
|