lib/dexter/indexer.rb in pgdexter-0.1.2 vs lib/dexter/indexer.rb in pgdexter-0.1.3

- old
+ new

@@ -104,11 +104,13 @@ # create hypothetical indexes candidates = {} columns(tables).each do |col| unless index_set.include?([col[:table], [col[:column]]]) - candidates[col] = select_all("SELECT * FROM hypopg_create_index('CREATE INDEX ON #{col[:table]} (#{[col[:column]].join(", ")})');").first["indexname"] + unless ["json", "jsonb"].include?(col[:type]) + candidates[col] = select_all("SELECT * FROM hypopg_create_index('CREATE INDEX ON #{quote_ident(col[:table])} (#{[col[:column]].map { |c| quote_ident(c) }.join(", ")})')").first["indexname"] + end end end candidates end @@ -188,19 +190,19 @@ # 1. create lock # 2. refresh existing index list # 3. create indexes that still don't exist # 4. release lock new_indexes.each do |index| - statement = "CREATE INDEX CONCURRENTLY ON #{index[:table]} (#{index[:columns].join(", ")})" + statement = "CREATE INDEX CONCURRENTLY ON #{quote_ident(index[:table])} (#{index[:columns].map { |c| quote_ident(c) }.join(", ")})" log "Creating index: #{statement}" started_at = Time.now select_all(statement) log "Index created: #{((Time.now - started_at) * 1000).to_i} ms" end end else - log "No indexes found" + log "No new indexes found" end new_indexes end @@ -220,11 +222,11 @@ rescue PG::ConnectionBad abort "Bad database url" end def select_all(query) - # use exec_params instead of exec for securiy + # use exec_params instead of exec for security # # Unlike PQexec, PQexecParams allows at most one SQL command in the given string. # (There can be semicolons in it, but not more than one nonempty command.) # This is a limitation of the underlying protocol, but has some usefulness # as an extra defense against SQL-injection attacks. @@ -258,18 +260,19 @@ def columns(tables) columns = select_all <<-SQL SELECT table_name, - column_name + column_name, + data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name IN (#{tables.map { |t| quote(t) }.join(", ")}) SQL - columns.map { |v| {table: v["table_name"], column: v["column_name"]} } + columns.map { |v| {table: v["table_name"], column: v["column_name"], type: v["data_type"]} } end def indexes(tables) select_all(<<-SQL SELECT