osmosis 0.1.3 → 0.1.5

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: 8a003b88c32b0dcd67c6ef409c0ac5c560d923882faa7436123f21a642e6def5
4
- data.tar.gz: f81d8284cda6f0cd05f64c25ca3eb5aef1bf700ffe2932aed84604017ceb9ef5
3
+ metadata.gz: 1c32c42fe854c9f29544cb890cc72db30bfb01d705fcd81bf0be4ce011b20881
4
+ data.tar.gz: 2f4a60dd5df8ebad2577f8a04f44532a6b9c67f80ba425915d0c9062b9ae0329
5
5
  SHA512:
6
- metadata.gz: 74e79675ce1f5575c27a4972d9f48732e4d02d90710c71705b73ce0c50fe392da830d428a6889e0fc866a0b596b7fda5ec9faafe053bf27e9e74c07ee890de98
7
- data.tar.gz: d53f62ee450a0746bd36f523df58eeafb4039f2247a627dff92ccc14c4f8d78171cfef3dde806147c0f179b4a8dcada243ebce3a16ebf3014cdad84d28849e2f
6
+ metadata.gz: 3a6e583c73707be4c9ce5f2524515fdf2bbc02173862a8f28c2ef538a5bdd7451ab6210907587f433f754948db3ff284183b17e55d2b6a3366d84f5d22e56085
7
+ data.tar.gz: 4f1e5458d0e5079ffa823f8b8bba4363033f788a646750245342561b157b294db412d9bf4a571b2721c29956ad29752ebf9666a1f4c3d92cad0ae25d311267d7
data/README.md CHANGED
@@ -16,9 +16,21 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
16
 
17
17
  Osmosis provides you with different rake tasks that simplify the management of your database.
18
18
 
19
- ### Database restore
19
+ ### Database import
20
20
 
21
+ The import command takes the `.dump` file provided and restores it to the PostgreSQL database. Any pre-existing data is dropped.
21
22
 
23
+ ```bash
24
+ bundle exec rails db:import <filename>
25
+ ```
26
+
27
+ ### Database export
28
+
29
+ The export command exports the PostgreSQL database into a `.dump` file. If the file already exists, it is overwritten.
30
+
31
+ ```bash
32
+ bundle exec rails db:export <filename>
33
+ ```
22
34
 
23
35
  ## Development
24
36
 
@@ -28,7 +40,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
40
 
29
41
  ## Contributing
30
42
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/osmosis.
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/code-fabrik/osmosis.
32
44
 
33
45
  ## License
34
46
 
@@ -1,35 +1,51 @@
1
+ require 'expect'
1
2
  require 'ostruct'
3
+ require 'pty'
2
4
 
3
5
  module Osmosis
4
6
  class Command
5
- def self.restore(argv)
6
- files = Dir.glob("./*.dump")
7
- case files.size
8
- when 0
9
- return [
10
- OpenStruct.new(type: :print, command: "No backups found for *.dump")
11
- ]
12
- when 1
13
- file = files.first
14
- host = ActiveRecord::Base.connection_db_config.host
15
- database = ActiveRecord::Base.connection_db_config.database
16
- username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]
17
- password = ActiveRecord::Base.connection_db_config.configuration_hash[:password]
18
- cmd = "PGPASSWORD=#{password} pg_restore -U #{username} -h #{host} -d #{database} -cO --if-exists -w < #{file}"
19
- Rake::Task["db:drop"].invoke
20
- Rake::Task["db:create"].invoke
21
- return [
22
- OpenStruct.new(type: :rake, command: 'db:drop'),
23
- OpenStruct.new(type: :rake, command: 'db:create'),
24
- OpenStruct.new(type: :cmd, command: cmd),
25
- OpenStruct.new(type: :print, command: "Restored database '#{database}'"),
26
- ]
27
- else
28
- return [
29
- OpenStruct.new(type: :print, command: "Too many backups found for *.dump:"),
30
- OpenStruct.new(type: :print, command: ' ' + files.join("\n ")),
31
- ]
7
+ def self.import(argv)
8
+ file = argv[0]
9
+ if file.nil?
10
+ return [OpenStruct.new(type: :print, command: "Please provide a filename parameter: rails db:import <filename>")]
32
11
  end
12
+ if !File.exist?(file)
13
+ return [OpenStruct.new(type: :print, command: "File #{file} does not exist")]
14
+ end
15
+ host = ActiveRecord::Base.connection_db_config.host
16
+ database = ActiveRecord::Base.connection_db_config.database
17
+ username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]
18
+ password = ActiveRecord::Base.connection_db_config.configuration_hash[:password]
19
+ cmd = "PGPASSWORD=#{password} pg_restore -U #{username} -h #{host} -d #{database} -cO --if-exists -w < #{file}"
20
+ return [
21
+ OpenStruct.new(type: :rake, command: 'db:drop'),
22
+ OpenStruct.new(type: :rake, command: 'db:create'),
23
+ OpenStruct.new(type: :cmd, command: cmd),
24
+ OpenStruct.new(type: :print, command: "Imported database '#{database}'"),
25
+ ]
26
+ end
27
+
28
+ def self.export(argv)
29
+ file = argv[0]
30
+ if file.nil?
31
+ return [OpenStruct.new(type: :print, command: "Please provide a filename parameter: rails db:export <filename>")]
32
+ end
33
+ host = ActiveRecord::Base.connection_db_config.host
34
+ database = ActiveRecord::Base.connection_db_config.database
35
+ username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]
36
+ password = ActiveRecord::Base.connection_db_config.configuration_hash[:password]
37
+ cmd = "PGPASSWORD=#{password} pg_dump -Fc --no-acl --no-owner -h #{host} -U #{username} -w #{database} > #{file}"
38
+ return [
39
+ OpenStruct.new(type: :cmd, command: cmd),
40
+ OpenStruct.new(type: :print, command: "Exported database '#{database}'"),
41
+ ]
42
+ end
43
+
44
+ def self.connect(argv)
45
+ cmd = "PGPASSWORD=#{password} psql -U #{username} -h #{host} -d #{database}"
46
+ return [
47
+ OpenStruct.new(type: :pty, command: cmd)
48
+ ]
33
49
  end
34
50
 
35
51
  def self.run(commands)
@@ -40,6 +56,25 @@ module Osmosis
40
56
  system cmd.command
41
57
  elsif cmd.type == :print
42
58
  puts cmd.command
59
+ elsif cmd.type == :pty
60
+ PTY.spawn(cmd.command) do |stdout, stdin, pid|
61
+ trap("INT") { Process.kill("INT", pid) }
62
+
63
+ begin
64
+ loop do
65
+ # Read output from psql
66
+ stdout.each { |line| puts line}
67
+
68
+ # Check if there's any input from the user
69
+ if IO.select([STDIN], nil, nil, 0.1)
70
+ user_input = STDIN.gets.chomp
71
+ stdin.puts user_input
72
+ end
73
+ end
74
+ rescue Errno::EIO
75
+ puts "psql session ended"
76
+ end
77
+ end
43
78
  else
44
79
  raise StandardError, "Unknown command type #{cmd.type}"
45
80
  end
@@ -0,0 +1,8 @@
1
+ namespace :db do
2
+ desc "Connects to the application database"
3
+ task connect: :environment do
4
+ commands = Osmosis::Command.connect(ARGV)
5
+ ARGV.clear
6
+ Osmosis::Command.run(commands)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ namespace :db do
2
+ desc "Exports a database to a backup"
3
+ task import: :environment do
4
+ commands = Osmosis::Command.export(ARGV)
5
+ ARGV.clear
6
+ Osmosis::Command.run(commands)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ namespace :db do
2
+ desc "Imports a database from a backup"
3
+ task import: :environment do
4
+ commands = Osmosis::Command.import(ARGV)
5
+ ARGV.clear
6
+ Osmosis::Command.run(commands)
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Osmosis
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osmosis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas_Skywalker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-20 00:00:00.000000000 Z
11
+ date: 2024-10-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This powerful database utility library simplifies and automates common
14
14
  database tasks, including exports, imports, and data migrations. With its intuitive
@@ -28,7 +28,9 @@ files:
28
28
  - lib/osmosis.rb
29
29
  - lib/osmosis/command.rb
30
30
  - lib/osmosis/railtie.rb
31
- - lib/osmosis/tasks/db/restore.rake
31
+ - lib/osmosis/tasks/db/connect.rake
32
+ - lib/osmosis/tasks/db/export.rake
33
+ - lib/osmosis/tasks/db/import.rake
32
34
  - lib/osmosis/version.rb
33
35
  - sig/osmosis.rbs
34
36
  homepage: https://github.com/code-fabrik/osmosis
@@ -1,7 +0,0 @@
1
- namespace :db do
2
- desc "Restores the database from a backup"
3
- task restore: :environment do
4
- commands = Osmosis::Command.restore(ARGV)
5
- Osmosis::Command.run(commands)
6
- end
7
- end