activerecord-dbsnapshot 0.1.1 → 0.1.3

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: 6f6d0ba8702b72497080db038c570316e8fa86695435e2ad531db0e9ac21aae4
4
- data.tar.gz: 95b154ff4dea18afd990dc301df252b391f6fac571213dfd8a4119f955bc172e
3
+ metadata.gz: 6176a0c48f0c782707247c24aeb2e4d93f9a497729042c9652aeeb3336ee640e
4
+ data.tar.gz: f29644ac30106b66e5b32b691c11990a75c85bd9a6cc461cab6e92c83d2dd133
5
5
  SHA512:
6
- metadata.gz: e2d8a26f0b16c39de9f17b4ef73a81881ae4c4deb609c36853094673a958a633baa0b7cdeabc18c9529c38b159aa3c235bf128970d8ff6e4f27d3e4b72cb428c
7
- data.tar.gz: 97b58f4bfcb9feaaf99a6f5305aa0ddc22785d7b8413b9964008d218cf1e4713660e29255f82bfa5e7199b46b12d51bd642fa7dad9ecf1a37761705d6528c222
6
+ metadata.gz: 341b735fe8abf9069eb08e3950d8869c98ce7a6a0b4159f9cdaebe392e5f537842fb4494dadc3871f460de2681cf5d96247bca9c4f2c3492ee1935686882dfcb
7
+ data.tar.gz: d164865468a7a414588c082d82e9783b08b62c4f6e125cc8fd000112e444339498c0344e34c64f98a63001fbb63af9204725ce48f541907b82f4096eef577b6a
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Activerecord
4
4
  module Dbsnapshot
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
@@ -26,8 +26,7 @@ module ActiveRecord
26
26
  require latest_config_file
27
27
 
28
28
  klass_name = File.basename(latest_config_file, '.rb').gsub(/^[^_]+_/, '').camelize
29
- klass = klass_name.constantize
30
- klass
29
+ klass_name.constantize
31
30
  end
32
31
 
33
32
  # Finds snapshot configuration by name
@@ -42,6 +41,19 @@ module ActiveRecord
42
41
  ActiveRecord::DbSnapshot.new
43
42
  end
44
43
 
44
+ def build_restore_command(db_config, file)
45
+ command = "pg_restore"
46
+ command << " --host=#{db_config[:host]}" if db_config[:host]
47
+ command << " --port=#{db_config[:port]}" if db_config[:port]
48
+ command << " --username=#{db_config[:username]}" if db_config[:username]
49
+ command << " --dbname=#{db_config[:database]}" if db_config[:database]
50
+ command << " --no-owner" # Optional: Prevents restoring ownership information
51
+ command << " --disable-triggers" # Optional: Disable triggers during data restore
52
+ command << " --clean" # Optional: Clean (drop) existing database objects before restore
53
+ command << " #{file}" # SQL file to restore
54
+ command
55
+ end
56
+
45
57
  # Builds the dump command based on configuration
46
58
  def build_dump_command(config, db_config, output_file)
47
59
  pg_dump_cmd = "pg_dump"
@@ -59,8 +71,8 @@ module ActiveRecord
59
71
  end
60
72
 
61
73
  # Executes the dump command
62
- def execute_dump_command(dump_command)
63
- system(dump_command)
74
+ def execute_command(command)
75
+ system(command)
64
76
  end
65
77
 
66
78
  private
@@ -25,20 +25,22 @@ namespace :db do
25
25
  next
26
26
  end
27
27
 
28
- db_config = ActiveRecord::Base.connection_config
28
+ db_config = ActiveRecord::Base.connection_db_config
29
29
  output_file = Rails.root.join('db', 'snapshots', "#{config.snapshot_name}.sql")
30
30
 
31
- puts output_file
32
-
33
-
34
- dump_command = ActiveRecord::DbSnapshot.build_dump_command(config, db_config, output_file)
35
- ActiveRecord::DbSnapshot.execute_dump_command(dump_command)
31
+ command = ActiveRecord::DbSnapshot.build_dump_command(config, db_config, output_file)
32
+ ActiveRecord::DbSnapshot.execute_command(command)
36
33
 
37
34
  puts "Database dump completed: #{output_file}"
38
35
  end
39
36
 
40
37
  desc "Restore a snapshot SQL file into the database"
41
38
  task :restore, [:name] => :environment do |_, args|
39
+ unless `which pg_restore`.present?
40
+ puts "Error: pg_restore not found. Ensure PostgreSQL client tools are installed."
41
+ next
42
+ end
43
+
42
44
  name = args[:name]
43
45
  filename = "db/snapshots/#{name}.sql"
44
46
 
@@ -58,8 +60,9 @@ namespace :db do
58
60
  end
59
61
 
60
62
  # Restore the SQL file into the database
61
- sql = File.read(filename)
62
- ActiveRecord::Base.connection.execute(sql)
63
+ db_config = ActiveRecord::Base.connection_db_config
64
+ command = ActiveRecord::DbSnapshot.build_restore_command(db_config, filename)
65
+ ActiveRecord::DbSnapshot.execute_command(command)
63
66
 
64
67
  puts "Snapshot '#{name}' restored successfully."
65
68
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-dbsnapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-06 00:00:00.000000000 Z
11
+ date: 2024-03-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubygems_version: 3.2.3
59
+ rubygems_version: 3.4.18
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Dump and Restore database snapshots.