activerecord-dbsnapshot 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 54a0b9a73c7af8b2c2f02e7b6a15b476ce11544309d3e293f4078fdbf468bdc6
4
- data.tar.gz: 4854a6d9eff890a5f9bd01f0d67422eba4620fc027bb8bd9bb9a0373dddc9115
3
+ metadata.gz: 3e1622e0e7ec44461ef0dbb0e36def428afdf27e8b27a163b5bb494955ec168d
4
+ data.tar.gz: 1fefe1899a23195363a6c0b1af4600a224e515b71777a9403ed34f8b51f1caca
5
5
  SHA512:
6
- metadata.gz: 74857122ff508b761901ea8020826c1bd5831f8e4247a61d4d4bdc091d52ab45838116dc6b4666043a0869f3c0a7fca027f0823943d802b6542c6a1749719a90
7
- data.tar.gz: 79d48c80d7880338520575b9dda121b9a21c1a6974c689fb3c9f73cacfcb739d43e20711533897582e39f4e7463713351cac3bef982c362256e1f284ac7919a5
6
+ metadata.gz: 6f2ccb151614bf0fc8d9937b05b6187f25dc0e2f9fab8bcc872e5afd8f0613b9e30f43a34091399986b5fd13c6514fec17de5a9815bf43f02f726cbf8bfb4dc6
7
+ data.tar.gz: d03777d254b049ca37717b6e33f304f2177886e825ebb54142fa54078f31ca436e355bf8090dc8d564e5d4aedc08075e4ccab35011537b0119057c9d543f8132
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Activerecord
4
4
  module Dbsnapshot
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
@@ -26,10 +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
-
31
- puts klass.snapshot_name, klass.excluded_tables.inspect
32
- klass
29
+ klass_name.constantize
33
30
  end
34
31
 
35
32
  # Finds snapshot configuration by name
@@ -44,6 +41,19 @@ module ActiveRecord
44
41
  ActiveRecord::DbSnapshot.new
45
42
  end
46
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
+
47
57
  # Builds the dump command based on configuration
48
58
  def build_dump_command(config, db_config, output_file)
49
59
  pg_dump_cmd = "pg_dump"
@@ -61,8 +71,8 @@ module ActiveRecord
61
71
  end
62
72
 
63
73
  # Executes the dump command
64
- def execute_dump_command(dump_command)
65
- system(dump_command)
74
+ def execute_command(command)
75
+ system(command)
66
76
  end
67
77
 
68
78
  private
@@ -28,17 +28,19 @@ namespace :db do
28
28
  db_config = ActiveRecord::Base.connection_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_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.0
4
+ version: 0.1.2
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: