pasqual 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b72c3b9c9bf68e2d65a72a8e52c21b23bae4d3c
4
- data.tar.gz: baf933fc8e884fd46773c521c1b9b7daffed2443
3
+ metadata.gz: 16d0bb1110e38f1ff70ae8c108949c29052b8faa
4
+ data.tar.gz: 87c42ab7e84370f7d448117b227c717439c82e3f
5
5
  SHA512:
6
- metadata.gz: dddc6b3cf7f6c86a1974c53aabc1db702b7f4cb5ae16a0adfee30f51abd63b3d921a3bb2480aea3db856ecb4b21fbbe2351b37b2379a3ff30c53f108e6db473a
7
- data.tar.gz: c5e077062401bc2e0bc42d0f29839453715680070bab6582f03474af57f91912265954aa8cb50db5fb4ead9f66d5ad404ab6b9802eea20d4801fccca0c6cddef
6
+ metadata.gz: 74fbc561d80a6258ba47bb5ac43719d607cebedf6179160aac00d02f933d120152d40de391b98b93aa72ccd2d648cb46e7db82541bafaec95ce0af750f94d045
7
+ data.tar.gz: b9473965169ecb96b5368b21069f436c59308843e0fbaa0a514cb8b17b011470fb08893889868f4236b0be16088569f55eed272f9fb63a6d20d79d5d16113759
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ bin/rspec
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Pasqual
2
2
 
3
- TODO: Write a gem description
3
+ Run Postgres CLI commands with the help of database settings configured with environment variables.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,54 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ First, get an instance of Pasqual:
24
+
25
+ ``` ruby
26
+ psql = Pasqual.for ENV['DATABASE_URL']
27
+ ```
28
+
29
+ ### createdb
30
+
31
+ Createdb automatically uses the database name defined in `ENV`.
32
+
33
+ ```ruby
34
+ psql.createdb
35
+ ```
36
+
37
+ A custom name can optionally be specified:
38
+
39
+ ```ruby
40
+ psql.createdb 'foodb'
41
+ ```
42
+
43
+ ### dropdb
44
+
45
+ Dropdb automatically uses the database name defined in `ENV`.
46
+
47
+ ```ruby
48
+ psql.dropdb
49
+ ```
50
+ A custom name can optionally be specified:
51
+
52
+ ```ruby
53
+ psql.dropdb 'foodb'
54
+ ```
55
+
56
+ ### command
57
+
58
+ Executes an SQL script, the same as piping text into the psql command.
59
+
60
+ ```ruby
61
+ psql.command "SELECT * from users;"
62
+ ```
63
+
64
+ ### pipe
65
+
66
+ You can pipe a file into the `psql` command:
67
+
68
+ ```ruby
69
+ psql.pipe_sql '/path/to/file.sql'
70
+ ```
24
71
 
25
72
  ## Contributing
26
73
 
@@ -4,20 +4,21 @@ module Pasqual
4
4
 
5
5
  class Command
6
6
 
7
- def self.execute(program, username, password, host, port, name)
8
- new(program, username, password, host, port, name).tap { |c| c.execute }
7
+ def self.execute(program, username, password, host, port, name, file = nil)
8
+ new(program, username, password, host, port, name, file).tap { |c| c.execute }
9
9
  end
10
10
 
11
- attr_accessor :program, :username, :password, :host, :port, :name,
11
+ attr_accessor :program, :username, :password, :host, :port, :name, :file,
12
12
  :output, :status
13
13
 
14
- def initialize(program, username, password, host, port, name)
14
+ def initialize(program, username, password, host, port, name, file = nil)
15
15
  self.program = program
16
16
  self.username = username
17
17
  self.password = password
18
18
  self.host = host
19
19
  self.port = port
20
20
  self.name = name
21
+ self.file = file
21
22
  end
22
23
 
23
24
  def execute
@@ -29,17 +30,27 @@ module Pasqual
29
30
 
30
31
  process.io.stdout = process.io.stderr = outfile
31
32
 
32
- if password
33
- # TODO: find out why piping to stdin doesn't work :(
34
- ENV['PGPASSWORD'] = password
35
- process.start
36
- process.poll_for_exit(30)
37
- ENV['PGPASSWORD'] = nil
38
- else
39
- process.start
40
- process.poll_for_exit(30)
33
+ process.duplex = true if file
34
+
35
+ # TODO: find out why piping to stdin doesn't work :(
36
+ ENV['PGPASSWORD'] = password
37
+ process.start
38
+
39
+ if file && File.exist?(file)
40
+ File.open file do |f|
41
+ process.io.stdin.puts f.read
42
+ process.io.stdin.flush
43
+ end
44
+ process.io.stdin.close
45
+ elsif file
46
+ process.io.stdin.puts file
47
+ process.io.stdin.flush
48
+ process.io.stdin.close
41
49
  end
42
50
 
51
+ process.poll_for_exit(30)
52
+ ENV['PGPASSWORD'] = nil
53
+
43
54
  outfile.rewind
44
55
  self.output = outfile.read
45
56
  self.status = process.exit_code
@@ -1,6 +1,7 @@
1
1
  require 'uri'
2
2
  require 'pasqual/createdb'
3
3
  require 'pasqual/dropdb'
4
+ require 'pasqual/psql'
4
5
 
5
6
  module Pasqual
6
7
 
@@ -20,12 +21,20 @@ module Pasqual
20
21
  @port ||= 5432
21
22
  end
22
23
 
23
- def createdb(name)
24
- Createdb.execute username, password, host, port, name
24
+ def createdb(create_name = name)
25
+ Createdb.execute username, password, host, port, create_name
25
26
  end
26
27
 
27
- def dropdb(name)
28
- Dropdb.execute username, password, host, port, name
28
+ def dropdb(drop_name = name)
29
+ Dropdb.execute username, password, host, port, drop_name
30
+ end
31
+
32
+ def command(statement)
33
+ Psql.command statement, username, password, host, port, name
34
+ end
35
+
36
+ def pipe_sql(file)
37
+ Psql.pipe file, username, password, host, port, name
29
38
  end
30
39
 
31
40
  end
@@ -0,0 +1,22 @@
1
+ module Pasqual
2
+
3
+ module Psql
4
+ class Failed < StandardError; end
5
+
6
+ def self.pipe(file, username, password, host, port, name)
7
+ cmd = Command.execute 'psql', username, password, host, port, name, file
8
+
9
+ raise Failed unless cmd.success?
10
+ true
11
+ end
12
+
13
+ def self.command(statement, username, password, host, port, name)
14
+ cmd = Command.execute 'psql', username, password, host, port, name, statement
15
+
16
+ raise Failed unless cmd.success?
17
+ cmd.output
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -1,3 +1,3 @@
1
1
  module Pasqual
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
data/pasqual.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["dkastner@gmail.com"]
11
11
  spec.summary = %q{Interface easily with postgres CLI tools}
12
12
  spec.description = %q{Shortcuts for postgres commands, with option to use ENV-configured conenction URLs}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/dkastner/pasqual"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'securerandom'
2
3
  require 'pasqual/database'
3
4
 
4
5
  describe Pasqual::Database do
@@ -25,5 +26,28 @@ describe Pasqual::Database do
25
26
 
26
27
  end
27
28
 
29
+ describe '#command' do
30
+
31
+ it 'runs an sql command' do
32
+ expect do
33
+ database.command "SELECT 0;"
34
+ end.to_not raise_error
35
+ end
36
+
37
+ end
38
+
39
+ describe '#pipe_sql' do
40
+
41
+ it 'runs an sql script file' do
42
+ file = Tempfile.new 'sql-script'
43
+ file.puts "SELECT 0;"
44
+
45
+ expect do
46
+ database.pipe_sql file.path
47
+ end.to_not raise_error
48
+ end
49
+
50
+ end
51
+
28
52
  end
29
53
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pasqual
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Kastner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-01 00:00:00.000000000 Z
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: childprocess
@@ -70,8 +70,7 @@ description: Shortcuts for postgres commands, with option to use ENV-configured
70
70
  URLs
71
71
  email:
72
72
  - dkastner@gmail.com
73
- executables:
74
- - rspec
73
+ executables: []
75
74
  extensions: []
76
75
  extra_rdoc_files: []
77
76
  files:
@@ -80,19 +79,19 @@ files:
80
79
  - LICENSE.txt
81
80
  - README.md
82
81
  - Rakefile
83
- - bin/rspec
84
82
  - lib/pasqual.rb
85
83
  - lib/pasqual/arglist.rb
86
84
  - lib/pasqual/command.rb
87
85
  - lib/pasqual/createdb.rb
88
86
  - lib/pasqual/database.rb
89
87
  - lib/pasqual/dropdb.rb
88
+ - lib/pasqual/psql.rb
90
89
  - lib/pasqual/version.rb
91
90
  - pasqual.gemspec
92
91
  - spec/pasqual/database_spec.rb
93
92
  - spec/pasqual_spec.rb
94
93
  - spec/spec_helper.rb
95
- homepage: ''
94
+ homepage: https://github.com/dkastner/pasqual
96
95
  licenses:
97
96
  - MIT
98
97
  metadata: {}
data/bin/rspec DELETED
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This file was generated by Bundler.
4
- #
5
- # The application 'rspec' is installed as part of a gem, and
6
- # this file is here to facilitate running it.
7
- #
8
-
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
- Pathname.new(__FILE__).realpath)
12
-
13
- require 'rubygems'
14
- require 'bundler/setup'
15
-
16
- load Gem.bin_path('rspec-core', 'rspec')