sequel_tools 0.1.3 → 0.1.8

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
- SHA1:
3
- metadata.gz: 437ee89e0a2b2873e1651f948c8a89fc1a0aedf6
4
- data.tar.gz: 4ba17eb0f479f61e39fb02be3caf45598a269115
2
+ SHA256:
3
+ metadata.gz: 17bb9b0da66eb5b99c648c41d902e56e2cb93348d7a1b50d6ab447bfab0cabe8
4
+ data.tar.gz: 2c066a0179cd2a67127cca5812cc37abb40b02b63ceef28186376540ec2798fc
5
5
  SHA512:
6
- metadata.gz: 3d59d32bc56d2d39f6bf378c7be5713b253162b7f48dbca61f922d23660a5137332a2070b2fec00dccebb6fd25a2a880e6fea8575c347f758f57ce3c99c5d6bd
7
- data.tar.gz: 15cce1e67d59678423f000e8ab192e4205726eb2f9939623b8f68eccaf7c80235f19e964a0f367aabc95d4af71b1e0e9e118b6cecbbbcef3666839d57784413b
6
+ metadata.gz: b3f10ee727d2b6ae3b8a3b7c0ead5947817fa6f6b9238d3c7708553534936c303bd09665a45519ff4b6cf0041f64c348b5eb117267d22b24b814cb21c8f33057
7
+ data.tar.gz: 65493ac56d38975f0857a0db28007fd06036c668a035e7de6e1694902025ea2df77d4b6b3e0f82a18115cf77684cd6451035a3d7c71174296b98ef4987be928a
data/.gitignore CHANGED
@@ -8,6 +8,9 @@
8
8
  /tmp/
9
9
  /spec/sample_project/db/migrations/
10
10
  /spec/sample_project/db/seeds.rb
11
+ /spec/sample_project/bin/bundle
12
+ /spec/sample_project/bin/sequel
13
+ /Gemfile.lock
11
14
 
12
15
  # rspec failure tracking
13
16
  .rspec_status
data/.travis.yml CHANGED
@@ -2,5 +2,6 @@ sudo: true
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.4.1
5
+ - jruby-9.1.9.0
5
6
  before_install: scripts/ci/travis-build.sh
6
7
  #before_install: gem install bundler -v 1.16.0
data/README.md CHANGED
@@ -21,6 +21,11 @@ Add this line to your application's Gemfile:
21
21
 
22
22
  ```ruby
23
23
  gem 'sequel_tools'
24
+ gem 'rake'
25
+
26
+ # For PostgreSQL:
27
+ gem 'pg', platform: :mri
28
+ gem 'jdbc-postgres', platform: :jruby
24
29
  ```
25
30
 
26
31
  And then execute:
@@ -51,10 +56,13 @@ base_config = SequelTools.base_config(
51
56
  dump_schema_on_migrate: false, # it's a good idea to enable it for the reference environment
52
57
  pg_dump: 'pg_dump', # command used to run pg_dump
53
58
  pg_dump: 'psql', # command used to run psql when calling rake db:shell if adapter is postgres
54
- db_migrations_location: 'db/migrations',
59
+ migrations_location: 'db/migrations',
55
60
  schema_location: 'db/migrations/schema.sql',
56
61
  seeds_location: 'db/seeds.rb',
57
- maintenancedb: 'postgres' # for tasks such as creating the database
62
+ # for tasks such as creating the database:
63
+ # when nil, defaults to the value of the :dbadapter config.
64
+ # This is the database we should connect to before executing "create database dbname"
65
+ maintenancedb: :default,
58
66
  )
59
67
 
60
68
  namespace 'db' do
@@ -71,8 +79,29 @@ Then you are able to run several tasks (`rake -T` will list all supported):
71
79
  rake db:create
72
80
  rake db:new_migration[migration_name]
73
81
  rake db:migrate
82
+ # setup creates the database, loads the latest schema
83
+ # and import seeds when available
74
84
  rake dbtest:setup
75
- rake dbtest:shell
85
+ # reset drops (if existing) then recreate the database, run all migrations
86
+ # and import seeds when available
87
+ rake dbtest:reset
88
+ # shell opens a psql section to the database for the PostgreSQL adapter
89
+ rake db:shell
90
+ # irb runs "bundle exec sequel" pointing to the database and stores the connection in "DB"
91
+ rake db:irb
92
+ rake db:rollback
93
+ rake db:status
94
+ # version displays latest applied migration
95
+ rake db:version
96
+ rake db:seed
97
+ rake db:redo[migration_filename]
98
+ rake db:down[migration_filename]
99
+ rake db:up[migration_filename]
100
+ # schema_dump is called automatically after migrate/rollback/up/down/redo
101
+ # if passing { dump_schema_on_migrate: true } to the config
102
+ rake db:schema_dump
103
+ # database must be empty before calling db:schema_load
104
+ rake db:schema_load
76
105
 
77
106
  You may define your own command to open a shell to your database upon the 'db:shell' task.
78
107
  PostgreSQL is supported out-of-the-box, but if it wasn't, here's a sample script that would
data/lib/sequel_tools.rb CHANGED
@@ -7,8 +7,8 @@ module SequelTools
7
7
  project_root: nil,
8
8
  pg_dump: 'pg_dump', # command used to run pg_dump
9
9
  psql: 'psql', # command used to run psql
10
- maintenancedb: 'postgres', # DB to connect to for creating/dropping databases
11
- db_migrations_location: 'db/migrations',
10
+ maintenancedb: :default, # DB to connect to for creating/dropping databases
11
+ migrations_location: 'db/migrations',
12
12
  schema_location: 'db/migrations/schema.sql',
13
13
  seeds_location: 'db/seeds.rb',
14
14
  dbname: nil,
@@ -20,6 +20,7 @@ module SequelTools
20
20
  dump_schema_on_migrate: false,
21
21
  log_level: nil,
22
22
  sql_log_level: :debug,
23
+ migrations_table: nil,
23
24
  } # unfrozen on purpose so that one might want to update the defaults
24
25
 
25
26
  REQUIRED_KEYS = [ :project_root, :dbadapter, :dbname, :username ]
@@ -29,7 +30,7 @@ module SequelTools
29
30
  REQUIRED_KEYS.each do |key|
30
31
  raise MissingConfigError, "Expected value for #{key} config is missing" if config[key].nil?
31
32
  end
32
- [:db_migrations_location, :schema_location, :seeds_location].each do |k|
33
+ [:migrations_location, :schema_location, :seeds_location].each do |k|
33
34
  config[k] = File.expand_path config[k], config[:project_root]
34
35
  end
35
36
  config
@@ -42,4 +43,15 @@ module SequelTools
42
43
  actions_manager.load_all
43
44
  actions_manager.export_as_rake_tasks rake_context
44
45
  end
46
+
47
+ def self.suppress_java_output
48
+ return yield unless RUBY_PLATFORM == 'java'
49
+ require 'java'
50
+ require 'stringio'
51
+ old_err = java.lang.System.err
52
+ java.lang.System.err = java.io.PrintStream.new(StringIO.new.to_outputstream)
53
+ yield
54
+ ensure
55
+ java.lang.System.err = old_err if RUBY_PLATFORM == 'java'
56
+ end
45
57
  end
@@ -0,0 +1,17 @@
1
+ # frozen-string-literal: true
2
+
3
+ require_relative '../actions_manager'
4
+
5
+ SequelTools::ActionsManager::Action.register :before_task, nil do |args, context|
6
+ config = context[:config]
7
+ adapter = config[:dbadapter]
8
+ action = context[:current_action]
9
+ hooks = [:before_any, :"before_#{action.name}", :"before_any_#{adapter}",
10
+ :"before_#{action.name}_#{adapter}"]
11
+ hooks.each do |h|
12
+ next unless a = SequelTools::ActionsManager::Action[h]
13
+ a.run args, context
14
+ context[:"#{h}_processed"] = true
15
+ end
16
+ config[:maintenancedb] = adapter if context[:maintenancedb] == :default
17
+ end
@@ -7,7 +7,9 @@ require_relative '../sequel_tools_logger'
7
7
  SequelTools::ActionsManager::Action.register :connect_db, nil do |args, context|
8
8
  next if context[:db]
9
9
  config = context[:config]
10
- context[:db] = db = Sequel.connect context[:uri_builder].call config
10
+ context[:db] = db = SequelTools.suppress_java_output do
11
+ Sequel.connect context[:uri_builder].call(config), test: true
12
+ end
11
13
  db.sql_log_level = config[:sql_log_level]
12
14
  db.log_connection_info = false
13
15
  next unless log_level = config[:log_level]
@@ -0,0 +1,20 @@
1
+ # frozen-string-literal: true
2
+
3
+ require_relative '../actions_manager'
4
+
5
+ class SequelTools::ActionsManager
6
+ description = "Opens an IRB session started as 'sequel uri_to_database' (DB is available to irb)"
7
+ Action.register :irb, description do |args, context|
8
+ # This code does the job, but for some reason the test will timeout under JRuby
9
+ #config = context[:config]
10
+ #uri = context[:uri_builder][config]
11
+ #exec "bundle exec sequel #{uri}"
12
+
13
+ Action[:connect_db].run({}, context)
14
+ require 'irb'
15
+ ::DB = context[:db]
16
+ ARGV.clear
17
+ puts 'Your database is stored in DB...'
18
+ IRB.start
19
+ end
20
+ end
@@ -12,7 +12,8 @@ class SequelTools::ActionsManager
12
12
  Sequel.extension :migration unless Sequel.respond_to? :migration
13
13
  options = {}
14
14
  options[:target] = args[:version].to_i if args[:version]
15
- Sequel::Migrator.run db, config[:db_migrations_location], options
15
+ options[:table] = config[:migrations_table] if config[:migrations_table]
16
+ Sequel::Migrator.run db, config[:migrations_location], options
16
17
  Action[:schema_dump].run({}, context) if config[:dump_schema_on_migrate]
17
18
  end
18
19
  end
@@ -7,7 +7,7 @@ class SequelTools::ActionsManager
7
7
  Action.register :new_migration, desc, arg_names: [ :name ] do |args, context|
8
8
  (puts 'Migration name is missing - aborting'; exit 1) unless name = args[:name]
9
9
  require 'time'
10
- migrations_path = context[:config][:db_migrations_location]
10
+ migrations_path = context[:config][:migrations_location]
11
11
  filename = "#{migrations_path}/#{Time.now.strftime '%Y%m%d%H%M%S'}_#{name}.rb"
12
12
  File.write filename, <<~MIGRATIONS_TEMPLATE_END
13
13
  # documentation available at http://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html
@@ -0,0 +1,14 @@
1
+ # frozen-string-literal: true
2
+
3
+ require_relative '../actions_manager'
4
+ require_relative 'schema_dump_postgres'
5
+ require_relative 'shell_postgres'
6
+
7
+ class SequelTools::ActionsManager
8
+ Action.register :before_any_postgres, nil do |args, context|
9
+ next if context[:before_any_postgres_processed]
10
+ config = context[:config]
11
+ config[:maintenancedb] = 'postgres' if config[:maintenancedb] == :default
12
+ config[:jdbc_adapter] = 'postgresql' if RUBY_PLATFORM == 'java'
13
+ end
14
+ end
@@ -16,7 +16,8 @@ class SequelTools::ActionsManager
16
16
  puts "Schema file '#{schema_location}' does not exist. Aborting."
17
17
  exit 1
18
18
  end
19
- context[:db] << File.read(schema_location)
19
+ context[:db].run File.read(schema_location)
20
+ context[:db].disconnect # forces reconnection
20
21
  end
21
22
  end
22
23
  end
@@ -3,8 +3,6 @@
3
3
  require_relative '../actions_manager'
4
4
 
5
5
  class SequelTools::ActionsManager
6
- # TODO: this action is not currently tested automatically as it's not critical and not
7
- # trivial to write a test for
8
6
  Action.register :shell, 'Open an interactive shell to the database' do |args, context|
9
7
  begin
10
8
  Action[:connect_db].run({}, context)
@@ -1,11 +1,8 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  require_relative '../actions_manager'
4
- require_relative '../pg_helper'
5
4
 
6
5
  class SequelTools::ActionsManager
7
- # TODO: this action is not currently tested automatically as it's not critical and not
8
- # trivial to write a test for
9
6
  Action.register :shell_postgres, nil do |args, context|
10
7
  c = context[:config]
11
8
  psql = c[:psql]
@@ -8,7 +8,7 @@ class SequelTools::ActionsManager
8
8
  description = 'Show migrations status (applied and missing in migrations path vs unapplied)'
9
9
  Action.register :status, description do |args, context|
10
10
  unapplied, files_missing = MigrationUtils.migrations_differences context
11
- path = context[:config][:db_migrations_location]
11
+ path = context[:config][:migrations_location]
12
12
  unless files_missing.empty?
13
13
  puts "The following migrations were applied to the database but can't be found in #{path}:"
14
14
  puts files_missing.map{|fn| " - #{fn}" }.join("\n")
@@ -6,15 +6,16 @@ module SequelTools
6
6
 
7
7
  URI_BUILDER = ->(config, dbname = config[:dbname]) do
8
8
  c = config
9
- uri_parts = [ "#{c[:dbadapter]}://" ]
10
- if user = c[:username]
11
- uri_parts << user
12
- uri_parts << ':' << c[:password] if c[:password]
13
- uri_parts << '@'
14
- end
9
+ uri_parts = []
10
+ uri_parts << 'jdbc:' if RUBY_PLATFORM == 'java'
11
+ uri_parts << "#{c[:jdbc_adapter] || c[:dbadapter]}://"
15
12
  uri_parts << c[:dbhost]
16
13
  uri_parts << ':' << c[:dbport] if c[:dbport]
17
14
  uri_parts << '/' << dbname
15
+ if user = c[:username]
16
+ uri_parts << '?user=' << user
17
+ uri_parts << '&password=' << c[:password] if c[:password]
18
+ end
18
19
  uri_parts.join('')
19
20
  end
20
21
 
@@ -33,6 +34,9 @@ module SequelTools
33
34
  rake_context.instance_eval do
34
35
  desc action.description unless action.description.nil?
35
36
  task action.name, action.arg_names do |t, args|
37
+ require_relative 'actions/before_task'
38
+ ctx[:current_action] = action
39
+ Action[:before_task].run args, ctx
36
40
  action.run args, ctx
37
41
  end
38
42
  end
@@ -3,7 +3,6 @@
3
3
  require_relative 'actions/connect_db'
4
4
  require_relative 'actions/create_db'
5
5
  require_relative 'actions/drop_db'
6
- require_relative 'actions/schema_dump_postgres'
7
6
  require_relative 'actions/schema_dump'
8
7
  require_relative 'actions/schema_load'
9
8
  require_relative 'actions/new_migration'
@@ -17,5 +16,6 @@ require_relative 'actions/redo'
17
16
  require_relative 'actions/version'
18
17
  require_relative 'actions/rollback'
19
18
  require_relative 'actions/status'
20
- require_relative 'actions/shell_postgres'
21
19
  require_relative 'actions/shell'
20
+ require_relative 'actions/irb'
21
+ require_relative 'actions/postgresql_support'
@@ -22,8 +22,9 @@ class MigrationUtils
22
22
  options = { allow_missing_migration_files: true }
23
23
  options[:target] = 0 if direction == :down
24
24
  config = context[:config]
25
- Sequel::Migrator.migrator_class(config[:db_migrations_location]).
26
- new(context[:db], config[:db_migrations_location], options)
25
+ options[:table] = config[:migrations_table] if config[:migrations_table]
26
+ Sequel::Migrator.migrator_class(config[:migrations_location]).
27
+ new(context[:db], config[:migrations_location], options)
27
28
  end
28
29
 
29
30
  def self.current_version(context)
@@ -32,7 +33,7 @@ class MigrationUtils
32
33
  end
33
34
 
34
35
  def self.last_found_migration(context)
35
- migrations_path = context[:config][:db_migrations_location]
36
+ migrations_path = context[:config][:migrations_location]
36
37
  migrator = find_migrator(context)
37
38
  migrator.ds.order(Sequel.desc(migrator.column)).select_map(migrator.column).find do |fn|
38
39
  File.exist?("#{migrations_path}/#{fn}")
@@ -41,7 +42,7 @@ class MigrationUtils
41
42
 
42
43
  def self.migrations_differences(context)
43
44
  config = context[:config]
44
- migrations_path = config[:db_migrations_location]
45
+ migrations_path = config[:migrations_location]
45
46
  existing = Dir["#{migrations_path}/*.rb"].map{|fn| File.basename fn }.sort
46
47
  migrator = find_migrator context
47
48
  migrated = migrator.ds.select_order_map(migrator.column)
@@ -1,5 +1,5 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  module SequelTools
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.8'
5
5
  end
data/sequel_tools.gemspec CHANGED
@@ -32,7 +32,8 @@ Gem::Specification.new do |spec|
32
32
  spec.require_paths = ['lib']
33
33
 
34
34
  spec.add_runtime_dependency 'sequel'
35
- spec.add_development_dependency 'pg'
35
+ spec.add_development_dependency 'pg' unless RUBY_PLATFORM == 'java'
36
+ spec.add_development_dependency 'jdbc-postgres' if RUBY_PLATFORM == 'java'
36
37
  spec.add_development_dependency 'bundler'
37
38
  spec.add_development_dependency 'rake'
38
39
  spec.add_development_dependency 'rspec'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Rosenfeld Rosas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-16 00:00:00.000000000 Z
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -99,19 +99,21 @@ files:
99
99
  - ".rspec"
100
100
  - ".travis.yml"
101
101
  - Gemfile
102
- - Gemfile.lock
103
102
  - LICENSE.txt
104
103
  - README.md
105
104
  - Rakefile
106
105
  - bin/console
107
106
  - bin/setup
108
107
  - lib/sequel_tools.rb
108
+ - lib/sequel_tools/actions/before_task.rb
109
109
  - lib/sequel_tools/actions/connect_db.rb
110
110
  - lib/sequel_tools/actions/create_db.rb
111
111
  - lib/sequel_tools/actions/down.rb
112
112
  - lib/sequel_tools/actions/drop_db.rb
113
+ - lib/sequel_tools/actions/irb.rb
113
114
  - lib/sequel_tools/actions/migrate.rb
114
115
  - lib/sequel_tools/actions/new_migration.rb
116
+ - lib/sequel_tools/actions/postgresql_support.rb
115
117
  - lib/sequel_tools/actions/redo.rb
116
118
  - lib/sequel_tools/actions/reset.rb
117
119
  - lib/sequel_tools/actions/rollback.rb
@@ -152,8 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
154
  - !ruby/object:Gem::Version
153
155
  version: '0'
154
156
  requirements: []
155
- rubyforge_project:
156
- rubygems_version: 2.6.13
157
+ rubygems_version: 3.2.3
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: Add Rake tasks to manage Sequel migrations
data/Gemfile.lock DELETED
@@ -1,39 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- sequel_tools (0.1.3)
5
- sequel
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- diff-lcs (1.3)
11
- pg (0.21.0)
12
- rake (12.3.0)
13
- rspec (3.7.0)
14
- rspec-core (~> 3.7.0)
15
- rspec-expectations (~> 3.7.0)
16
- rspec-mocks (~> 3.7.0)
17
- rspec-core (3.7.0)
18
- rspec-support (~> 3.7.0)
19
- rspec-expectations (3.7.0)
20
- diff-lcs (>= 1.2.0, < 2.0)
21
- rspec-support (~> 3.7.0)
22
- rspec-mocks (3.7.0)
23
- diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.7.0)
25
- rspec-support (3.7.0)
26
- sequel (5.3.0)
27
-
28
- PLATFORMS
29
- ruby
30
-
31
- DEPENDENCIES
32
- bundler
33
- pg
34
- rake
35
- rspec
36
- sequel_tools!
37
-
38
- BUNDLED WITH
39
- 1.16.0