Sha256: 7ada6b70bd949e9175b8944e0b0ecc22828dd6e71d0d0443ed72a0b43aa1b535

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module DatabaseDocumenter::TablesSql
  def self.generate
    configuration = DatabaseDocumenter.configuration.database_configuration

    tables_sql = generate_sql_file(configuration)

    send("process_#{configuration['adapter']}_sql", tables_sql)
  end

  def self.generate_sql_file(configuration)
    ActiveRecord::Tasks::DatabaseTasks.structure_dump(configuration, 'database.sql')
    tables_sql = IO.read('database.sql')
    File.delete('database.sql')
    tables_sql
  end

  def self.process_postgresql_sql(tables_sql)
    tables_sql_hash = {}
    tables_sql = tables_sql.split('--').select { |line| line.match(/CREATE TABLE/) }

    tables_sql.each do |sql_statement|
      key = sql_statement.scan(/public.(.*) \(/)[0][0]
      broken_cell_para = Caracal::Core::Models::TableCellModel.new do |c|
        sql_statement.strip.squeeze(' ').split("\n").each do |p|
          c.p p
        end
      end
      tables_sql_hash[key] = broken_cell_para
    end

    tables_sql_hash
  end

  def self.process_mysql2_sql(tables_sql)
    tables_sql_hash = {}
    tables_sql = tables_sql.split(';').select { |line| line.match(/CREATE/) }

    tables_sql.each do |sql_statement|
      key = sql_statement.scan(/`(.*)`/)[0][0]
      broken_cell_para = Caracal::Core::Models::TableCellModel.new do |c|
        sql_statement[1..-1].squeeze(' ').split("\n").each do |p|
          c.p p
        end
      end
      tables_sql_hash[key] = broken_cell_para
    end

    tables_sql_hash
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
database_documenter-0.1.11 lib/database_documenter/tables_sql.rb
database_documenter-0.1.10 lib/database_documenter/tables_sql.rb
database_documenter-0.1.9 lib/database_documenter/tables_sql.rb
database_documenter-0.1.8 lib/database_documenter/tables_sql.rb
database_documenter-0.1.7 lib/database_documenter/tables_sql.rb