You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
7
+
## 1.6.0
8
+
9
+
### Added
10
+
11
+
- Added `delete_missing` option to `sync_table_data!` and `sync_all!`. When set to `true`, any records in the database that are not defined in the data files will be deleted. This option defaults to `false` to preserve backward compatibility.
Copy file name to clipboardExpand all lines: README.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,6 +240,21 @@ Status.sync_table_data!
240
240
241
241
This will add any missing records to the table and update existing records so that the attributes in the table match the values in the data files. Records that do not appear in the data files will not be touched. Any attributes not specified in the data files will not be changed.
242
242
243
+
If you want to remove records from the database that are no longer in the data files, you can pass `delete_missing: true`:
244
+
245
+
```ruby
246
+
Status.sync_table_data!(delete_missing:true)
247
+
```
248
+
249
+
This option can also be passed to `SupportTableData.sync_all!`:
250
+
251
+
```ruby
252
+
SupportTableData.sync_all!(delete_missing:true)
253
+
```
254
+
255
+
> [!CAUTION]
256
+
> Use `delete_missing` with care. It will delete any records in the table that are not defined in the data files, which may include user-created data or fail due to foreign key constraints.
257
+
243
258
The number of records contained in data files should be fairly small (ideally fewer than 100). It is possible to load just a subset of rows in a large table because only the rows listed in the data files will be synced. You can use this feature if your table allows user-entered data, but has a few rows that must exist for the code to work.
244
259
245
260
Loading data is done inside a database transaction. No changes will be persisted to the database unless all rows for a model can be synced.
@@ -309,6 +324,20 @@ end
309
324
310
325
You must also call `SupportTableData.sync_all!` before running your test suite. This method should be called in the test suite setup code after any data in the test database has been purged and before any tests are run.
311
326
327
+
> [!TIP]
328
+
> If you are using a truncation database cleaning strategy exclude the support tables from the tables that get truncated. Syncing data is much faster when the data is already in the database. You should use the `delete_missing` option to remove any records that are not in the data files instead of deleting all records before each test.
329
+
330
+
```ruby
331
+
# Excluding support tables from truncation in DatabaseCleaner configuration.
0 commit comments