nomadize 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee738d9952a99b01a995138cb42d3f02a33a552e
4
+ data.tar.gz: a2d88cf37009b8826e651f4e7823e94305c45661
5
+ SHA512:
6
+ metadata.gz: 10b5374b4a415af7adb87ad3268ef6fa103a86c17b989311e957085df2b843d722ee92314b3b89cbf515118f9c2d4eb879fe0085acf234812eb1fe5c0f639298
7
+ data.tar.gz: 1c9f547803f18082f8ee1d8a94f153b4c9f07886551524bc50d3c467951a9a4250cd010beb256853f783e3d119a7ce98477bc9984864da67119a1c03b74c0e54
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nomadize.gemspec
4
+ gemspec
5
+
6
+ gem 'pg'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Nomadize
2
+
3
+ Some simple tools for managing migrations with postgres (without requiring an entire ORM).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'nomadize'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install nomadize
20
+
21
+
22
+ ## Usage
23
+
24
+ Nomadize expects a database configuration file in `config/database.yml`. This file should look something like:
25
+
26
+ ```
27
+ migrations_path: db/migrations
28
+ development:
29
+ :dbname: lol_dev
30
+ test:
31
+ :dbname: lol_test
32
+ production:
33
+ :dbname: lol_production
34
+ ```
35
+
36
+ `migrations_path` defines where Nomadize should find and create your migration files.
37
+
38
+ The test/development/production keys set environment dependent options (through `RACK_ENV`) for the postgres connection object. These key/value pairs are handed directly to the `PG.connect` method, documentation for what options can be passed can be found [here](http://deveiate.org/code/pg/PG/Connection.html#method-c-new).
39
+
40
+ After a config file is in place add `require 'nomadize/tasks'` to your rake file, and enjoy helpful new rake tasks such as:
41
+
42
+ * `rake db:create` - creates a database and a schema_migrations table
43
+ * `rake db:drop` - dumps your poor poor database
44
+ * `rake db:new_migration[migration_name]` - creates a timestamped migration file in db/migrations/ just fill in the details.
45
+ * `rake db:migrate` - runs migrations found in db/migrations that have not been run yet
46
+ * `rake db:status` - see which migrations have or have not been run
47
+ * `rake db:rollback[count]` - rollback migrations (default count: 1)
48
+
49
+
50
+ ## Development
51
+
52
+ todo:
53
+
54
+ - [x] an actual config setup / object
55
+ - [x] sql cleaning (getting rid of the interpolation)
56
+ - [x] to display migration status
57
+ - [x] migration rollbacks
58
+ - [ ] transactions / error handling
59
+ - [ ] maybe some kind of logging idk
60
+ - [x] possibly wrap pg
61
+
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piisalie/nomadize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
66
+
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,33 @@
1
+ require 'yaml'
2
+ require 'pg'
3
+ require 'nomadize/pg_wrapper'
4
+
5
+ module Nomadize
6
+ class Config
7
+
8
+ def self.env
9
+ ENV.fetch('RACK_ENV', 'development')
10
+ end
11
+
12
+ def self.db_connection_info
13
+ config_file.fetch(env)
14
+ end
15
+
16
+ def self.database_name
17
+ db_connection_info.fetch(:dbname)
18
+ end
19
+
20
+ def self.migrations_path
21
+ config_file.fetch("migrations_path")
22
+ end
23
+
24
+ def self.config_file(path = 'config/database.yml')
25
+ @file ||= File.open(path) { |f| YAML.load(f) }
26
+ end
27
+
28
+ def self.db
29
+ PGWrapper.new(PG.connect(db_connection_info))
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ require 'fileutils'
2
+ require 'yaml'
3
+
4
+ module Nomadize
5
+ class FileGenerator
6
+ attr_reader :path, :name, :timestamp
7
+ private :path, :name, :timestamp
8
+
9
+ def initialize(path:, name:, timestamp: DateTime.now.new_offset(0).strftime("%Y%m%d%H%M%S"))
10
+ @path = path
11
+ @name = name
12
+ @timestamp = timestamp
13
+ end
14
+
15
+ def save
16
+ FileUtils.mkdir_p path unless File.exists?(path)
17
+ File.open(File.join(path, timestamped_name), "w") { |f| f.write(template_content) }
18
+ end
19
+
20
+ private
21
+
22
+ def timestamped_name
23
+ "#{timestamp}_#{name}.yml"
24
+ end
25
+
26
+ def template_content
27
+ YAML.dump( {up: "", down: "" })
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module Nomadize
2
+ class Migration
3
+ attr_reader :up, :down, :filename
4
+ private :up, :down
5
+
6
+ def initialize(up:, down:, filename:)
7
+ @up = up
8
+ @down = down
9
+ @filename = filename
10
+ end
11
+
12
+ def run(db)
13
+ db.exec(up)
14
+ db.insert_migration_filename(filename)
15
+ end
16
+
17
+ def rollback(db)
18
+ db.exec(down)
19
+ db.remove_migration_filename(filename)
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ require 'yaml'
2
+
3
+ module Nomadize
4
+ class MigrationLoader
5
+ attr_reader :path
6
+ private :path
7
+
8
+ def initialize(path:)
9
+ @path = path
10
+ end
11
+
12
+ def migrations
13
+ filenames.map do |filename|
14
+ File.open(filename, "r") { |f|
15
+ YAML.load(f).merge(filename: File.basename(filename, ".yml"))
16
+ }
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def filenames
23
+ Dir[File.join(path, '*.yml')]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ module Nomadize
2
+ class Migrator
3
+ attr_reader :migrations, :db
4
+ private :migrations, :db
5
+
6
+ def initialize(db:, migrations:)
7
+ @migrations = migrations
8
+ @db = db
9
+ end
10
+
11
+ def run
12
+ pending.map { |migration| migration.run(db) }
13
+ end
14
+
15
+ def rollback(count)
16
+ done[-count..-1].reverse.map { |migration| migration.rollback(db) }
17
+ end
18
+
19
+ def pending
20
+ sorted_by_timestamp.select do |migration|
21
+ !recorded_migrations.include?(migration.filename)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def done
28
+ sorted_by_timestamp.select do |migration|
29
+ recorded_migrations.include?(migration.filename)
30
+ end
31
+ end
32
+
33
+ def sorted_by_timestamp
34
+ migrations.sort_by { |migration| migration.filename }
35
+ end
36
+
37
+ def recorded_migrations
38
+ db.retrieve_all_migration_filenames
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ require 'forwardable'
2
+
3
+ module Nomadize
4
+ class PGWrapper
5
+ extend Forwardable
6
+ attr_reader :pg
7
+
8
+ def initialize(pg)
9
+ @pg = pg
10
+ end
11
+
12
+ def_delegators :pg, :exec
13
+
14
+ def create_schema_migrations_table
15
+ exec("CREATE TABLE schema_migrations (filename TEXT NOT NULL);")
16
+ end
17
+
18
+ def retrieve_all_migration_filenames
19
+ exec("SELECT filename FROM schema_migrations;").to_a.flat_map(&:values)
20
+ end
21
+
22
+ def insert_migration_filename(filename)
23
+ exec("INSERT INTO schema_migrations (filename) VALUES ($1);", [ filename ])
24
+ end
25
+
26
+ def remove_migration_filename(filename)
27
+ exec("DELETE FROM schema_migrations WHERE filename = $1;", [ filename ])
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,63 @@
1
+ module Nomadize
2
+ class StatusDisplay
3
+
4
+ attr_reader :files, :records
5
+ private :files, :records
6
+
7
+ def initialize(files:, records:)
8
+ @files = files
9
+ @records = records
10
+ end
11
+
12
+ def titlebar
13
+ "filename".ljust(lsize) + " | " + "status".rjust(rsize)
14
+ end
15
+
16
+ def divider
17
+ "-" * titlebar.size
18
+ end
19
+
20
+ def migrations
21
+ sorted_migrations.map do |migration|
22
+ migration[:filename].ljust(lsize) + " | " + migration[:status].to_s.rjust(rsize)
23
+ end.to_enum
24
+ end
25
+
26
+ private
27
+
28
+ def files_with_status
29
+ files.map { |file|
30
+ file.merge(status: calculate_status(file))
31
+ }
32
+ end
33
+
34
+ def calculate_status(file)
35
+ if records.include?(file[:filename])
36
+ 'up'
37
+ else
38
+ 'down'
39
+ end
40
+ end
41
+
42
+ def sorted_migrations
43
+ migrations = files_with_status + migrations_without_files
44
+ migrations.sort_by { |file| file[:filename] }
45
+ end
46
+
47
+ def migrations_without_files
48
+ filenames = files.map { |file| file[:filename] }
49
+ records.select { |record| !filenames.include?(record) }.map { |record|
50
+ { filename: record, status: 'missing' }
51
+ }
52
+ end
53
+
54
+ def rsize
55
+ 7
56
+ end
57
+
58
+ def lsize
59
+ @lsize ||= sorted_migrations.map { |file| file[:filename] }.map(&:size).max
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ require 'nomadize'
2
+
3
+ namespace :db do
4
+
5
+ desc 'Create database using name in {appdir}/config/database.yml'
6
+ task :create do
7
+ Nomadize.create_database
8
+ end
9
+
10
+ desc 'drop database using name in {appdir}/config/database.yml'
11
+ task :drop do
12
+ Nomadize.drop_database
13
+ end
14
+
15
+ desc 'Generate a migration template file in {appdir}/db/migrations/'
16
+ task :new_migration, [:migration_name] do |_, args|
17
+ Nomadize.generate_template_migration_file(args.migration_name)
18
+ end
19
+
20
+ desc 'Run migrations'
21
+ task :migrate do
22
+ Nomadize.run_migrations
23
+ end
24
+
25
+ desc 'view the status of known migrations'
26
+ task :status do
27
+ Nomadize.status
28
+ end
29
+
30
+ desc 'rollback migrations (default count: 1)'
31
+ task :rollback, :steps do |_, args|
32
+ count = args.steps || 1
33
+ Nomadize.rollback(count)
34
+ end
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ module Nomadize
2
+ VERSION = "0.1.0"
3
+ end
data/lib/nomadize.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'nomadize/version'
2
+ require 'nomadize/migration_loader'
3
+ require 'nomadize/migration'
4
+ require 'nomadize/migrator'
5
+ require 'nomadize/config'
6
+ require 'nomadize/status_display'
7
+ require 'nomadize/file_generator'
8
+
9
+ module Nomadize
10
+
11
+ def self.run_migrations
12
+ db = Config.db
13
+ migration_files = MigrationLoader.new(path: Config.migrations_path).migrations
14
+ migrations = migration_files.map { |migration| Migration.new(migration) }
15
+ migrator = Migrator.new(db: db, migrations: migrations)
16
+
17
+ migrator.run
18
+ db
19
+ end
20
+
21
+ def self.create_database
22
+ system("createdb --echo #{Config.database_name}")
23
+ db = Config.db
24
+ db.create_schema_migrations_table
25
+ db
26
+ end
27
+
28
+ def self.status
29
+ files = MigrationLoader.new(path: Config.migrations_path).migrations
30
+ db = Config.db
31
+ records = db.retrieve_all_migration_filenames
32
+
33
+ display = StatusDisplay.new(files: files, records: records)
34
+
35
+ puts display.titlebar
36
+ puts display.divider
37
+ display.migrations.each do |row|
38
+ puts row
39
+ end
40
+ end
41
+
42
+ def self.rollback(count = 1)
43
+ db = Config.db
44
+ migration_files = MigrationLoader.new(path: Config.migrations_path).migrations
45
+ migrations = migration_files.map { |migration| Migration.new(migration) }
46
+ migrator = Migrator.new(db: db, migrations: migrations)
47
+
48
+ migrator.rollback(count)
49
+ db
50
+ end
51
+
52
+ def self.drop_database
53
+ system("dropdb --echo #{Config.database_name}")
54
+ end
55
+
56
+ def self.generate_template_migration_file(name)
57
+ Nomadize::FileGenerator.new(path: Config.migrations_path, name: name).save
58
+ end
59
+
60
+ end
data/nomadize.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nomadize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nomadize"
8
+ spec.version = Nomadize::VERSION
9
+ spec.authors = ["Paul Dawson"]
10
+ spec.email = ["paul@into.computer"]
11
+
12
+ spec.summary = %q{A simple database migration utility}
13
+ spec.homepage = "https://github.com/piisalie/nomadize"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest"
24
+
25
+ spec.add_dependency "pg", "~> 0.18.1"
26
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nomadize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Dawson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-10 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.18.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.18.1
69
+ description:
70
+ email:
71
+ - paul@into.computer
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/nomadize.rb
84
+ - lib/nomadize/config.rb
85
+ - lib/nomadize/file_generator.rb
86
+ - lib/nomadize/migration.rb
87
+ - lib/nomadize/migration_loader.rb
88
+ - lib/nomadize/migrator.rb
89
+ - lib/nomadize/pg_wrapper.rb
90
+ - lib/nomadize/status_display.rb
91
+ - lib/nomadize/tasks.rb
92
+ - lib/nomadize/version.rb
93
+ - nomadize.gemspec
94
+ homepage: https://github.com/piisalie/nomadize
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A simple database migration utility
118
+ test_files: []