Sha256: 6cf4600df7d9fa7d53982fb30891f8fcfa3ad6f1ad4eed0edd4bf35e425227c6

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

#! /usr/bin/env ruby

require "sequel"
Sequel::Model.plugin :subclasses

DATABASE_CREDENTIALS = {
  adapter: "postgres",
  database: ENV["DATABASE"] || "postgres",
  username: ENV["DATABASE_USERNAME"] || "postgres",
  password: ENV["DATABASE_PASSWORD"] || "postgres",
  host: "localhost",
  :encoding => "utf8"
}
Sequel.connect(DATABASE_CREDENTIALS)

$LOAD_PATH.unshift "./lib"
$LOAD_PATH.unshift "./tasks"
ENV["RACK_ENV"] = "development"
require "pact_broker/db/models"

def generate_erd_graphviz()
  associations = []
  Sequel::Model.descendents.each do |model|
    next if model.name.nil?
    model.associations.each do |a|
      ar = model.association_reflection(a)
      associations << [model.name, ar[:type], ar.associated_class.name]
    end
  end
  styles = {
    :many_to_one=>:bold,
    :one_to_many=>:solid,
    :many_to_many=>:dashed,
    :one_to_one=>:dotted
  }

  graph = "digraph G {\n"
  graph += associations.uniq.map{|c, t, ac| "  \"#{c}\" -> \"#{ac}\" [style=#{styles[t]}];"}.sort.join("\n")
  graph += "\n}"

  graph
end

puts "Usage:"
puts "generate-erd [erd-filename]"
puts "CAUTION: Please make sure that you have graphviz installed"
puts

GRAPHVIZ_FILENAME = ARGV[0] || "erd"

puts "Generating graphviz file ..."
File.write("#{GRAPHVIZ_FILENAME}.dot", generate_erd_graphviz())

puts "Converting graphviz file to a pdf"
`dot -Tpdf "#{GRAPHVIZ_FILENAME}.dot" > "#{GRAPHVIZ_FILENAME}.pdf"`

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pact_broker-2.89.1 script/generate-erd
pact_broker-2.89.0 script/generate-erd
pact_broker-2.88.0 script/generate-erd
pact_broker-2.87.0 script/generate-erd
pact_broker-2.86.0 script/generate-erd