1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
|
#!/usr/bin/env ruby
#
# Copyright(C) 2016 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
require "pp"
require "json"
require "groonga/command/parser"
if ARGV.empty?
puts("Usage:")
puts(" #{$0} schema.grn object_list_result.json")
puts(" #{$0} schema.grn < object_list_result.json")
puts(" groonga DB_PATH object_list | #{$0} schema.grn")
exit(false)
end
schema_grn = ARGV.shift
schema = {}
Groonga::Command::Parser.parse(File.read(schema_grn)) do |type, *args|
case type
when :on_command
command, = args
case command.name
when "table_create"
if command.table_no_key?
type = "table:no_key"
elsif command.table_pat_key?
type = "table:pat_key"
elsif command.table_dat_key?
type = "table:dat_key"
else
type = "table:hash_key"
end
name = command[:name]
schema[name] ||= []
schema[name] << {
:type => type,
:flags => command.flags.join("|"),
}
when "column_create"
if command.column_index?
type = "column:index"
elsif command.column_vector? or command.type == "ShortText"
type = "column:var_size"
else
type = "column:fix_size"
end
name = "#{command[:table]}.#{command[:name]}"
schema[name] ||= []
schema[name] << {
:type => type,
:flags => command.flags.join("|"),
}
end
end
end
MAX_RESERVED_ID = 255
response = JSON.parse(ARGF.read)
body = response[1]
body.each do |name, object|
id = object["id"]
next if id <= MAX_RESERVED_ID
normalized_name = name.gsub(/\d{4}\d{2}(?:\d{2})?/, "YYYYMMDD")
definitions = schema[normalized_name]
if definitions.nil?
next if object["type"]["name"] == "proc"
puts("Unknown table/column: #{name}(#{id})")
exit(false)
end
type = object["type"]
if type.nil?
puts("[invalid][no-type] #{id}:#{name}")
puts(PP.pp(object, "").gsub(/^/, " "))
next
end
type_name = type["name"]
valid_type_names = definitions.collect {|definition| definition[:type]}
unless valid_type_names.include?(type["name"])
expected = "expected:[#{valid_type_names.join(", ")}]"
puts("[invalid][wrong-type] #{id}:#{name} <#{type_name}> #{expected}")
puts(PP.pp(object, "").gsub(/^/, " "))
puts(PP.pp(definitions, "").gsub(/^/, " "))
next
end
end
|