Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set up DB import/export scripts #50

Merged
merged 1 commit into from
Sep 26, 2023
Merged

Conversation

dustin-jw
Copy link
Contributor

@dustin-jw dustin-jw commented Sep 22, 2023

Description

This sets up import and export scripts for syncing DBs across environments. Some key benefits of this approach:

  • Files are always gzipped to keep size down when transferring to other devs/environments
  • Naming is flexible so you don't need to rename files before importing/sending to other devs
  • Backups are automatically saved when you import a DB, allowing you to revert back if needed
  • Exported/backed up files are timestamped and can be named, allowing for better organization

Closes #32

Validation DB

You can use this DB export to verify the import functionality.

db-export-2023-09-22T17-48-43_UTC.sql.gz

To Validate

  1. Pull down this branch
  2. Add SITE_URL=https:\\/\\/example.com to your .env file (this is used to replace absolute URLs based on environment)
  3. Run npm start
  4. Run npm run backup-db to save your current DB (confirm that a file is added to sql/backups)
  5. Run npm run backup-db named-backup to confirm that this uses the name you give it instead of db-backup-*
  6. Download the provided DB export for testing and put it in your sql folder
  7. Run npm run import-db, confirming that another backup is created before the import happens, and that the imported file is moved to sql/previous-imports when the script is finished running
  8. Visit http://localhost:8000 and confirm that there's a post with the title "This test post is for data exports!"
  9. Run npm run export-db, confirming that a gzipped sql file is added to sql/exports
  10. Run npm run export-db named-export, confirming that the new export file uses the name you gave it instead of db-export-*
  11. Move your original backup DB to the top-level sql folder and run npm run import-db to get your original DB back

@dustin-jw dustin-jw marked this pull request as draft September 22, 2023 18:16
@dustin-jw dustin-jw marked this pull request as ready for review September 22, 2023 18:23
Copy link
Contributor

@rise-erpelding rise-erpelding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! It sure does make working with database imports and exports less of a headache--they can now be included easily on PRs, imported and exported super easily without having to rename files, and it is so fast for me to switch to a database vs. importing it as an .xml. Your documentation is also great! 🌟

@@ -7,3 +7,7 @@ MYSQL_ROOT_PASSWORD=

# Set `WP_ENV` to `yourLastName-local` (ex. `smith-local`).
WP_ENV=sparkuser-local

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR but I'm just realizing we don't have GOOGLE_API_KEY in here, should we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little more inclined to delete the reference to GOOGLE_API_KEY, since it isn't actually used for anything. It is useful as an example, though, so maybe we could make it clearer that it is just an example–otherwise I could see someone getting thrown off if they set an API key and nothing changed about the site. Maybe like this?

function define_theme_globals() {
	// phpcs:ignore
	define( 'CUSTOM_ENVIRONMENT_VARIABLE', $_SERVER['CUSTOM_ENVIRONMENT_VARIABLE'] ?? '' );
}
add_filter( 'init', 'define_theme_globals' );

I'll create an issue for it, and we can work out the details from there.

- add basic script to export the DB
- timestamp and gzip DB exports
- support backups and named exports
- add npm scripts for backups and exports
- add script to import DB
- add npm scripts to backup, then import a new DB
- add instructions for DB script usage
- move used import files out of the sql folder when done
@dustin-jw dustin-jw merged commit 8960a6e into main Sep 26, 2023
1 check passed
@dustin-jw dustin-jw deleted the chore--db-scripts branch September 26, 2023 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add database import / export scripts
2 participants