-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_tables.rb
executable file
·35 lines (28 loc) · 1.08 KB
/
create_tables.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
#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'byebug'
def normalize_types(type, sql_type)
if (['enum']).include?(type) || (['hstore', 'json', 'jsonb', 'text']).include?(sql_type) || sql_type.include?('character varying')
'CHARACTER VARYING(max)'
elsif %w(uuid).include?(sql_type)
# https://gist.github.com/wrobstory/4b0ce4e8ba51ec40c494881bc126c003
'CHAR(36)'
elsif %w(datetime).include?(type)
# Redshift timestamp column does not support precision, but some of our newer timestamp columns include it,
# so normalize on the non-precision version
'TIMESTAMP WITHOUT TIME ZONE'
else
sql_type
end
end
uri = URI('https://demo.controlshiftlabs.com/api/bulk_data/schema.json')
json = Net::HTTP.get(uri)
tables = JSON.parse(json)['tables']
tables.each do |table|
name = table['table']['name']
columns = table['table']['columns']
column_fields = columns.collect{|col, attrs| "\"#{col}\" #{normalize_types(attrs["type"], attrs["sql_type"])}" }.join(', ')
puts "DROP TABLE IF EXISTS #{name};"
puts "CREATE TABLE #{name} (#{column_fields});"
end