psqlversions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Postgres::Local::Versions
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/postgres/local/versions`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'psqlversions'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install postgres-local-versions
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/postgres-local-versions. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Postgres::Local::Versions project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/postgres-local-versions/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "psqlversions/local/versions"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/psqlversions ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require "psqlversions/cli"
3
+
4
+ Psqlversions::CLI.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,90 @@
1
+ require 'open3'
2
+ require 'thor'
3
+ require 'daybreak'
4
+ require 'terminal-table'
5
+ require 'psqlversions'
6
+
7
+ module Psqlversions
8
+ # The main class for the command-line interface
9
+ class CLI < Thor
10
+ desc 'list', 'list all local databases'
11
+ def list
12
+ with_protect_db do |protections_db|
13
+ rows = list_dbs.each.map do |local_db_name|
14
+ protected = protections_db[local_db_name] == 'protected'
15
+ [local_db_name, protected ? 'protected' : '']
16
+ end
17
+
18
+ puts Terminal::Table.new(headings: %w[Database Protected?], rows: rows)
19
+ end
20
+ end
21
+
22
+ desc 'drop {local_db}', 'drop a local database (following protection flags)'
23
+ def drop(local_db_name)
24
+ with_protect_db do |protections_db|
25
+ if protections_db[local_db_name] == 'protected'
26
+ puts "#{local_db_name} is protected and I won't drop it."
27
+ else
28
+ _stdout, stderr, status = Open3.capture3('dropdb', local_db_name)
29
+ puts "error when dropping: #{stderr}" if status != 0
30
+ end
31
+ end
32
+ end
33
+
34
+ desc 'copy {from} {to}', 'create a copy of a local database'
35
+ def copy(from, to)
36
+ _stdout, stderr, status = Open3.capture3('createdb', "-T#{from}", to)
37
+ puts "error when copying: #{stderr}" if status != 0
38
+ end
39
+
40
+ desc 'checkpoint {local_db} {tag}', 'create a checkpoint for a local db'
41
+ def checkpoint(db, tag)
42
+ next_checkpoint = build_checkpoint_name(tag, last_checkpoint(tag) + 1)
43
+ copy(db, next_checkpoint)
44
+ end
45
+
46
+ desc 'protect {local_db}', 'prevent psqlversions from dropping a local db'
47
+ def protect(local_db_name)
48
+ with_protect_db do |protections_db|
49
+ protections_db.set! local_db_name, 'protected'
50
+ end
51
+ end
52
+
53
+ desc 'unprotect {local_db}', 'allow psqlversions to drop a local db'
54
+ def unprotect(local_db_name)
55
+ with_protect_db do |protections_db|
56
+ protections_db.set! local_db_name, 'unprotected'
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def with_protect_db
63
+ db = Daybreak::DB.new "#{Dir.home}/.psqlversions.db"
64
+ yield(db)
65
+ db.close
66
+ end
67
+
68
+ def last_checkpoint(checkpoint_base)
69
+ points = list_dbs.select { |db| db.start_with? checkpoint_base }
70
+ .map { |db| db.sub checkpoint_base, '' }
71
+ .select { |suffix| suffix =~ /-\d+/ }
72
+ .map { |suffix| suffix[1..-1].to_i }
73
+
74
+ (points + [0]).max
75
+ end
76
+
77
+ def build_checkpoint_name(checkpoint_base_name, next_checkpoint)
78
+ "#{checkpoint_base_name}-#{next_checkpoint.to_s.rjust 3, '0'}"
79
+ end
80
+
81
+ def list_dbs
82
+ stdout, status = Open3.capture2('psql', '--list')
83
+ return [] if status != 0
84
+
85
+ stdout.each_line.drop(3)
86
+ .map { |line| line.split('|')[0].strip }
87
+ .reject { |db| db.empty? || db =~ /\(.*\)/ }
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module Psqlversions
2
+ VERSION = "0.1.0"
3
+ end
File without changes
data/psqlversions.gem ADDED
Binary file
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'psqlversions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'psqlversions'
8
+ spec.version = Psqlversions::VERSION
9
+ spec.authors = ['Cameron Seebach']
10
+ spec.email = ['cameron.seebach@lendinghome.com']
11
+
12
+ spec.summary = 'Manage versions of your local Postgres databases more effectively.'
13
+ spec.description = 'Requires the Postgres command line tools.'
14
+ spec.homepage = 'https://github.com/cseebach-lh/psqlversions'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.16"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+
35
+ spec.add_runtime_dependency "thor"
36
+ spec.add_runtime_dependency "daybreak"
37
+ spec.add_runtime_dependency "terminal-table"
38
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psqlversions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Seebach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: daybreak
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: terminal-table
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Requires the Postgres command line tools.
98
+ email:
99
+ - cameron.seebach@lendinghome.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".idea/misc.xml"
106
+ - ".idea/modules.xml"
107
+ - ".idea/postgres-local-versions.iml"
108
+ - ".idea/vcs.xml"
109
+ - ".idea/workspace.xml"
110
+ - ".rspec"
111
+ - ".travis.yml"
112
+ - CODE_OF_CONDUCT.md
113
+ - Gemfile
114
+ - Gemfile.lock
115
+ - LICENSE
116
+ - README.md
117
+ - Rakefile
118
+ - bin/console
119
+ - bin/psqlversions
120
+ - bin/setup
121
+ - lib/psqlversions.rb
122
+ - lib/psqlversions/cli.rb
123
+ - lib/psqlversions/version.rb
124
+ - psqlversions.gem
125
+ - psqlversions.gemspec
126
+ homepage: https://github.com/cseebach-lh/psqlversions
127
+ licenses: []
128
+ metadata:
129
+ allowed_push_host: https://rubygems.org
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.6.14
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Manage versions of your local Postgres databases more effectively.
150
+ test_files: []