yeet_dba 0.2.1 → 1.0.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: 92a86fe5de9138137a8afdd48be9cb53bbc39a637d30da59d3ad5325bdc9b915
4
- data.tar.gz: 3b091561a3fcf620dbc9efd842191d05fb621ef4ff81abbe50c17d32e8b5268f
3
+ metadata.gz: a4ebeaa010c0aac25fec89ae37bfa612bba92ac059f409d555a41c62a591e9ac
4
+ data.tar.gz: 2a1936a44bb7ec4f776420be9517c08604fc6ca5f0f7f4855adf8458cd6cda61
5
5
  SHA512:
6
- metadata.gz: a10647d80a806e4a05da43ed8636762cbc9ce1ff9eef08e0d7510f1e5aed6b5372d00087a44f091658f11f085fd877804a1eb878427f2d26af9d162042ca7a49
7
- data.tar.gz: d98a6c41b3b925b468d3c2c92c7c1fbc416d42d597c943e8842ca2fd4b9ad5304b0ce9cec5419faa055d29d0a79a307db5d2c2e97d18e3db891cd6209f8c6003
6
+ metadata.gz: aca3b09485598e836fcd99c4a8dde0986dfa2a602b69c15fe2f45e06472e050868be0200760eec6c77779a4f0891912b8626f65890028d041ac406960266a9c8
7
+ data.tar.gz: 06e42dba1df5abdc3aa4e2f5a0c0e8fc998838d820378cb6df994c6a247448740c09fac9e31b153a234e1b2139ad01a67d632d2f9e9c33de4fb701e5b357adf7
data/CHANGELOG.md CHANGED
@@ -1,9 +1,13 @@
1
- # 0.2.1
1
+ # 1.0.1 (unreleased, master)
2
2
 
3
- ## Major changes
3
+ ## Minor changes
4
+
5
+ # 1.0.0
4
6
 
5
7
  ## Minor changes
6
8
 
9
+ Improved test coverage
10
+
7
11
  # 0.1.2
8
12
 
9
13
  ## Major changes
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yeet_dba (0.1.2)
4
+ yeet_dba (0.2.1)
5
5
  actionpack (>= 3.0, < 6.0)
6
6
  activerecord (>= 3.0, < 6.0)
7
7
  railties (>= 3.0, < 6.0)
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  ![Foreign Key by Ary Prasetyo from the Noun Project](./yeet_dba.png)
2
3
 
3
4
  # yeet_dba - find missing foreign key constraints
@@ -34,29 +35,9 @@ And then execute:
34
35
 
35
36
  $ bundle
36
37
 
37
- ## Usage
38
-
39
- ### Foriegn keys migration
40
-
41
- This probably should run against the production database so you can know if there are dangling records. If there are records with a value, but not the corresponding table does not have an id, then the migration will fail.
38
+ ## Start here
42
39
 
43
- ```
44
- $ RAILS_ENV=production rails g yeet_dba:foreign_key_migration
45
- ```
46
-
47
- This will create a new migration with for every foreign_key that can safely be added without running into orphaned data errors. We also warn you if active_record models that are missing association declarations (`has_many`, `belongs_to`, etc.)`
48
-
49
- `WARNING - cannot find an association for alternative_housings . supplier_id | suppliers`
50
-
51
- We also warn if we have tables that don't have existing models attached to them. This can be safe to ignore because join tables on many to many relations don't need models, but ideally, everything should have an AR model backing it.
52
-
53
- `WARNING - cannot find a model for alternative_housings . supplier_id | suppliers`
54
-
55
- Finnally, if there is a table that we think should have a foreign key constraint, but there are dangling values we warn you against that too.
56
-
57
- `WARNING - orphaned rows alternative_housings . supplier_id | suppliers`
58
-
59
- ### Invalid rows
40
+ ### 1. Find invalid rows
60
41
 
61
42
  If a row has an id, but there doesn't exist an id the expected associated table, then the row has bad data and should either be fixed by nulling the orphaned row or assigning it to an existing row.
62
43
 
@@ -82,35 +63,43 @@ SELECT "notifications".* FROM "notifications" left join active_storage_attachmen
82
63
 
83
64
  ```
84
65
 
85
- ### Fix invalid rows
66
+ ### 2. Fix invalid rows
86
67
 
87
- If a row has an id, but there doesn't exist an id the expected associated table, then the row has bad data and should either be fixed by nulling the orphaned row or assigning it to an existing row.
68
+ You can either manually repair your data via rails console or direct SQL queries, or you can run a rake task to resolve failures.
88
69
 
89
- This rake task will scan every column for orphaned rows.
70
+ If your rails association requires a value (e.g. `belongs_to :user, required: true`), then we try to delete the row.
71
+
72
+ If your rails association says a value is optional, then we try to nullify the value if the schema alls that column to be null.
73
+
74
+ If your schema does not allow you to nullify a column, we print a warning.
90
75
 
91
76
  ```
92
- $ RAILS_ENV=production rake yeet_dba:fix_invalid_columns
77
+ $ RAILS_ENV=production rake yeet_dba:nullify_or_destroy_invalid_rows
93
78
  ```
94
79
 
95
- Sample output:
80
+ ### 3a. Add foreign keys via migration
81
+
82
+ Now that the database is in a valid state, we can add the foreign keys in a migration.
96
83
 
97
84
  ```
98
- ---RESULTS---
85
+ $ RAILS_ENV=production rails g yeet_dba:foreign_key_migration
86
+ ```
99
87
 
100
- 🚨Houston, we have a problem 🚨. We found 1 invalid column.
88
+ This will create a new migration with for every foreign_key that can safely be added without running into orphaned data errors. We also warn you if active_record models that are missing association declarations (`has_many`, `belongs_to`, etc.)
101
89
 
102
- -> notifications.primary_image_id
103
- Invalid rows: 83
104
- Foreign table: active_storage_attachments
90
+ `WARNING - cannot find an association for alternative_housings . supplier_id | suppliers`
105
91
 
106
- This query should return no results:
107
- SELECT "notifications".* FROM "notifications" left join active_storage_attachments as association_table on association_table.id = notifications.primary_image_id WHERE "notifications"."primary_image_id" IS NOT NULL AND (association_table.id is null)
92
+ We also warn if we have tables that don't have existing models attached to them. This can be safe to ignore because join tables on many to many relations don't need models, but ideally, everything should have an AR model backing it.
108
93
 
109
- ```
94
+ `WARNING - cannot find a model for alternative_housings . supplier_id | suppliers`
110
95
 
111
- ### Add missing foriegn keys as a rake task
96
+ Finally, if there is a table that we think should have a foreign key constraint, but there are dangling values we warn you against that too.
112
97
 
113
- You might want to add foreign keys outside of your regular deployment flow in case there are failures and deployment would be blocked by bad data. This would be especially obnoxious for MySql users since you can't rollback migrations.
98
+ `WARNING - orphaned rows alternative_housings . supplier_id | suppliers`
99
+
100
+ ### 3b. Add missing foreign keys as a rake task
101
+
102
+ You might want to add foreign keys outside of your regular deployment flow in case there are failures and deployment would be blocked by bad data. This would be especially obnoxious for MySql users since you can't rollback migrations on failure.
114
103
 
115
104
  ```
116
105
  $ RAILS_ENV=production rake yeet_dba:add_foreign_keys
@@ -135,9 +124,7 @@ This rake task is idempotent (safe to run as many times as you need).
135
124
  - [x] add rake task identify all dangling records
136
125
  - [x] add rake task to automatically nullify or destroy dangling records
137
126
  - [x] run adding foreign keys as rake task instead of generating a migration
138
- - [ ] support "soft delete" gems
139
- - [ ] Use rails associations to find columns that should be "not null" to [improve performance](https://stackoverflow.com/questions/1017239/how-do-null-values-affect-performance-in-a-database-search)
140
-
127
+ - [x] Use rails associations to find columns that should be "not null" to [improve performance](https://stackoverflow.com/questions/1017239/how-do-null-values-affect-performance-in-a-database-search)
141
128
 
142
129
  ## Development
143
130
 
@@ -167,3 +154,4 @@ Foreign Key by Ary Prasetyo from the Noun Project
167
154
  ## Author
168
155
 
169
156
  Kevin Coleman, [https://kcoleman.me/](https://kcoleman.me)
157
+
@@ -1,3 +1,3 @@
1
1
  module YeetDba
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yeet_dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Coleman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-06 00:00:00.000000000 Z
11
+ date: 2019-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler