forked from evilmartians/fias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.rb
106 lines (82 loc) · 2.33 KB
/
create.rb
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
105
106
# Can be run in cloned repo
# DATABASE_URL=postgres://localhost/fias bundle exec ruby create.rb
require 'pg_data_encoder'
require 'ruby-progressbar'
require 'sequel'
require 'active_support/core_ext/object/blank'
require 'fias'
def create_bar(count)
ProgressBar.create(total: count, format: '%a |%B| [%E] (%c/%C) %p%%')
end
DB = Sequel.connect(ENV['DATABASE_URL'])
PREFIX = ENV['PREFIX'] || 'fias'
FIAS_ADDRESS_OBJECTS_TABLE_NAME =
[PREFIX, 'address_objects'].delete_if(&:blank?).join('_').to_sym
FIAS_ADDRESS_OBJECTS = DB[FIAS_ADDRESS_OBJECTS_TABLE_NAME]
ADDRESS_OBJECTS_TABLE_NAME = :address_objects
ADDRESS_OBJECTS = DB[ADDRESS_OBJECTS_TABLE_NAME]
def create_table
puts 'Creating target table...'
DB.create_table(ADDRESS_OBJECTS_TABLE_NAME) do
primary_key :id
column :aoid, :uuid
column :aoguid, :uuid
column :parentguid, :uuid
column :parent_id, :integer
column :name, :text
column :abbr, :text
column :code, :text
column :center, :boolean
end
end
def copy_fias_data
puts 'Copying data from FIAS...'
encoder = PgDataEncoder::EncodeForCopy.new(
column_types: { 0 => :uuid, 1 => :uuid, 2 => :uuid }
)
# Nonhistorical records
scope = FIAS_ADDRESS_OBJECTS.where(livestatus: 1)
bar = create_bar(scope.count)
scope.each do |row|
bar.increment
encoder.add([
row[:aoid],
row[:aoguid],
row[:parentguid],
row[:formalname],
row[:shortname],
row[:code],
row[:centerst].to_i > 0
])
end
io = encoder.get_io
columns = %i(aoid aoguid parentguid name abbr code center)
DB.copy_into(ADDRESS_OBJECTS_TABLE_NAME, columns: columns, format: :binary) do
begin
io.readpartial(65_536)
rescue EOFError => _e
nil
end
end
end
def restore_hierarchy
puts 'Restoring parent_id values...'
Fias::Import::RestoreParentId.new(ADDRESS_OBJECTS).restore
end
create_table
copy_fias_data
restore_hierarchy
# Uncomment this migration if you want to use closure_tree for hierarchies:
#
# DB.create_table(:address_object_hierarchies) do
# column :ancestor_id, Integer
# column :descendant_id, Integer
# column :generations, Integer
# index [:ancestor_id, :descendant_id, :generations]
# index [:ancestor_id]
# index [:descendant_id]
# end
#
# Use http://github.com/gzigzigzeo/pg_closure_tree_rebuild to fill it.
#
# Database ready!