db-purger 0.1.0 → 0.1.2

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: 5b7144cdb2cda2433339283a8ca98c35ace1015c30fce5642a4c064234f07e72
4
- data.tar.gz: 8b199fe73457afb3dabf37a2e6746cc839d42514a0ff81b99e0bd0ab62a1d2b5
3
+ metadata.gz: 14ec62643b185d00f26b61c57db34008d36fc58f2b92bf2375ba8501d3dd065d
4
+ data.tar.gz: db80d215880b0b8d889a986ae6f75b524c0fe4297bee086f516a5f5a0fcc2da5
5
5
  SHA512:
6
- metadata.gz: 3c56fb34ca9276ad3e3e258825f703631f80ab6a1711a8e703feb4fb2b2d5676672c221f2f86ca8c310712928eb23705d86cb7fbdca8e01e56e2e0b653e859a3
7
- data.tar.gz: 7283b99020d4164eadba078b9560921339c4e87d467bad5b0870991f6ed00f42be464db212949e6f9e083cc3d8d79e8bedada03bb3450bf36c2d57cfd5776d64
6
+ metadata.gz: abeaae8ae9930615a84e9639de423cef35869abf5fe5fd0a47caef1682135fcf61fc9e2fc127531db2ee3c104e322d4c6a8ce00984f3efdb569934813c0ce26c
7
+ data.tar.gz: ecfcd9b6c3557a1ba9fa2933ec0a4dd57698e95b528bf613adff62121136ed2f5514e579bd56775b8e8886c75e6e6d93ce5e6e31ba34c95577c8b7c3dd04881f
@@ -3,8 +3,11 @@
3
3
  module DBPurger
4
4
  # DBPurger::Config keeps track of global config options for the purge process
5
5
  class Config
6
+ DEFAULT_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
7
+
6
8
  attr_writer :explain,
7
- :explain_file
9
+ :explain_file,
10
+ :datetime_format
8
11
 
9
12
  def explain?
10
13
  @explain == true
@@ -13,5 +16,9 @@ module DBPurger
13
16
  def explain_file
14
17
  (@explain_file || $stdout)
15
18
  end
19
+
20
+ def datetime_format
21
+ @datetime_format || DEFAULT_DATETIME_FORMAT
22
+ end
16
23
  end
17
24
  end
@@ -65,6 +65,8 @@ module DBPurger
65
65
  table.foreign_key = options[:foreign_key]
66
66
  table.batch_size = options[:batch_size]
67
67
  table.conditions = options[:conditions]
68
+ table.mark_deleted_field = options[:mark_deleted_field]
69
+ table.mark_deleted_value = options[:mark_deleted_value]
68
70
  build_nested_plan(table, &block) if block
69
71
  table
70
72
  end
@@ -54,9 +54,9 @@ module DBPurger
54
54
  num_records: num_records) do |payload|
55
55
  records_deleted =
56
56
  if ::DBPurger.config.explain?
57
- delete_sql = scope.to_sql.sub(/SELECT .*?FROM/, 'DELETE FROM')
58
- ::DBPurger.config.explain_file.puts(delete_sql)
59
- scope.count
57
+ explain(scope)
58
+ elsif @table.mark_deleted_field
59
+ scope.update_all(@table.mark_deleted_field => @table.mark_deleted_value)
60
60
  else
61
61
  scope.delete_all
62
62
  end
@@ -66,6 +66,37 @@ module DBPurger
66
66
  end
67
67
  end
68
68
 
69
+ def explain(scope)
70
+ sql =
71
+ if @table.mark_deleted_field
72
+ explain_update_sql(scope)
73
+ else
74
+ scope.to_sql.sub(/SELECT .*?FROM/, 'DELETE FROM')
75
+ end
76
+ ::DBPurger.config.explain_file.puts(sql)
77
+ scope.count
78
+ end
79
+
80
+ def explain_update_sql(scope)
81
+ sql = scope.to_sql.dup
82
+ sql.sub!(/SELECT .*?FROM/, 'UPDATE')
83
+ sql.sub!('WHERE', "SET #{mark_deleted_field_quoted} = #{mark_deleted_value_quoted} WHERE")
84
+ sql
85
+ end
86
+
87
+ def mark_deleted_field_quoted
88
+ @mark_deleted_field_quoted ||= model.connection.quote_column_name(@table.mark_deleted_field)
89
+ end
90
+
91
+ def mark_deleted_value_quoted
92
+ @mark_deleted_value_quoted ||=
93
+ if @table.mark_deleted_value.is_a?(Time)
94
+ model.connection.quote(@table.mark_deleted_value.strftime(::DBPurger.config.datetime_format))
95
+ else
96
+ model.connection.quote(@table.mark_deleted_value)
97
+ end
98
+ end
99
+
69
100
  def delete_records(batch)
70
101
  if foreign_tables?
71
102
  delete_records_and_foreign_tables(batch)
@@ -8,7 +8,10 @@ module DBPurger
8
8
  attr_accessor :foreign_key,
9
9
  :batch_size,
10
10
  :conditions,
11
- :search_proc
11
+ :search_proc,
12
+ :mark_deleted_field
13
+
14
+ attr_writer :mark_deleted_value
12
15
 
13
16
  attr_reader :name,
14
17
  :field
@@ -40,5 +43,9 @@ module DBPurger
40
43
  def fields
41
44
  [@field] + foreign_keys
42
45
  end
46
+
47
+ def mark_deleted_value
48
+ @mark_deleted_value || 1
49
+ end
43
50
  end
44
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-purger
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
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dynamic-active-model