pasqual 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fa2a389b07160cc2665fb9479244fb58105c6a8
4
- data.tar.gz: 96f7e56d1826db3f006e6c9ab051ad14bbd6ab31
3
+ metadata.gz: 63f57b1365c84d913c39985c0673762ba445638f
4
+ data.tar.gz: 317c78bc15c24a6deb002976a0987b1bcd3b9d03
5
5
  SHA512:
6
- metadata.gz: 2f784dd7884782c5936fdefa1aa83fdbbc750fa9c2883fb097cf340c9cd91203eb30bfecd3172e9874c0b1480b03b99937aed7f7a3363d1be0f3e2d938f5bc87
7
- data.tar.gz: feadca25da1227f4228a4ab624188987cbb8e93afdeef5b4792c732d83892b922c0efda700fd7d1a38d0150f7383e5cc27536e55374e871e503b5f8666745ed4
6
+ metadata.gz: a489963175a7d98be65d4eac1f9855bccf3040150be608d9cae3d3515c0f947e18ba998dd82d351949ff910c835f1d046d07f1de52c6dd75167ded514f1685c9
7
+ data.tar.gz: 5e6ad4fe9c88ca80bd60f499f55139b0299fc9a69e2b37a03945f8b3799af2bb07a4d0e9e3b7ee829799b9b1c13d7c1533a987673816f959c40b10cec27f904c
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ * 0.2.0 - 2015-05-06
2
+
3
+ * Enhancements
4
+
5
+ * Add pasqual bin
6
+
1
7
  * 0.1.3 - 2015-03-16
2
8
 
3
9
  * Added more detailed failure message on createdb
data/bin/pasqual ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'pasqual/cli'
6
+ Pasqual::CLI.start ARGV
@@ -0,0 +1,118 @@
1
+ require 'thor'
2
+
3
+ require 'pasqual'
4
+
5
+ module Pasqual
6
+ class CLI < Thor
7
+ def self.exit_on_failure?
8
+ true
9
+ end
10
+
11
+ desc "create DATABASE_URL", "create database"
12
+ def create(url = nil)
13
+ puts "Creating #{pasqual(url).name}"
14
+ pasqual(url).createdb
15
+ rescue Pasqual::Createdb::AlreadyExists
16
+ puts "#{pasqual(url).name} already exists, skipped creation"
17
+ end
18
+
19
+ desc "drop DATABASE_URL", "drop database"
20
+ def drop(url = nil)
21
+ puts "Dropping #{pasqual(url).name}"
22
+ pasqual(url).dropdb
23
+ rescue Pasqual::Dropdb::Failed
24
+ puts "Could not drop #{pasqual(url).name}, skipped"
25
+ end
26
+
27
+ desc "poll DATABASE_URL", "poll for the existence of a database server"
28
+ def poll(url = nil)
29
+ say "Waiting for Postgres to come available"
30
+ if attempt_connect(url)
31
+ say "Connected!"
32
+ exit 0
33
+ else
34
+ say "Failed to connect"
35
+ exit 1
36
+ end
37
+ end
38
+
39
+ desc "reset [SQL_FILE] [DATABASE_URL]", "drop, create, structure"
40
+ def reset(path = nil, url = nil)
41
+ drop(url)
42
+ create(url)
43
+ file(path, url) if path
44
+ end
45
+
46
+ desc "psql [SQL] [DATABASE_URL]", "start a psql console or run an SQL statement"
47
+ def psql(*cmd_args)
48
+ url = nil
49
+ sql_args = []
50
+ cmd_args.each do |arg|
51
+ if arg =~ /postgres:/
52
+ url = arg
53
+ else
54
+ sql_args << arg
55
+ end
56
+ end
57
+ db = pasqual(url)
58
+ args = Pasqual::Arglist.
59
+ args(db.username, db.password, db.host, db.port, db.name)
60
+ if sql_args.any?
61
+ args << '-c'
62
+ args += sql_args.map(&:inspect)
63
+ puts "Executing in #{db.name}: #{sql_args.inspect}"
64
+ else
65
+ cmd = "psql #{args.join(' ')}"
66
+ end
67
+ puts "Running #{cmd}"
68
+ Kernel.exec({ 'PGPASSWORD' => db.password }, "psql #{args.join(' ')}")
69
+ end
70
+
71
+
72
+ desc "file PATH [DATABASE_URL]", "execute an SQL script in a file"
73
+ def file(path, url = nil)
74
+ puts "Structuring #{pasqual(url).name}"
75
+ pasqual(url).pipe_sql path
76
+ rescue Errno::EPIPE
77
+ puts "#{pasqual(url).name} doesn't exist, skipped structure"
78
+ end
79
+
80
+ private
81
+
82
+ def pasqual(url)
83
+ @pasqual ||= Pasqual.for(url || ENV['DATABASE_URL'])
84
+ end
85
+
86
+ def pg_connection(url, no_db: false)
87
+ db = pasqual(url)
88
+ conn = PG::Connection.connect_start(
89
+ host: db.host,
90
+ port: db.port,
91
+ dbname: no_db ? nil : db.name,
92
+ user: db.username,
93
+ password: db.password,
94
+ connect_timeout: (60 * 5))
95
+ end
96
+
97
+ def attempt_connect(url)
98
+ require 'pg'
99
+
100
+ timeout = Time.now.to_i + (60 * 5)
101
+
102
+ # Don't connect to a specific database because we may be polling the
103
+ # the server to know whether we can _create_ a database yet.
104
+ conn = pg_connection(url, no_db: true)
105
+ status = conn.connect_poll
106
+ while(Time.now.to_i < timeout && status != PG::PGRES_POLLING_OK) do
107
+ sleep 2
108
+ status = conn.connect_poll
109
+
110
+ conn = pg_connection(url, no_db: true) if status == PG::PGRES_POLLING_FAILED
111
+ puts "Status: #{status.inspect}, Timeout: #{Time.now.to_i - timeout}" if ENV['DEBUG']
112
+ end
113
+ conn.finish
114
+ status == PG::PGRES_POLLING_OK
115
+ end
116
+
117
+ end
118
+ end
@@ -1,3 +1,3 @@
1
1
  module Pasqual
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/pasqual.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "childprocess", "~> 0.5"
22
+ spec.add_runtime_dependency "thor", "~> 0.5"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
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.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Kastner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: childprocess
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -70,7 +84,8 @@ description: Shortcuts for postgres commands, with option to use ENV-configured
70
84
  URLs
71
85
  email:
72
86
  - dkastner@gmail.com
73
- executables: []
87
+ executables:
88
+ - pasqual
74
89
  extensions: []
75
90
  extra_rdoc_files: []
76
91
  files:
@@ -81,8 +96,10 @@ files:
81
96
  - LICENSE.txt
82
97
  - README.md
83
98
  - Rakefile
99
+ - bin/pasqual
84
100
  - lib/pasqual.rb
85
101
  - lib/pasqual/arglist.rb
102
+ - lib/pasqual/cli.rb
86
103
  - lib/pasqual/command.rb
87
104
  - lib/pasqual/createdb.rb
88
105
  - lib/pasqual/database.rb