sequel-db_tasks 0.1.0 → 0.2.0

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
2
  SHA256:
3
- metadata.gz: 9b88935e4a3b26336e0486debb41ddf602eabc687913453aeb7aca56553c1d33
4
- data.tar.gz: 0f925a0948ed134954523c7b845434fe3e7dcd971d0d5af7c79e96f8a1ad1a2e
3
+ metadata.gz: aa4f1f71edc8b104b15f64cded74d7cbe0d16029038763e7d4b23491292f3c25
4
+ data.tar.gz: f07aebf68979b26281e503df1c715ef444e44e2e4950b0f971f7f68a18b8a5f7
5
5
  SHA512:
6
- metadata.gz: c80b76b56cb336a42d246b653179d8fda48631cd595b73d4fce7680da61ad820137b8da51ccf9ae7f992255aa4af34631d2bdd277af1bda2df7c4e77303aedd7
7
- data.tar.gz: 1f15c1a78ac1eaf4986cde3434d0a8d51957059b1541aa3bd406202d0b82990d78a93d29b7bb0f91e3a8d3779bc5571aff681dcf78e650412ba358e883f199dc
6
+ metadata.gz: 2120f3f8cd89095ce6e8aa2cd83d2e2a75539a51007d8e5f4c7a2baf52a18e98de4f54a0d93b2f787c604144a0e1391fe0d03104a27dcd0594c73296325ccdfa
7
+ data.tar.gz: 94851bf553e1475ec1a4952dd9fbb30d4a2d95c8331bc9456ef561edd1aec4f417c961b5e3e10ae1d034d9d9bae945f6aa802d9e9be843bfd484d31c3dd9aa6e
data/README.md CHANGED
@@ -2,10 +2,15 @@
2
2
  Rake CLI tasks for Sequel ORM and Postgres. All available tasks:
3
3
 
4
4
  ```
5
+ rake db:create_user # Create database user
6
+ rake db:drop_user # Drop database user
7
+
5
8
  rake db:create # Create database
6
9
  rake db:drop # Drop database
10
+
7
11
  rake db:gm[name] # Generate migration file
8
12
  rake db:migrate[version] # Run migrations
13
+
9
14
  rake db:schema:print # Print current database schema
10
15
  ```
11
16
 
@@ -42,8 +47,8 @@ You can tweak settings using configure block:
42
47
  # Rakefile
43
48
 
44
49
  Sequel::DbTasks.configure do |config|
45
- database_url: ENV["DATABASE_URL"], # default value
46
- migrations_path: "db/migrate" # default value
50
+ config.database_url = ENV["DATABASE_URL"] # default value
51
+ config.migrations_path = "db/migrate" # default value
47
52
  end
48
53
 
49
54
  Sequel::DbTasks.load!
@@ -51,7 +56,26 @@ Sequel::DbTasks.load!
51
56
 
52
57
  ## Notes
53
58
 
54
- Inspired by https://github.com/sandelius/sequel-rake
59
+ * Inspired by https://github.com/sandelius/sequel-rake
60
+
61
+ * Capistrano task to migrate database while deployning:
62
+
63
+ ```ruby
64
+ # lib/capistrano/tasks/sequel_migrate.rake
65
+
66
+ namespace :deploy do
67
+ desc "Migrate sequel db"
68
+ task :migrate do
69
+ on roles(:all) do
70
+ within release_path do
71
+ execute :rake, 'db:migrate'
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ after 'deploy:updated', 'deploy:migrate'
78
+ ```
55
79
 
56
80
  ## License
57
81
 
@@ -1,5 +1,4 @@
1
1
  require 'ostruct'
2
- require "sequel/db_tasks/version"
3
2
 
4
3
  module Sequel
5
4
  module DbTasks
@@ -4,38 +4,67 @@ namespace :db do
4
4
  task :preload do
5
5
  require 'uri'
6
6
 
7
- url = Sequel::DbTasks.configuration.database_url
8
- uri = URI.parse(url)
7
+ @url = Sequel::DbTasks.configuration.database_url
8
+ uri = URI.parse(@url)
9
9
  raise "DB adapter is not postgres" if uri.scheme != "postgres"
10
-
11
- @conf = {
12
- url: url,
13
- db: uri.path.sub("/", ""),
14
- host: uri.host,
15
- port: uri.port,
16
- user: uri.user,
17
- password: uri.password
18
- }
19
10
  end
20
11
 
12
+ ###
13
+
21
14
  # https://www.postgresql.org/docs/current/app-createdb.html
22
15
  desc "Create database"
23
16
  task create: :preload do
24
- env = { "PGPASSWORD" => @conf[:password] }
25
- if system env, "createdb", @conf[:db], "-h", @conf[:host], "-p", @conf[:port].to_s, "-U", @conf[:user]
26
- puts "Created database '#{@conf[:db]}'"
27
- end
17
+ env = { "DATABASE_URL" => @url }
18
+ exec env, "postgressor", "createdb"
28
19
  end
29
20
 
30
- # https://www.postgresql.org/docs/9.3/app-dropdb.html
21
+ # https://www.postgresql.org/docs/current/app-dropdb.html
31
22
  desc "Drop database"
32
23
  task drop: :preload do
33
- env = { "PGPASSWORD" => @conf[:password] }
34
- if system env, "dropdb", @conf[:db], "-h", @conf[:host], "-p", @conf[:port].to_s, "-U", @conf[:user]
35
- puts "Dropped database '#{@conf[:db]}'"
36
- end
24
+ env = { "DATABASE_URL" => @url }
25
+ exec env, "postgressor", "dropdb"
26
+ end
27
+
28
+ ###
29
+
30
+ # https://www.postgresql.org/docs/current/app-pgdump.html
31
+ desc "Dump (backup) database"
32
+ task dump: :preload do
33
+ env = { "DATABASE_URL" => @url }
34
+ exec env, "postgressor", "dumpdb"
35
+ end
36
+
37
+ # https://www.postgresql.org/docs/current/app-pgrestore.html
38
+ desc "Restore database from backup"
39
+ task :restore, [:restore_dump_file_path] do |t, args|
40
+ raise "Restore dump file path is not provided" unless args.restore_dump_file_path
41
+ Rake::Task["db:preload"].execute
42
+
43
+ env = { "DATABASE_URL" => @url }
44
+ command = ["postgressor", "restoredb", args.restore_dump_file_path]
45
+ command << "--switch_to_superuser" if ENV["AS_SUPERUSER"] == "true"
46
+
47
+ exec env, *command
37
48
  end
38
49
 
50
+ ###
51
+
52
+ # https://www.postgresql.org/docs/current/app-createuser.html
53
+ desc "Create database user" # superuser is optional
54
+ task create_user: :preload do
55
+ env = { "DATABASE_URL" => @url }
56
+ exec env, "postgressor", "createuser"
57
+ end
58
+
59
+ # https://www.postgresql.org/docs/current/app-dropuser.html
60
+ desc "Drop database user"
61
+ task drop_user: :preload do
62
+ env = { "DATABASE_URL" => @url }
63
+ exec env, "postgressor", "dropuser"
64
+ end
65
+
66
+ ###
67
+
39
68
  # https://github.com/jeremyevans/sequel/blob/master/doc/migration.rdoc#a-basic-migration
40
69
  desc "Generate migration file"
41
70
  task :gm, [:name] do |t, args|
@@ -69,7 +98,7 @@ namespace :db do
69
98
  Sequel.extension :migration
70
99
  version = args[:version].to_i if args[:version]
71
100
 
72
- Sequel.connect(@conf[:url], logger: Logger.new(STDOUT)) do |db|
101
+ Sequel.connect(@url, logger: Logger.new(STDOUT)) do |db|
73
102
  Sequel::Migrator.run(db, Sequel::DbTasks.configuration.migrations_path, target: version)
74
103
  end
75
104
  end
@@ -77,6 +106,6 @@ namespace :db do
77
106
  # https://github.com/jeremyevans/sequel/blob/master/doc/migration.rdoc#dumping-the-current-schema-as-a-migration
78
107
  desc "Print current database schema"
79
108
  task :'schema:print' => :preload do
80
- exec "bundle", "exec", "sequel", "-d", @conf[:url]
109
+ exec "bundle", "exec", "sequel", "-d", @url
81
110
  end
82
111
  end
@@ -1,11 +1,9 @@
1
-
2
1
  lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "sequel/db_tasks/version"
5
3
 
6
4
  Gem::Specification.new do |spec|
7
5
  spec.name = "sequel-db_tasks"
8
- spec.version = Sequel::DbTasks::VERSION
6
+ spec.version = "0.2.0"
9
7
  spec.authors = ["Victor Afanasev"]
10
8
  spec.email = ["vicfreefly@gmail.com"]
11
9
 
@@ -22,6 +20,8 @@ Gem::Specification.new do |spec|
22
20
  spec.require_paths = ["lib"]
23
21
 
24
22
  spec.add_dependency "sequel"
23
+ spec.add_dependency "postgressor"
24
+
25
25
  spec.add_development_dependency "bundler", "~> 1.16"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
27
  spec.add_development_dependency "minitest", "~> 5.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-db_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Afanasev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-12 00:00:00.000000000 Z
11
+ date: 2019-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: postgressor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +96,6 @@ files:
82
96
  - bin/setup
83
97
  - lib/sequel/db_tasks.rb
84
98
  - lib/sequel/db_tasks/tasks.rake
85
- - lib/sequel/db_tasks/version.rb
86
99
  - sequel-db_tasks.gemspec
87
100
  homepage: https://github.com/vifreefly/sequel-db_tasks
88
101
  licenses:
@@ -1,5 +0,0 @@
1
- module Sequel
2
- module DbTasks
3
- VERSION = "0.1.0"
4
- end
5
- end