In this episode, we're laying the groundwork for exporting data from our app—specifically, exporting large sets of records (like millions!) to a CSV file. Instead of making users wait around while the export happens, we want a background process: the user can start the export, leave, and come back later to download the file from a list of their exports.
To make this work, we need to keep track of each export operation. So, we set up a new export
model and database table to hold all the info about each export: which user started it, how many records it includes, when it was started and completed, and the filename for the export itself. The filename is especially nice—it includes the table name, the timestamp the export started (formatted to avoid problem characters), and the .csv
extension. We use a handy slug method to make sure it looks tidy and filesystem-friendly.
After building this structure, we test it out by initiating some exports and checking that database records are being created properly, complete with friendly filenames. This approach gives us a robust auditable log for exports and sets the stage for handling heavy lifting in the background.
Next up, we'll actually implement the CSV export logic using this new export record as our anchor.