nomadize 0.3.0 → 0.4.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: 24e4a53c7ce85b9aee4219137933e62f8d4ae79c
4
- data.tar.gz: a859e3e0db9e603db1998d7f5e8e11907e5c9274
3
+ metadata.gz: 1356c5d4667a3fbd4c11ab51081300c6a9f33c9a
4
+ data.tar.gz: 5aaf1896c0727b7707c0ce1e451534dce8e08446
5
5
  SHA512:
6
- metadata.gz: 9959f0b9257e56ac4d77cb8c7918fe5c9d9927e4a6b63d9fde13a7253e36f149a739792926ab1bcbed96b51159bfe06997206965dd125ae43ecad37d5b530247
7
- data.tar.gz: 53fc2a4752307418c5a5a48a154cd626e44d8c60866c7b184fb6584a21464a9bb6f1cea36539d543f0d41ad0cbcc9c6eedcdec7684e2e5462c291530fe552dee
6
+ metadata.gz: 2796afcc8b7c193276fed363e6224c2f41afe483feb6ba144611bde7d074fbd29b8d962c25e5c19dbc1ac084a8a804feea504e8279c2ccc4c26948717ca0f1ad
7
+ data.tar.gz: 39241f364e82510901a39a32f45527048090dda7188134e1c98dabe2d3f7c87e9e9e7a1a7bef46e16efe4f6c575cd9bf97345a7f491aa47fd7e77f824a040157
data/README.md CHANGED
@@ -22,7 +22,11 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- Nomadize expects a database configuration file in `config/database.yml`. You can generate this file with the rake task `db:generate_template_config` or write it yourself. This file should look something like:
25
+ Nomadize supports two different methods for configuring the connection to Postgres. Nomadize will also provide access to the underlying PG connection wrapper (using your defined config) by using the `Nomadize::Config.db` method. This wrapper responds to `exec` in the same way that the underlying PG connection object does.
26
+
27
+ ### Config File
28
+
29
+ You may use a config file `config/database.yml`. This file can be generated with either the rake task: `db:generate_template_config` or the CLI: `$ nomadize generate_template_config`. You can also choose to create the file for yourself. `config/database.yml` should look something like:
26
30
 
27
31
  ```
28
32
  development:
@@ -33,10 +37,28 @@ production:
33
37
  :dbname: lol_production
34
38
  ```
35
39
 
36
- 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).
40
+ The test/development/production keys define environment dependent options for the `PG.connection` based on the environment set via `RACK_ENV`. 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).
41
+
42
+ The directory in which to find migrations files can optionally be set with the config:
43
+ `migrations_path: db/migrations`
44
+ The default value is `db/migrations`
37
45
 
38
- `migrations_path: db/migrations` defines where Nomadize should find and create your migration files, this setting is optional and is set to `db/migrations` by default.
46
+ ### ENV['DATABASE_URL']
39
47
 
48
+ As of 0.4.0 Nomadize will also respect the `DATABASE_URL` environment variable. If `DATABASE_URL` is set it will override the connection information in the config file `config/database.yml`.
49
+ eg `postgres://user1:supersecure@somehost:1337/database-name` will result in the following configuration hash being passed to the underlying `PG.connection` object.
50
+
51
+ ```ruby
52
+ {
53
+ dbname: 'database-name',
54
+ port: 1337,
55
+ user: 'user1',
56
+ password: 'supersecure',
57
+ host: 'somehost'
58
+ }
59
+ ```
60
+
61
+ ### Migrations
40
62
  After a config file is in place add `require 'nomadize/tasks'` to your rake file, and enjoy helpful new rake tasks such as:
41
63
 
42
64
  * `rake db:create` - creates a database and a schema_migrations table
@@ -55,6 +77,7 @@ Alternatively you can use the commandline tool `nomadize`:
55
77
  * `nomadize migrate` - runs migrations found in db/migrations that have not been run yet
56
78
  * `nomadize status` - see which migrations have or have not been run
57
79
  * `nomadize rollback $count` - rollback migrations (default count: 1)
80
+ * `nomadize generate_template_config` - generate a config file in `config/database.yml`
58
81
 
59
82
  Migrations are written in SQL in the generated YAML files:
60
83
 
@@ -64,8 +87,6 @@ Migrations are written in SQL in the generated YAML files:
64
87
  :down: 'DROP TABLE testing;'
65
88
  ```
66
89
 
67
- You also have access to the underlying PG connection wrapper (using your defined config) by using the `Nomadize::Config.db` method. This wrapper responds to `exec` in the same way that the underlying PG connection object does.
68
-
69
90
  ## Development
70
91
 
71
92
  todo:
@@ -75,7 +96,7 @@ todo:
75
96
  - [x] to display migration status
76
97
  - [x] migration rollbacks
77
98
  - [ ] transactions / error handling
78
- - [ ] maybe some kind of logging idk
99
+ - [x] maybe some kind of logging idk
79
100
  - [x] possibly wrap pg
80
101
  - [x] template config file generator
81
102
  - [x] maybe set a default migrations path (so the key isn't required in the config file)
@@ -91,6 +112,13 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/piisal
91
112
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
92
113
 
93
114
  ## Changelog
115
+ 0.4.0
116
+ * support DATABASE_URL env variable
117
+ * Add template_config generator to command line tool
118
+ * Update the README
119
+ * Added some basic logging
120
+ * Fix an issue with rollback count not actually working :'(
121
+
94
122
  0.3.0
95
123
  * Include a command line interface for Nomadize commands (THANKS [@moonglum](https://github.com/moonglum))
96
124
 
data/exe/nomadize CHANGED
@@ -6,7 +6,7 @@ require 'nomadize/help'
6
6
  def show_help
7
7
  puts "Usage:"
8
8
  Nomadize.help.each_pair do |command, help|
9
- puts " nomadize #{command.to_s.ljust(15)} #{help}"
9
+ puts " nomadize #{command.to_s.ljust(25)} #{help}"
10
10
  end
11
11
  end
12
12
 
@@ -25,6 +25,7 @@ when 'new_migration' then Nomadize.generate_template_migration_file(migration_na
25
25
  when 'migrate' then Nomadize.migrate
26
26
  when 'status' then Nomadize.status
27
27
  when 'rollback' then Nomadize.rollback(ARGV.fetch(1, 1))
28
+ when 'generate_template_config' then Nomadize.generate_template_config
28
29
  when nil then abort 'Error: No Command Provided'
29
30
  else abort "Error: Unknown Command '#{ARGV[0]}'"
30
31
  end
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'pg'
3
+ require 'uri'
3
4
  require 'nomadize/pg_wrapper'
4
5
 
5
6
  module Nomadize
@@ -10,7 +11,7 @@ module Nomadize
10
11
  end
11
12
 
12
13
  def self.db_connection_info
13
- config_file.fetch(env)
14
+ database_url_config || config_file.fetch(env)
14
15
  end
15
16
 
16
17
  def self.database_name
@@ -29,5 +30,22 @@ module Nomadize
29
30
  PGWrapper.new(PG.connect(db_connection_info))
30
31
  end
31
32
 
33
+ private
34
+
35
+ def self.database_url_config
36
+ ENV['DATABASE_URL'] ? parse(ENV['DATABASE_URL']) : nil
37
+ end
38
+
39
+ def self.parse(database_url)
40
+ uri = URI.parse(database_url)
41
+ config = { }
42
+ config[:port] = uri.port if uri.port
43
+ config[:user] = uri.user if uri.user
44
+ config[:host] = uri.host if uri.host
45
+ config[:password] = uri.password if uri.password
46
+ config[:dbname] = uri.path.sub("/", "") if uri.path
47
+ config
48
+ end
49
+
32
50
  end
33
51
  end
@@ -14,7 +14,9 @@ module Nomadize
14
14
 
15
15
  def save
16
16
  FileUtils.mkdir_p path unless File.exists?(path)
17
+ migration_file = File.join(path, timestamped_name)
17
18
  File.open(File.join(path, timestamped_name), "w") { |f| f.write(template_content) }
19
+ migration_file
18
20
  end
19
21
 
20
22
  private
data/lib/nomadize/help.rb CHANGED
@@ -5,7 +5,8 @@ module Nomadize
5
5
  new_migration: 'Generate a migration template file. default directory: {appdir}/db/migrations',
6
6
  migrate: 'Run migrations',
7
7
  status: 'View the status of known migrations',
8
- rollback: 'Rollback migrations (default count: 1)'
8
+ rollback: 'Rollback migrations (default count: 1)',
9
+ generate_template_config: 'generate template config/database.yml config file'
9
10
  }
10
11
  class << self; attr_reader :help; end
11
12
  end
@@ -12,11 +12,13 @@ module Nomadize
12
12
  def run(db)
13
13
  db.exec(up)
14
14
  db.insert_migration_filename(filename)
15
+ filename
15
16
  end
16
17
 
17
18
  def rollback(db)
18
19
  db.exec(down)
19
20
  db.remove_migration_filename(filename)
21
+ filename
20
22
  end
21
23
 
22
24
  end
@@ -13,7 +13,7 @@ module Nomadize
13
13
  end
14
14
 
15
15
  def rollback(count)
16
- done[-count..-1].reverse.map { |migration| migration.rollback(db) }
16
+ done[-count.to_i..-1].reverse.map { |migration| migration.rollback(db) }
17
17
  end
18
18
 
19
19
  def pending
@@ -34,7 +34,7 @@ namespace :db do
34
34
  Nomadize.rollback(count)
35
35
  end
36
36
 
37
- desc 'generate template config file'
37
+ desc Nomadize.help.fetch(:generate_template_config)
38
38
  task :generate_template_config do
39
39
  Nomadize.generate_template_config
40
40
  puts 'Config created in config/database.yml'
@@ -1,3 +1,3 @@
1
1
  module Nomadize
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/nomadize.rb CHANGED
@@ -14,7 +14,8 @@ module Nomadize
14
14
  migrations = migration_files.map { |migration| Migration.new(migration) }
15
15
  migrator = Migrator.new(db: db, migrations: migrations)
16
16
 
17
- migrator.run
17
+ files_migrated = migrator.run
18
+ puts files_migrated
18
19
  db
19
20
  end
20
21
 
@@ -45,7 +46,8 @@ module Nomadize
45
46
  migrations = migration_files.map { |migration| Migration.new(migration) }
46
47
  migrator = Migrator.new(db: db, migrations: migrations)
47
48
 
48
- migrator.rollback(count)
49
+ files_rolled_back = migrator.rollback(count)
50
+ puts files_rolled_back
49
51
  db
50
52
  end
51
53
 
@@ -54,7 +56,8 @@ module Nomadize
54
56
  end
55
57
 
56
58
  def self.generate_template_migration_file(name)
57
- Nomadize::FileGenerator.new(path: Config.migrations_path, name: name).save
59
+ migration_file = Nomadize::FileGenerator.new(path: Config.migrations_path, name: name).save
60
+ puts "Migration file created: #{Dir.pwd}/#{migration_file}"
58
61
  end
59
62
 
60
63
  def self.generate_template_config
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nomadize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dawson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2015-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler