diff --git a/Rakefile b/Rakefile index 1cd1976009..953769d579 100644 --- a/Rakefile +++ b/Rakefile @@ -25,7 +25,7 @@ if %(development test).include?(Rails.env) end end -task :default => ["test:prepare", "spec:models", "spec:cartodb_lib", "spec:cartodb_acceptance"] +task :default => ["spec:models", "spec:cartodb_lib", "spec:cartodb_acceptance"] namespace :cartodb do namespace :api do diff --git a/app/controllers/admin/tables_controller.rb b/app/controllers/admin/tables_controller.rb index 55d9f69d0e..c365470952 100644 --- a/app/controllers/admin/tables_controller.rb +++ b/app/controllers/admin/tables_controller.rb @@ -10,18 +10,18 @@ class Admin::TablesController < ApplicationController per_page = 10 unless params[:public] @tags = Tag.load_user_tags(current_user.id, :limit => 5) - @tables = Table.filter(:user_id => current_user.id).order(:id).reverse.select(:id,:name,:privacy,:updated_at,:tags,:user_id,:stored_schema). + @tables = Table.filter(:user_id => current_user.id).order(:id).reverse. paginate(current_page, per_page, current_user.tables_count) else @tags = Tag.load_public_tags(current_user.id, :limit => 5) @tables = Table.filter(~{:user_id => current_user.id} & {:privacy => Table::PUBLIC}).order(:id).reverse. - select(:id,:name,:privacy,:updated_at,:tags,:user_id,:stored_schema).paginate(current_page, per_page) + paginate(current_page, per_page) render :template => 'admin/tables/index_public' and return end end def show - @table = Table.select(:id,:name,:privacy,:user_id,:tags,:stored_schema).filter(:id => params[:id]).first + @table = Table.filter(:id => params[:id]).first raise RecordNotFound if @table.nil? || (@table.user_id != current_user.id && @table.private?) end diff --git a/app/controllers/api/json/tables_controller.rb b/app/controllers/api/json/tables_controller.rb index 380b861b4d..5efd1d6460 100644 --- a/app/controllers/api/json/tables_controller.rb +++ b/app/controllers/api/json/tables_controller.rb @@ -103,8 +103,7 @@ class Api::Json::TablesController < Api::ApplicationController end def destroy - @table = Table.fetch("select id, user_id, name - from user_tables + @table = Table.fetch("select * from user_tables where user_tables.user_id = ? and user_tables.name = ?", current_user.id, params[:id]).all.first raise RecordNotFound if @table.nil? @table.destroy diff --git a/app/models/table.rb b/app/models/table.rb index d82ee93956..987297b2f8 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -45,6 +45,7 @@ class Table < Sequel::Model(:user_tables) # - set the cartodb schema, adding cartodb primary key, etc.. # - import the data if necessary def before_create + self.database_name = owner.database_name update_updated_at unless import_from_file.blank? handle_import_file! @@ -105,7 +106,11 @@ class Table < Sequel::Model(:user_tables) self.force_schema = nil $tables_metadata.hset key, "user_id", user_id end - + + def before_destroy + $tables_metadata.del key + end + def after_destroy super Tag.filter(:user_id => user_id, :table_id => id).delete @@ -114,7 +119,6 @@ class Table < Sequel::Model(:user_tables) user_database.drop_table(name.to_sym) user_database.run("DROP SEQUENCE IF EXISTS #{self.name}_cartodb_id_seq") end - $tables_metadata.del key end ## End of Callbacks @@ -168,7 +172,7 @@ class Table < Sequel::Model(:user_tables) end def key(new_name = nil) - 'rails:' + owner.database_name + ':' + (new_name || name) + 'rails:' + database_name + ':' + (new_name || name) end # TODO: use the database field @@ -482,7 +486,7 @@ TRIGGER @quote = (@quote == '"' || @quote.blank?) ? '\"' : @quote @quote = @quote == '`' ? '\`' : @quote command = "copy #{self.name} from STDIN WITH DELIMITER '#{@col_separator || ','}' CSV QUOTE AS '#{@quote}'" - system %Q{awk 'NR>1{print $0}' #{path} | `which psql` #{host} #{port} -U#{db_configuration['username']} -w #{owner.database_name} -c"#{command}"} + system %Q{awk 'NR>1{print $0}' #{path} | `which psql` #{host} #{port} -U#{db_configuration['username']} -w #{database_name} -c"#{command}"} owner.in_database do |user_database| #Check if the file had data, if not rise an error because probably something went wrong if user_database["SELECT * from #{self.name} LIMIT 1"].first.blank? @@ -768,8 +772,8 @@ TRIGGER self.name = self.imported_table_name.dup if self.name.blank? random_name = "importing_table_#{self.name}" Rails.logger.info "Table name to import: #{random_name}" - Rails.logger.info "Running shp2pgsql: `which shp2pgsql` -W#{importing_encoding} -s #{self.importing_SRID} #{path} #{random_name} | `which psql` #{host} #{port} -U#{owner.database_username} -w #{owner.database_name}" - system("`which shp2pgsql` -W#{importing_encoding} -s #{self.importing_SRID} #{path} #{random_name}| `which psql` #{host} #{port} -U#{owner.database_username} -w #{owner.database_name}") + Rails.logger.info "Running shp2pgsql: `which shp2pgsql` -W#{importing_encoding} -s #{self.importing_SRID} #{path} #{random_name} | `which psql` #{host} #{port} -U#{owner.database_username} -w #{database_name}" + system("`which shp2pgsql` -W#{importing_encoding} -s #{self.importing_SRID} #{path} #{random_name} | `which psql` #{host} #{port} -U#{owner.database_username} -w #{database_name}") owner.in_database do |user_database| imported_schema = user_database[random_name.to_sym].columns user_database.run("CREATE TABLE #{self.name} AS SELECT #{(imported_schema - [:the_geom]).join(',')},the_geom,ST_TRANSFORM(the_geom,#{CartoDB::GOOGLE_SRID}) as #{THE_GEOM_WEBMERCATOR} FROM #{random_name}") diff --git a/db/migrate/20110412143331_remove_stored_schema_from_user_tables.rb b/db/migrate/20110412143331_remove_stored_schema_from_user_tables.rb new file mode 100644 index 0000000000..ee1a7c6c64 --- /dev/null +++ b/db/migrate/20110412143331_remove_stored_schema_from_user_tables.rb @@ -0,0 +1,11 @@ +class RemoveStoredSchemaFromUserTablesMigration < Sequel::Migration + + def up + drop_column :user_tables, :stored_schema + end + + def down + add_column :user_tables, :stored_schema, 'character varying[]' + end + +end diff --git a/db/migrate/20110412143448_add_database_name_to_user_tables.rb b/db/migrate/20110412143448_add_database_name_to_user_tables.rb new file mode 100644 index 0000000000..f4d976f6d4 --- /dev/null +++ b/db/migrate/20110412143448_add_database_name_to_user_tables.rb @@ -0,0 +1,12 @@ +class AddDatabaseNameToUserTablesMigration < Sequel::Migration + + def up + add_column :user_tables, :database_name, String + + end + + def down + drop_column :user_tables, :database_name + end + +end diff --git a/lib/tasks/schemas.rake b/lib/tasks/schemas.rake index 6e378ab02d..68cf2d3b72 100644 --- a/lib/tasks/schemas.rake +++ b/lib/tasks/schemas.rake @@ -2,6 +2,9 @@ namespace :cartodb do desc 'Update all table schemas' task :update_table_schemas => :environment do Table.all.each do |table| + user = User[table.user_id] + table.database_name = user.database_name + table.save_changes table.update_stored_schema! end end diff --git a/spec/models/table_spec.rb b/spec/models/table_spec.rb index 2d86c0736a..f20cf38859 100644 --- a/spec/models/table_spec.rb +++ b/spec/models/table_spec.rb @@ -62,7 +62,7 @@ describe Table do it "should have a unique key to be identified in Redis" do table = create_table user = User[table.user_id] - table.key.should == "rails:#{user.database_name}:#{table.name}" + table.key.should == "rails:#{table.database_name}:#{table.name}" end it "should rename the entries in Redis when the table has been renamed" do @@ -71,7 +71,7 @@ describe Table do table.name = "brand_new_name" table.save_changes table.reload - table.key.should == "rails:#{user.database_name}:brand_new_name" + table.key.should == "rails:#{table.database_name}:brand_new_name" $tables_metadata.exists(table.key).should be_true end @@ -723,5 +723,11 @@ describe Table do record = table.record(pk) RGeo::GeoJSON.decode(record[:the_geom], :json_parser => :json).as_text.should == "Point(#{"%.6f" % -3.699732} #{"%.6f" % 40.423012})" end + + it "should store the name of its database" do + table = create_table + user = User[table.user_id] + table.database_name.should == user.database_name + end end