Class: Blufin::YmlSqlStructureWriter

Inherits:
YmlWriterBase show all
Defined in:
lib/core/yml_writers/yml_sql_structure_writer.rb

Constant Summary

Constants inherited from YmlWriterBase

Blufin::YmlWriterBase::AUTO_TYPES, Blufin::YmlWriterBase::PLACEHOLDER_CLASS, Blufin::YmlWriterBase::PLACEHOLDER_IMPORT, Blufin::YmlWriterBase::PLACEHOLDER_PACKAGE, Blufin::YmlWriterBase::PLACEHOLDER_SCHEMA

Instance Method Summary collapse

Methods inherited from YmlWriterBase

#get_base_path, #get_java_path, #get_package, #get_package_from_file, #write_file_java

Constructor Details

#initialize(site, schema_data) ⇒ YmlSqlStructureWriter

Returns a new instance of YmlSqlStructureWriter.

Raises:

  • (RuntimeError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/core/yml_writers/yml_sql_structure_writer.rb', line 5

def initialize(site, schema_data)

    @schema_data = schema_data

    raise RuntimeError, 'Could not find valid @schema_data.' if @schema_data.nil? || !@schema_data.is_a?(Hash)

    @site          = Blufin::SiteResolver::validate_site(site)
    @site_name     = Blufin::SiteResolver::get_site_name(@site)
    @site_domain   = Blufin::SiteResolver::get_site_domain(@site)
    @site_location = "#{Blufin::SiteResolver::get_site_location(@site)}/"

    @yml_enum_scanner = Blufin::ScannerJavaEnums.new(@site)

end

Instance Method Details

#writeObject



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/core/yml_writers/yml_sql_structure_writer.rb', line 20

def write

    # Remove ALL previous structure files.
    Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE.each do |schema|
        sfs = get_structure_files(schema)
        next if sfs.nil? # Skip if user doesn't have the PATH_TO_RUBY config value.
        structure_file   = sfs[0]
        foreign_key_file = sfs[1]
        Blufin::Files::delete_file(structure_file)
        Blufin::Files::delete_file(foreign_key_file)
    end

    @schema_data.each do |schema, schema_data|

        # This filters out the "mock" schema used for testing.
        next if schema == Blufin::YmlSchemaValidator::MOCK

        fk_count  = 0
        idx_count = 0

        @structure         = []
        @alter_table_lines = []
        @fks               = []
        @link_tables       = []

        schema_data.each_with_index do |(table, table_data), idx|

            if table_data.length > 0

                primary_key      = nil
                indexes          = []
                indexes_unique   = []
                indexes_fulltext = []
                foreign_keys     = []

                @structure << "-- Create #{table} table"
                @structure << "CREATE TABLE `#{table}` ("

                table_data.each do |column_name, column_data|

                    # Skip Placeholders
                    next if column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\[\]\z/ || column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\z/

                    @type = column_data[Blufin::YmlSchemaValidator::TYPE]
                    @fkey = !column_data[Blufin::YmlSchemaValidator::FKEY].nil?

                    # Skip Transient Objects.
                    next if [Blufin::ScannerJavaEmbeddedObjects::OBJECT].include?(@type)

                    # Create FKEY LINK tables
                    if column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/
                        @link_tables << [
                            "#{schema}.#{table}",
                            column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '')
                        ]
                        next
                    end

                    # Get the flags and check for indexes.
                    if column_data[Blufin::YmlSchemaValidator::FLAG].nil?
                        @flags = nil
                        indexes << "  INDEX `#{schema}_#{table}_#{column_name}_idx` (`#{column_name}`)," if @type =~ Blufin::YmlSchemaValidator::REGEX_ENUM
                    else
                        @flags      = Blufin::YmlCommon::extract_flags(column_data[Blufin::YmlSchemaValidator::FLAG])[0]
                        # See if this is a PRIMARY KEY
                        primary_key = column_name if @flags.primary_key
                        # See if there are any INDEXES
                        if @flags.index
                            if [Blufin::YmlSchemaValidator::TYPE_TEXT, Blufin::YmlSchemaValidator::TYPE_TEXT_LONG].include?(@type)
                                indexes_fulltext << "  FULLTEXT `#{schema}_#{table}_#{column_name}_idx_fulltext` (`#{column_name}`),"
                            else
                                indexes << "  INDEX `#{schema}_#{table}_#{column_name}_idx` (`#{column_name}`)," if @flags.unique.nil?
                                indexes_unique << "  UNIQUE INDEX `#{schema}_#{table}_#{column_name}_uniq_idx` (`#{column_name}`)," if @flags.unique
                            end
                        end
                    end

                    # Check for FKs
                    unless column_data[Blufin::YmlSchemaValidator::FKEY].nil?
                        fk_count          = fk_count + 1
                        fk_ref            = column_data[Blufin::YmlSchemaValidator::FKEY].split('.') # The target in form -> app.ebay_user.id
                        foreign_key_lines = []
                        foreign_key_lines << "ADD CONSTRAINT `fk_#{fk_count}`"
                        foreign_key_lines << "    FOREIGN KEY (`#{column_name}`)"
                        foreign_key_lines << "    REFERENCES `#{fk_ref[1]}` (`#{fk_ref[2]}`)"
                        foreign_key_lines << '    ON DELETE RESTRICT'
                        foreign_key_lines << '    ON UPDATE CASCADE,'
                        foreign_keys << foreign_key_lines
                    end

                    column_definition = "  `#{column_name}`"

                    determine_type(column_definition, column_data)
                    determine_not_null(column_definition, column_data)
                    determine_auto_increment(column_definition)
                    determine_comment(column_data[Blufin::YmlSchemaValidator::DESCRIPTION], column_definition)

                    @structure << "#{column_definition},"

                end

                # Add primary key (if any) ..
                @structure << "  PRIMARY KEY (`#{primary_key}`)," if primary_key != nil

                # Add indexes (if any) ..
                indexes_fulltext.each { |index_fulltext| @structure << index_fulltext } if indexes_fulltext.any?
                indexes_unique.each { |index_unique| @structure << index_unique } if indexes_unique.any?
                indexes.each { |index| @structure << index } if indexes.any?

                # Add FKs (if any) ..
                if foreign_keys.any?
                    @alter_table_lines << "-- Add #{table} FKs"
                    @alter_table_lines << "ALTER TABLE `#{table}`"
                    foreign_keys.each do |foreign_key_lines|
                        foreign_key_lines.each do |foreign_key_line|
                            @alter_table_lines << foreign_key_line
                        end
                    end
                    @alter_table_lines[@alter_table_lines.length - 1] = @alter_table_lines[@alter_table_lines.length - 1][0...-1]
                    @alter_table_lines[@alter_table_lines.length - 1] << ";\n"
                end

                @structure[@structure.length - 1] = @structure[@structure.length - 1][0...-1]
                @structure << ") ENGINE = InnoDB;#{idx == (schema_data.length - 1) ? '' : "\n"}"

            end

        end

        # Add LINK tables (if any) ..
        if @link_tables.any?
            @link_tables.each_with_index do |link_data, idx|
                source_table    = link_data[0].split('.')
                target_table    = link_data[1].split('.')
                link_table_name = Blufin::YmlCommon::get_link_table_name(source_table[1], link_data[1])

                # Create structure.
                @structure << "-- Add table to link '#{source_table[1]}' with '#{target_table[1]}'"
                @structure << "CREATE TABLE `#{link_table_name}` ("
                @structure << "  `#{source_table[1]}_id` INT NOT NULL,"
                @structure << "  `#{target_table[1]}_id` INT NOT NULL"
                @structure << ") ENGINE = InnoDB;#{idx == (@link_tables.length - 1) ? '' : "\n"}"

                # Create FKs and indexes.
                @fks << "ALTER TABLE `#{link_table_name}`"
                idx_count = idx_count + 1
                @fks << "  ADD INDEX `idx_#{idx_count}` (`#{source_table[1]}_id` DESC),"
                idx_count = idx_count + 1
                @fks << "  ADD INDEX `idx_#{idx_count}` (`#{target_table[1]}_id` DESC),"
                @fks << "  ADD UNIQUE INDEX `#{source_table[1]}_to_#{target_table[1]}_uniq_idx` (`#{source_table[1]}_id` DESC, `#{target_table[1]}_id` DESC),"
                fk_count = fk_count + 1
                @fks << "  ADD CONSTRAINT `fk_#{fk_count}`"
                @fks << "    FOREIGN KEY (`#{source_table[1]}_id`)"
                @fks << "    REFERENCES `#{source_table[1]}` (`id`)"
                @fks << '    ON DELETE RESTRICT'
                @fks << '    ON UPDATE RESTRICT,'
                fk_count = fk_count + 1
                @fks << "  ADD CONSTRAINT `fk_#{fk_count}`"
                @fks << "    FOREIGN KEY (`#{target_table[1]}_id`)"
                @fks << "    REFERENCES `#{target_table[1]}` (`id`)"
                @fks << '    ON DELETE RESTRICT'
                @fks << '    ON UPDATE RESTRICT;'
                @fks << '' unless idx == @link_tables.length - 1
            end
        end

        sfs = get_structure_files(schema)
        next if sfs.nil? # Skip if user doesn't have the PATH_TO_RUBY config value.
        structure_file   = sfs[0]
        foreign_key_file = sfs[1]

        Blufin::Files.write_file(structure_file, @structure).gsub(@site_location, '') if @structure.any?
        Blufin::Files.write_file(foreign_key_file, @alter_table_lines + @fks).gsub(@site_location, '') if @alter_table_lines.any? || @fks.any?

    end

end