Skip to content

Commit

Permalink
#1693 Small refactor of carto_db initializer code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartones committed Jan 21, 2015
1 parent 3173b2b commit e71239c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
15 changes: 11 additions & 4 deletions app/models/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Table < Sequel::Model(:user_tables)
RESERVED_TABLE_NAMES = %W{ layergroup all }

# @see services/importer/lib/importer/column.rb -> RESERVED_WORDS
# @see config/initializers/carto_db.rb -> POSTGRESQL_RESERVED_WORDS & RESERVED_COLUMN_NAMES
# @see config/initializers/carto_db.rb -> RESERVED_COLUMN_NAMES
RESERVED_COLUMN_NAMES = %W{ oid tableoid xmin cmin xmax cmax ctid ogc_fid }
PUBLIC_ATTRIBUTES = {
:id => :id,
Expand All @@ -59,6 +59,8 @@ class Table < Sequel::Model(:user_tables)

DEFAULT_THE_GEOM_TYPE = 'geometry'

VALID_GEOMETRY_TYPES = %W{ geometry multipolygon point multilinestring }

# Associations
many_to_one :map
many_to_many :layers, join_table: :layers_user_tables,
Expand Down Expand Up @@ -1248,7 +1250,7 @@ def the_geom_type=(value)
else
value !~ /^multi/ ? "multi#{value.downcase}" : value.downcase
end
raise CartoDB::InvalidGeomType unless CartoDB::VALID_GEOMETRY_TYPES.include?(the_geom_type_value)
raise CartoDB::InvalidGeomType unless VALID_GEOMETRY_TYPES.include?(the_geom_type_value)
if owner.in_database.table_exists?(name)
$tables_metadata.hset(key, 'the_geom_type', the_geom_type_value)
else
Expand Down Expand Up @@ -1602,11 +1604,16 @@ def self.get_valid_table_name(name, options = {})
end

def get_new_column_type(invalid_column)
next_cartodb_type = {
"number" => "double precision",
"string" => "text"
}

flatten_cartodb_schema = schema.flatten
cartodb_column_type = flatten_cartodb_schema[flatten_cartodb_schema.index(invalid_column.to_sym) + 1]
flatten_schema = schema(cartodb_types: false).flatten
flatten_schema[flatten_schema.index(invalid_column.to_sym) + 1]
CartoDB::NEXT_TYPE[cartodb_column_type]
next_cartodb_type[cartodb_column_type]
end

def set_the_geom_column!(type = nil)
Expand Down Expand Up @@ -1650,7 +1657,7 @@ def set_the_geom_column!(type = nil)
end
end

raise "Error: unsupported geometry type #{type.to_s.downcase} in CartoDB" unless CartoDB::VALID_GEOMETRY_TYPES.include?(type.to_s.downcase)
raise "Error: unsupported geometry type #{type.to_s.downcase} in CartoDB" unless VALID_GEOMETRY_TYPES.include?(type.to_s.downcase)

type = type.to_s.upcase

Expand Down
49 changes: 11 additions & 38 deletions config/initializers/carto_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def self.secret_token
end

def self.domain
@@domain ||= if Rails.env.production? || Rails.env.staging?
@@domain ||= _get_domain
end

def _get_domain
if Rails.env.production? || Rails.env.staging?
`hostname -f`.strip
elsif Rails.env.development?
"vizzuality#{session_domain}"
Expand All @@ -59,7 +63,11 @@ def self.domain
end

def self.hostname
@@hostname ||= if Rails.env.production? || Rails.env.staging?
@@hostname ||= _get_hostname
end

def _get_hostname
if Rails.env.production? || Rails.env.staging?
"https://#{domain}"
elsif Rails.env.development?
"http://#{domain}:3000"
Expand All @@ -76,21 +84,6 @@ def self.account_path
Cartodb.config[:account_path]
end

# TODO move to separated file and activate in order
# to enable CartoDB plugins
# module Plugin
# class << self
# def register(plugin)
# @registered_plugins ||= []
# @registered_plugins << plugin
# end

# def registered
# @registered_plugins
# end
# end
# end

module API
VERSION_1 = "v1"
end
Expand All @@ -104,27 +97,7 @@ module API
PUBLIC_DB_USER = 'publicuser'
PUBLIC_DB_USER_PASSWORD = 'publicuser'
TILE_DB_USER = 'tileuser'
GOOGLE_SRID = 3857
SRID = 4326
USER_REQUESTS_PER_DAY = 10000

TYPES = {
"number" => ["smallint", /numeric\(\d+,\d+\)/, "integer", "bigint", "decimal", "numeric", "double precision", "serial", "big serial", "real"],
"string" => ["varchar", "character varying", "text", /character\svarying\(\d+\)/, /char\s*\(\d+\)/, /character\s*\(\d+\)/],
"boolean" => ["boolean"],
"date" => [
"timestamptz",
"timestamp with time zone",
"timestamp without time zone"
]
}

NEXT_TYPE = {
"number" => "double precision",
"string" => "text"
}

VALID_GEOMETRY_TYPES = %W{ geometry multipolygon point multilinestring }

# @see services/importer/lib/importer/column.rb -> RESERVED_WORDS
# @see app/models/table.rb -> RESERVED_COLUMN_NAMES
Expand All @@ -136,7 +109,7 @@ module API
REFERENCES RIGHT SELECT SESSION_USER SIMILAR SOME SYMMETRIC TABLE THEN TO TRAILING TRUE UNION UNIQUE USER
USING VERBOSE WHEN WHERE XMIN XMAX }

RESERVED_COLUMN_NAMES = %W{ FORMAT CONTROLLER ACTION }
RESERVED_COLUMN_NAMES = %W{ FORMAT CONTROLLER ACTION oid tableoid xmin cmin xmax cmax ctid ogc_fid }

LAST_BLOG_POSTS_FILE_PATH = "#{Rails.root}/public/system/last_blog_posts.html"

Expand Down
22 changes: 19 additions & 3 deletions config/initializers/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,31 @@ def strip_tags
self.gsub(/<[^>]+>/m,'').strip
end


def get_cartodb_types
{
"number" => ["smallint", /numeric\(\d+,\d+\)/, "integer", "bigint", "decimal", "numeric", "double precision", "serial", "big serial", "real"],
"string" => ["varchar", "character varying", "text", /character\svarying\(\d+\)/, /char\s*\(\d+\)/, /character\s*\(\d+\)/],
"boolean" => ["boolean"],
"date" => [
"timestamptz",
"timestamp with time zone",
"timestamp without time zone"
]
}
end

def convert_to_db_type
if CartoDB::TYPES.keys.include?(self.downcase)
cartodb_types = get_cartodb_types

if cartodb_types.keys.include?(self.downcase)
case (self.downcase)
when "number"
"double precision"
when "string"
"text"
else
CartoDB::TYPES[self.downcase].first
cartodb_types[self.downcase].first
end
else
self.downcase
Expand All @@ -152,7 +168,7 @@ def convert_to_db_type

# {"integer"=>:number, "real"=>:number, "varchar"=>:string, "text"=>:string, "timestamp"=>:date, "boolean"=>:boolean}
def convert_to_cartodb_type
inverse_types = CartoDB::TYPES.invert.inject({}){ |h, e| e.first.each{ |k| h[k] = e.last }; h}
inverse_types = get_cartodb_types.invert.inject({}){ |h, e| e.first.each{ |k| h[k] = e.last }; h}
if inverse_types.keys.include?(self.downcase)
inverse_types[self.downcase]
else
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/db_maintenance.rake
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ namespace :cartodb do
col[:geometrytype]
end
geometry_type ||= "POINT"
user_database.run("SELECT public.AddGeometryColumn('#{user.database_schema}','#{table.name}','#{Table::THE_GEOM_WEBMERCATOR}',#{CartoDB::GOOGLE_SRID},'#{geometry_type}',2)")
user_database.run("SELECT public.AddGeometryColumn('#{user.database_schema}','#{table.name}','#{Table::THE_GEOM_WEBMERCATOR}',3857,'#{geometry_type}',2)")
user_database.run("CREATE INDEX #{table.name}_#{Table::THE_GEOM_WEBMERCATOR}_idx ON #{table.name} USING GIST(#{Table::THE_GEOM_WEBMERCATOR})")
user_database.run("ANALYZE #{table.name}")
table.save_changes
Expand Down
3 changes: 1 addition & 2 deletions services/importer/lib/importer/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class Column
KML_MULTI_RE = /<Line|<Polygon/
KML_POINT_RE = /<Point>/
DEFAULT_SCHEMA = 'cdb_importer'
# @see app/models/table.rb -> RESERVED_COLUMN_NAMES
# @see config/initializers/carto_db.rb -> POSTGRESQL_RESERVED_WORDS & RESERVED_COLUMN_NAMES
# @see config/initializers/carto_db.rb -> POSTGRESQL_RESERVED_WORDS
RESERVED_WORDS = %w{ ALL ANALYSE ANALYZE AND ANY ARRAY AS ASC ASYMMETRIC
AUTHORIZATION BETWEEN BINARY BOTH CASE CAST CHECK
COLLATE COLUMN CONSTRAINT CREATE CROSS CURRENT_DATE
Expand Down

0 comments on commit e71239c

Please sign in to comment.