lib/pgslice.rb in pgslice-0.4.1 vs lib/pgslice.rb in pgslice-0.4.2

- old
+ new

@@ -80,10 +80,17 @@ if declarative && !options[:no_partition] queries << <<-SQL CREATE TABLE #{quote_table(intermediate_table)} (LIKE #{quote_table(table)} INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING STORAGE INCLUDING COMMENTS) PARTITION BY RANGE (#{quote_table(column)}); SQL + if server_version_num >= 110000 + index_defs = execute("SELECT pg_get_indexdef(indexrelid) FROM pg_index WHERE indrelid = #{regclass(table)} AND indisprimary = 'f'").map { |r| r["pg_get_indexdef"] } + index_defs.each do |index_def| + queries << index_def.sub(/ ON \S+ USING /, " ON #{quote_table(intermediate_table)} USING ").sub(/ INDEX .+ ON /, " INDEX ON ") + ";" + end + end + # add comment cast = column_cast(table, column) queries << <<-SQL COMMENT ON TABLE #{quote_table(intermediate_table)} is 'column:#{column},period:#{period},cast:#{cast}'; SQL @@ -173,11 +180,18 @@ original_table else existing_partitions(original_table, period).last end - index_defs = execute("SELECT pg_get_indexdef(indexrelid) FROM pg_index WHERE indrelid = #{regclass(schema_table)} AND indisprimary = 'f'").map { |r| r["pg_get_indexdef"] } + # indexes automatically propagate in Postgres 11+ + index_defs = + if !declarative || server_version_num < 110000 + execute("SELECT pg_get_indexdef(indexrelid) FROM pg_index WHERE indrelid = #{regclass(schema_table)} AND indisprimary = 'f'").map { |r| r["pg_get_indexdef"] } + else + [] + end + fk_defs = foreign_keys(schema_table) primary_key = self.primary_key(schema_table) added_partitions = [] range.each do |n| @@ -291,11 +305,16 @@ schema_table = period && declarative ? existing_tables.last : table primary_key = self.primary_key(schema_table)[0] abort "No primary key" unless primary_key - max_source_id = max_id(source_table, primary_key) + max_source_id = nil + begin + max_source_id = max_id(source_table, primary_key) + rescue PG::UndefinedFunction + abort "Only numeric primary keys are supported" + end max_dest_id = if options[:start] options[:start] elsif options[:swapped] @@ -452,25 +471,23 @@ def connection @connection ||= begin url = options[:url] || ENV["PGSLICE_URL"] abort "Set PGSLICE_URL or use the --url option" unless url + uri = URI.parse(url) - uri_parser = URI::Parser.new - config = { - host: uri.host, - port: uri.port, - dbname: uri.path.sub(/\A\//, ""), - user: uri.user, - password: uri.password, - connect_timeout: 1 - }.reject { |_, value| value.to_s.empty? } - config.map { |key, value| config[key] = uri_parser.unescape(value) if value.is_a?(String) } - @schema = CGI.parse(uri.query.to_s)["schema"][0] || "public" - PG::Connection.new(config) + params = CGI.parse(uri.query.to_s) + # remove schema + @schema = Array(params.delete("schema") || "public")[0] + uri.query = URI.encode_www_form(params) + + ENV["PGCONNECT_TIMEOUT"] ||= "1" + PG::Connection.new(uri.to_s) end rescue PG::ConnectionBad => e abort e.message + rescue URI::InvalidURIError + abort "Invalid url" end def schema connection # ensure called first @schema