Class: App::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/core/mysql.rb

Constant Summary collapse

CONFIG_SCHEMA =
'blufin-master'
CONFIG_FILENAME_STRUCTURE =
'blufin-master-structure.sql'
CONFIG_FILENAME_STRUCTURE_FKS =
'blufin-master-structure-fks.sql'
MOCK_SCHEMA =
'blufin-mock'
MOCK_FILENAME_STRUCTURE =
'blufin-mock-structure.sql'
MOCK_FILENAME_STRUCTURE_FKS =
'blufin-mock-structure-fks.sql'

Class Method Summary collapse

Class Method Details

.reset_full_mock_data(site, skip_confirm = false, verbose = false) ⇒ Object

Reset the DB.

Returns:

  • void



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/core/mysql.rb', line 17

def self.reset_full_mock_data(site, skip_confirm = false, verbose = false)

    begin

        # TODO - 4/13/18 - IMPLEMENT OPTIONAL CONFIG RESET FLAG.
        Blufin::Terminal::output("Must create flag to make #{CONFIG_SCHEMA} reset optional.", Blufin::Terminal::MSG_TODO)

        @site = Blufin::SiteResolver::validate_site(site)
        @site_name = Blufin::SiteResolver::get_site_name(@site)

        @error_handler = Blufin::SqlErrorHandler.new(@site)

        @sql_mismatch_errors = []
        @vsf_max = []
        @asf_max = []

        @schema_data = Blufin::YmlCacheHandler::get(@site, Blufin::YmlCacheHandler::SCHEMA_DATA)
        @schema_fks_hash = Blufin::YmlCacheHandler::get(@site, Blufin::YmlCacheHandler::SCHEMA_FKS)

        schemas = Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE
        warning_message = ['This process cannot be reversed and all current data will be lost in the following schemas:', nil]
        has_path_to_ruby = !Blufin::Config::get_path('Paths', 'BlufinRuby').nil?

        Blufin::Terminal::output("Path to ruby not found in #{Blufin::Terminal::format_directory(App::CONFIG_FILE)}, #{Blufin::Terminal::format_highlight(App::MySQL::CONFIG_SCHEMA)} table will not be generated.", Blufin::Terminal::MSG_WARNING) unless has_path_to_ruby

        schemas.each do |schema|
            if schema == Blufin::YmlSchemaValidator::CONFIG
                warning_message << CONFIG_SCHEMA if has_path_to_ruby
            elsif schema == Blufin::YmlSchemaValidator::MOCK
                warning_message << MOCK_SCHEMA if has_path_to_ruby
            else
                warning_message << "#{Blufin::SiteResolver::get_site_name(@site)}-#{schema}"
            end
        end

        sql_data_files = get_sql_data_files

        # Validates all the data-insert statements.
        sql_data_files.each { |file| validate_sql_data_file(file) } if sql_data_files.any?

        generate_sql_mismatch_errors if @sql_mismatch_errors.any?

        # If SQL data errors exist, script will not execute past this point.
        return if @error_handler.display_errors_if_any

        # TODO DB - Need to replace with DBResolver.

        cli_host = Blufin::Config::get['Databases'][0]['Host']
        cli_user = Blufin::Config::get['Databases'][0]['User']
        cli_pass = Blufin::Config::get['Databases'][0]['Password']
        cli_host = cli_host == 'nil' ? '' : " -h #{cli_host}"
        cli_user = cli_user == 'nil' ? '' : " -u#{cli_user}"
        cli_pass = cli_pass == 'nil' ? '' : " -p#{cli_pass.gsub('$', '\$')}"
        cli_verbose = verbose ? '' : ' &>/dev/null';

        if skip_confirm || Blufin::Terminal::prompt_yes_no("Are you sure you want to #{Blufin::Terminal::format_highlight('completely reset')} your database(s)?", warning_message)

            commands_truncate = []
            commands_build_schema = []
            commands_build_fks = []
            commands_insert_data = []

            schemas.each do |schema|

                structure_path = get_structure_path(schema)
                data_path = get_data_path(schema)

                # Skip if user doesn't have the PATH_TO_RUBY config value.
                next if structure_path.nil? || data_path.nil?

                site_schema = (schema == Blufin::YmlSchemaValidator::CONFIG) ? CONFIG_SCHEMA : "#{@site_name}-#{schema}"
                structure_file = "#{structure_path}/" + ((schema == Blufin::YmlSchemaValidator::CONFIG) ? CONFIG_FILENAME_STRUCTURE : "#{@site_name}-#{schema}.sql")
                foreign_key_file = "#{structure_path}/" + ((schema == Blufin::YmlSchemaValidator::CONFIG) ? CONFIG_FILENAME_STRUCTURE_FKS : "#{@site_name}-fks-#{schema}.sql")

                commands_truncate << "mysql#{cli_host}#{cli_user}#{cli_pass} -e 'set foreign_key_checks = 0; drop database if exists `#{site_schema}`; create database `#{site_schema}`; set foreign_key_checks = 1;'#{cli_verbose}"
                commands_build_schema << "mysql#{cli_host}#{cli_user}#{cli_pass} #{site_schema} < #{structure_file}#{cli_verbose}" if Blufin::Files::file_exists(structure_file)
                commands_build_fks << "mysql#{cli_host}#{cli_user}#{cli_pass} #{site_schema} < #{foreign_key_file}#{cli_verbose}" if Blufin::Files::file_exists(foreign_key_file)

                path_vcs = "#{data_path}/#{schema}"

                # Generate path(s) if they don't exist.
                unless Blufin::Files::path_exists(path_vcs)
                    Blufin::Files::create_directory(path_vcs)
                    Blufin::Terminal::output(Blufin::Terminal::format_directory(path_vcs), Blufin::Terminal::MSG_GENERATED)
                end

                # Generates all the data-insert commands.
                Blufin::Files::get_files_in_dir(path_vcs).each { |file| commands_insert_data << "mysql#{cli_host}#{cli_user}#{cli_pass} #{site_schema} < #{file}#{cli_verbose}" if file_exists_and_has_data(file) } if sql_data_files.any?

            end

            Blufin::Terminal::info("Destroying #{Blufin::Terminal::format_highlight('schema(s)')}")
            Blufin::Terminal::command(commands_truncate)
            Blufin::Terminal::info("Creating #{Blufin::Terminal::format_highlight('schema(s)')}")
            Blufin::Terminal::command(commands_build_schema)
            Blufin::Terminal::info("Inserting #{Blufin::Terminal::format_highlight('mock data')}")
            Blufin::Terminal::command(commands_insert_data)
            Blufin::Terminal::info("Adding #{Blufin::Terminal::format_highlight('foreign key constraints')}")
            Blufin::Terminal::command(commands_build_fks)
            Blufin::Terminal::success('Reset of MySQL data done', 'You should now have a clean database.')

        end

    rescue => e

        Blufin::Terminal::print_exception(e)

    end

end