lib/pgtk/pgsql_task.rb in pgtk-0.7.5 vs lib/pgtk/pgsql_task.rb in pgtk-0.8.0

- old
+ new

@@ -1,8 +1,8 @@ # frozen_string_literal: true -# Copyright (c) 2019 Yegor Bugayenko +# Copyright (c) 2019-2023 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -30,34 +30,25 @@ require 'yaml' require_relative '../pgtk' # Pgsql rake task. # Author:: Yegor Bugayenko (yegor256@gmail.com) -# Copyright:: Copyright (c) 2019 Yegor Bugayenko +# Copyright:: Copyright (c) 2019-2023 Yegor Bugayenko # License:: MIT class Pgtk::PgsqlTask < Rake::TaskLib - attr_accessor :name - attr_accessor :dir - attr_accessor :fresh_start - attr_accessor :user - attr_accessor :password - attr_accessor :dbname - attr_accessor :yaml - attr_accessor :quiet - attr_accessor :port + attr_accessor :name, :dir, :fresh_start, :user, :password, :dbname, :yaml, :quiet, :port def initialize(*args, &task_block) + super() @name = args.shift || :pgsql @fresh_start = false @quite = false @user = 'test' @password = 'test' @dbname = 'test' @port = nil - unless ::Rake.application.last_description - desc 'Start a local PostgreSQL server' - end + desc 'Start a local PostgreSQL server' unless ::Rake.application.last_description task(name, *args) do |_, task_args| RakeFileUtils.send(:verbose, true) do yield(*[self, task_args].slice(0, task_block.arity)) if block_given? run end @@ -69,16 +60,14 @@ def run raise "Option 'dir' is mandatory" unless @dir raise "Option 'yaml' is mandatory" unless @yaml home = File.expand_path(@dir) FileUtils.rm_rf(home) if @fresh_start - if File.exist?(home) - raise "Directory/file #{home} is present, use fresh_start=true" - end + raise "Directory/file #{home} is present, use fresh_start=true" if File.exist?(home) out = "2>&1 #{@quiet ? '>/dev/null' : ''}" Tempfile.open do |pwfile| - IO.write(pwfile.path, @password) + File.write(pwfile.path, @password) system( [ 'initdb --auth=trust', "-D #{Shellwords.escape(home)}", '--username', @@ -96,11 +85,11 @@ puts "Random TCP port #{port} is used" else puts "Required TCP port #{port} is used" end pid = Process.spawn('postgres', '-k', home, '-D', home, "--port=#{port}") - IO.write(File.join(@dir, 'pid'), pid) + File.write(File.join(@dir, 'pid'), pid) at_exit do `kill -TERM #{pid}` puts "PostgreSQL killed in PID #{pid}" end sleep 1 @@ -121,10 +110,10 @@ sleep(5) attempt += 1 raise if attempt > 10 retry end - IO.write( + File.write( @yaml, { 'pgsql' => { 'host' => 'localhost', 'port' => port,