yeet_dba 0.2.1 → 1.0.0
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 +4 -4
- data/CHANGELOG.md +6 -2
- data/Gemfile.lock +1 -1
- data/README.md +28 -40
- data/lib/yeet_dba/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4ebeaa010c0aac25fec89ae37bfa612bba92ac059f409d555a41c62a591e9ac
|
4
|
+
data.tar.gz: 2a1936a44bb7ec4f776420be9517c08604fc6ca5f0f7f4855adf8458cd6cda61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aca3b09485598e836fcd99c4a8dde0986dfa2a602b69c15fe2f45e06472e050868be0200760eec6c77779a4f0891912b8626f65890028d041ac406960266a9c8
|
7
|
+
data.tar.gz: 06e42dba1df5abdc3aa4e2f5a0c0e8fc998838d820378cb6df994c6a247448740c09fac9e31b153a234e1b2139ad01a67d632d2f9e9c33de4fb701e5b357adf7
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|

|
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
|
-
##
|
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
|
-
|
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
|
-
|
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:
|
77
|
+
$ RAILS_ENV=production rake yeet_dba:nullify_or_destroy_invalid_rows
|
93
78
|
```
|
94
79
|
|
95
|
-
|
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
|
-
|
85
|
+
$ RAILS_ENV=production rails g yeet_dba:foreign_key_migration
|
86
|
+
```
|
99
87
|
|
100
|
-
|
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
|
-
|
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
|
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
|
-
|
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
|
-
|
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
|
-
- [
|
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
|
+
|
data/lib/yeet_dba/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2019-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|