Class: Dbdoc::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/dbdoc/manager.rb

Overview

Dbdoc::Manager class manages database schema documentation.

It knows how to generate and update documentation based on the database schema generated by the schema query for user’s database.

Instance Method Summary collapse

Constructor Details

#initialize(local_path: Dir.pwd) ⇒ Manager

Returns a new instance of Manager.



14
15
16
17
# File 'lib/dbdoc/manager.rb', line 14

def initialize(local_path: Dir.pwd)
  @local_path = local_path
  @config = Dbdoc::Config.new(local_path: local_path).load
end

Instance Method Details

#applyObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity



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
# File 'lib/dbdoc/manager.rb', line 55

def apply
  puts "--> APPLY"
  puts
  puts

  input_schema = read_input_schema.map { |r| r.first(4) }.map { |r| r.join(":") }
  current_schema = read_documented_schema

  added_columns = input_schema - current_schema
  dropped_columns = current_schema - input_schema

  doc_folder = File.join(@local_path, "doc")

  Dir.mkdir(doc_folder) unless Dir.exist?(doc_folder)

  ## DROP COLUMNS
  dropped_columns.each do |column|
    schema_name, table_name, column_name = column.split(":")

    columns_file = File.join(doc_folder, schema_name, table_name, "columns.yml")
    next unless File.exist?(columns_file)

    columns = YAML.load(File.read(columns_file))
    columns.reject! { |c| c[:name] == column_name }
    columns.each { |c| c[:description].strip! }

    File.open(columns_file, "w") do |f|
      f.puts(columns.to_yaml)
    end
  end

  ## DROP EMPTY TABLES
  Dir.entries(doc_folder).each do |schema_name|
    next if schema_name == "."
    next if schema_name == ".."

    schema_folder = File.join(doc_folder, schema_name)
    next unless File.directory?(File.join(doc_folder, schema_name))

    Dir.entries(schema_folder).each do |table_name|
      next if table_name == "."
      next if table_name == ".."

      table_folder = File.join(schema_folder, table_name)
      next unless File.directory?(table_folder)

      columns_file = File.join(table_folder, "columns.yml")
      next unless File.exist?(columns_file)

      columns = YAML.load(File.read(columns_file))

      if columns.empty?
        puts "--> DELETING #{schema_name}.#{table_name}"
        FileUtils.rm_rf(table_folder)
      end
    end
  end

  ## DROP EMPTY SCHEMAS
  Dir.entries(doc_folder).each do |schema_name|
    next if schema_name == "."
    next if schema_name == ".."

    schema_folder = File.join(doc_folder, schema_name)
    next unless File.directory?(schema_folder)

    FileUtils.rm_rf(schema_folder) if Dir.empty?(schema_folder)
  end

  create_new_columns(added_columns)
end

#planObject



19
20
21
22
23
24
25
26
27
# File 'lib/dbdoc/manager.rb', line 19

def plan
  input_schema = read_input_schema.map { |r| r.first(4).join(":") }
  current_schema = read_documented_schema

  {
    new_columns: input_schema - current_schema,
    columns_to_drop: current_schema - input_schema
  }
end

#queryObject



47
48
49
50
51
52
# File 'lib/dbdoc/manager.rb', line 47

def query
  db_type = @config["db"]["type"]
  query_file = File.join(File.expand_path(__dir__), "../..", "config", "schema_queries", "#{db_type}.sql")

  File.read(query_file)
end

#todoObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dbdoc/manager.rb', line 29

def todo
  doc_folder_files = File.join(@local_path, "doc", "**/*")

  Dir[doc_folder_files].each do |file|
    next if file == "."
    next if file == ".."
    next if File.directory?(file)

    File.read(file).split("\n").each_with_index do |line, i|
      next unless line.include?("TODO")

      relative_path = file.gsub(@local_path, "")

      puts "#{relative_path}:#{i + 1}"
    end
  end
end