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

WP-692 Add runSql Blueprint step #745

Merged
merged 25 commits into from
Dec 4, 2023

Conversation

seanmorris
Copy link
Contributor

@seanmorris seanmorris commented Nov 15, 2023

What is this PR doing?

This PR addresses #692, by allowing developers to provide SQL via a resource. The queries will be run as part of the Blueprint setup process.

What problem is it solving?

This allows a user to execute SQL in the context of a playground instance to set up the WordPress install inside.

Currently, in production, there's no easy way to set up Playground for a complex plugin like WooCommerce:

Preparing an entire custom SQLite file is tedious and may not be portable between WordPress versions
The ability to import entire SQL files was requested by multiple developers. Let's support it, or something equally expressive and convenient.

How is the problem addressed?

The following notation is now valid for Blueprint steps:

Run a set of SQL queries:

{
  step: 'runSql',
  sql: {
    resource: "literal",
    contents: "DELETE FROM wp_posts;\nDELETE FROM wp_users;",
    name: "queries.sql",
  }
}

Testing Instructions

  1. Checkout the project & run
nx run playground-website:dev

Click the following link: (ensure the port number matches)
http://localhost:5400/website-server/#{%22landingPage%22:%20%22/wp-admin/edit.php%22,%20%22steps%22:[{%22step%22:%20%22login%22,%20%22username%22:%20%22admin%22,%20%22password%22:%20%22password%22},%20{%20%22step%22:%22runSql%22,%20%22sql%22:%20{%20%22resource%22:%20%22literal%22,%20%22name%22:%20%22queries.sql%22,%20%22contents%22:%20%22DELETE%20FROM%20wp_posts%22%20}%20}]}

You should see that all posts have been deleted.

@adamziel
Copy link
Collaborator

adamziel commented Nov 15, 2023

@seanmorris instead of defining two steps that are only distinguished by how you provide input, let's define a single step and use resources to provide input from different sources – just like the installPlugin step does:

{
	step: 'runSql',
	sql: {
		"resource": "vfs",
		"path": "/wordpress/schema/_Schema.sql"
	}
}
[
	{
		step: 'runSql',
		sql: {
			"resource": "literal",
			"contents": "DELETE FROM wp_posts"
		}
	},
	{
		step: 'runSql',
		sql: {
			"resource": "literal",
			"contents": "DELETE FROM wp_users"
		}
	}
]

That second example feels a bit clunky. I'd say that's fine for starters. If that becomes a problem, though, we might introduce another resource like:

[
	{
		step: 'runSql',
		sql: {
			"resource": "array",
			"glue": "\n",
			"contents": [
				"DELETE FROM wp_users",
				"DELETE FROM wp_posts"
			]
		}
	}
]

@adamziel
Copy link
Collaborator

Capturing what @seanmorris noticed:

I actually noticed a bug with that late last night if you give it a string that doesn't end in a newline, it doesn't handle it right

@seanmorris seanmorris changed the title Creating RunSqlQueryStep & RunSqlFileStep PGP-49 Creating RunSqlQueryStep & RunSqlFileStep Nov 22, 2023
@seanmorris seanmorris changed the title PGP-49 Creating RunSqlQueryStep & RunSqlFileStep WP-692 Creating RunSqlQueryStep & RunSqlFileStep Nov 22, 2023
@seanmorris
Copy link
Contributor Author

Capturing what @seanmorris noticed:

I actually noticed a bug with that late last night if you give it a string that doesn't end in a newline, it doesn't handle it right

This has been fixed.

@dmsnell
Copy link
Member

dmsnell commented Nov 27, 2023

wanted to note that I appreciate the approach of handling each line at a time and reading in chunks from the file. there could be rather large database files and doing it line by line should avoid excessive peak memory use.

@adamziel
Copy link
Collaborator

adamziel commented Nov 28, 2023

Nice! This looks great, @seanmorris, thank you!

Two last things here would be:

@adamziel adamziel assigned seanmorris and unassigned seanmorris Nov 28, 2023
@seanmorris
Copy link
Contributor Author

https://github.com/WordPress/wordpress-playground/pull/745/files#diff-a760bde1f650fd8be213a81d7ce7c07e8ce79bceefdc1439bbe63b48eae591f4R49

@seanmorris
Copy link
Contributor Author

  • This could be as simple as "click this localhost:5400/#{"steps": [... link and confirm the site title says potato" or "take the first usage example above, go to localhost:5400/, paste it after #, confirm X happened".

@adamziel I JUST realized what you meant by this. I forgot that you could just spin up playground and supply a blueprint on the query hash.

I added that to the description.

@adamziel
Copy link
Collaborator

adamziel commented Dec 4, 2023

Looks good! Would you please resolve the merge conflicts? Then, once the CI runs and all the checks are green, we can merge!

@seanmorris
Copy link
Contributor Author

Looks good! Would you please resolve the merge conflicts? Then, once the CI runs and all the checks are green, we can merge!

@adamziel @dmsnell

https://github.com/WordPress/wordpress-playground/actions/runs/7091739719/job/19301742699

Copy link
Collaborator

@adamziel adamziel left a comment

Choose a reason for hiding this comment

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

🚢

@adamziel adamziel merged commit 88efc61 into WordPress:trunk Dec 4, 2023
5 checks passed
adamziel added a commit to WordPress/playground-tools that referenced this pull request Feb 7, 2024
Adds a `playground` plugin for plugin-preview functionality and
renames the plugin to WordPress Playground.

Requires:
WordPress/wordpress-playground#745 .
https://github.com/WordPress/wordpress-playground/issues/419

## Why?

Users want to be able to preview plugins on their own sites before
actually committing to installing them live.

## Testing Instructions

- Checkout the project and copy the plugin to your local WordPress
install:
```bash
git checkout sm-collector-plugin
cp packages/collector/ [PATH_TO_YOUR_WORDPRESS]/wp-content/plugins/
```
- Open your local WordPress install and activate the WordPress
Playground plugin
- Start Playground by clicking on Tools > Sandbox Site in wp-admin
- After the site loads it should have the same content as your site
(files and database)

### Testing Previews
- Install this plugin on a site
- Navigate to `plugins` > `add new`
- Search for "Akismet"
- You should now see a `Preview Now` button next to the `Install Now`
button.
- Click `Preview now`, and you should see Playground boot your site with
the plugin activated.

---------

Co-authored-by: Sean Morris <640101+seanmorris@users.noreply.github.com>
Co-authored-by: Adam Zielinski <adam@adamziel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants