diff --git a/app/models/table.rb b/app/models/table.rb index 307fc1783348..d04dc59273e8 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -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, @@ -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, @@ -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 @@ -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) @@ -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 diff --git a/config/initializers/carto_db.rb b/config/initializers/carto_db.rb index 6b1321d08466..31a96d7bf58a 100644 --- a/config/initializers/carto_db.rb +++ b/config/initializers/carto_db.rb @@ -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}" @@ -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" @@ -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 @@ -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 @@ -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" diff --git a/config/initializers/string.rb b/config/initializers/string.rb index 1e70ffeef3c1..d7cd9e365129 100644 --- a/config/initializers/string.rb +++ b/config/initializers/string.rb @@ -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 @@ -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 diff --git a/lib/tasks/db_maintenance.rake b/lib/tasks/db_maintenance.rake index 31547256ba0b..40e3aeb9be79 100644 --- a/lib/tasks/db_maintenance.rake +++ b/lib/tasks/db_maintenance.rake @@ -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 diff --git a/services/importer/lib/importer/column.rb b/services/importer/lib/importer/column.rb index bda560a4577d..fead2178621f 100644 --- a/services/importer/lib/importer/column.rb +++ b/services/importer/lib/importer/column.rb @@ -14,8 +14,7 @@ class Column KML_MULTI_RE = // 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