Skip to content

Commit 893a21c

Browse files
authored
Merge branch 'master' into chore/dorny-test-reporter-gh-action-STOR-471
2 parents 84fb7d7 + 854e6c5 commit 893a21c

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/introduction.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,34 @@ Note, that unlike MySQL, there is no automatic migrations facility. Currently, t
141141

142142
#### Emulator
143143

144-
Google supports an in-memory Spanner emulator, which can run on your local machine for development purposes. You can install the emulator via the gcloud CLI or Docker by following the instructions [here](https://cloud.google.com/spanner/docs/emulator#installing_and_running_the_emulator). Once the emulator is running, you'll need to create a new instance and a new database. To create an instance using the REST API (exposed via port 9020 on the emulator), we can use `curl`:
144+
Google supports an in-memory Spanner emulator, which can run on your local machine for development purposes. You can install the emulator via the gcloud CLI or Docker by following the instructions [here](https://cloud.google.com/spanner/docs/emulator#installing_and_running_the_emulator). Once the emulator is running, you'll need to create a new instance and a new database.
145+
146+
##### Quick Setup Using prepare-spanner.sh
147+
148+
The easiest way to set up a Spanner emulator database is to use the `prepare-spanner.sh` script:
149+
150+
```sh
151+
SYNC_SYNCSTORAGE__SPANNER_EMULATOR_HOST=localhost:9020 ./scripts/prepare-spanner.sh
152+
```
153+
154+
This script will automatically:
155+
1. Create a test instance (`test-instance`) on a test project (`test-project`)
156+
2. Create a test database (`test-database`) with the schema from `schema.ddl`
157+
3. Apply all DDL statements to set up the database structure
158+
159+
The script looks for `schema.ddl` in either the current directory or in `syncstorage-spanner/src/`. Make sure the `SYNC_SYNCSTORAGE__SPANNER_EMULATOR_HOST` environment variable points to your emulator's REST API endpoint (typically `localhost:9020`).
160+
161+
After running the script, make sure that the `database_url` config variable in your `local.toml` file reflects the created database (i.e. `spanner://projects/test-project/instances/test-instance/databases/test-database`).
162+
163+
To run an application server that points to the local Spanner emulator:
164+
165+
```sh
166+
SYNC_SYNCSTORAGE__SPANNER_EMULATOR_HOST=localhost:9010 make run_spanner
167+
```
168+
169+
##### Manual Setup Using curl
170+
171+
If you prefer to manually create the instance and database, or need custom project/instance/database names, you can use the REST API directly. The Spanner emulator exposes a REST API on port 9020. To create an instance, use `curl`:
145172

146173
```sh
147174
curl --request POST \
@@ -151,7 +178,7 @@ curl --request POST \
151178
--data "{\"instance\":{\"config\":\"emulator-test-config\",\"nodeCount\":1,\"displayName\":\"Test Instance\"},\"instanceId\":\"$INSTANCE_ID\"}"
152179
```
153180

154-
Note that you may set `PROJECT_ID` and `INSTANCE_ID` to your liking. To create a new database on this instance, we'll use a similar HTTP request, but we'll need to include information about the database schema. Since we don't have migrations for Spanner, we keep an up-to-date schema in `src/db/spanner/schema.ddl`. The `jq` utility allows us to parse this file for use in the JSON body of an HTTP POST request:
181+
Note that you may set `PROJECT_ID` and `INSTANCE_ID` to your liking. To create a new database on this instance, you'll need to include information about the database schema. Since we don't have migrations for Spanner, we keep an up-to-date schema in `src/db/spanner/schema.ddl`. The `jq` utility allows us to parse this file for use in the JSON body of an HTTP POST request:
155182

156183
```sh
157184
DDL_STATEMENTS=$(
@@ -163,6 +190,13 @@ DDL_STATEMENTS=$(
163190
)
164191
```
165192

193+
This command:
194+
- Filters out SQL comments (lines starting with `--`)
195+
- Normalizes whitespace
196+
- Removes newlines to create a single line
197+
- Removes the trailing semicolon from the concatenated string
198+
- Splits the DDL statements back into an array using `jq`
199+
166200
Finally, to create the database:
167201

168202
```sh
@@ -173,9 +207,9 @@ curl -sS --request POST \
173207
--data "{\"createStatement\":\"CREATE DATABASE \`$DATABASE_ID\`\",\"extraStatements\":$DDL_STATEMENTS}"
174208
```
175209

176-
Note that, again, you may set `DATABASE_ID` to your liking. Make sure that the `database_url` config variable reflects your choice of project name, instance name, and database name (i.e. it should be of the format `spanner://projects/<your project ID here>/instances/<your instance ID here>/databases/<your database ID here>`).
210+
Note that, again, you may set `DATABASE_ID` to your liking. Make sure that the `database_url` config variable in your `local.toml` file reflects your choice of project name, instance name, and database name (i.e. it should be of the format `spanner://projects/<your project ID here>/instances/<your instance ID here>/databases/<your database ID here>`).
177211

178-
To run an application server that points to the local Spanner emulator:
212+
To run the application server that points to the local Spanner emulator:
179213

180214
```sh
181215
SYNC_SYNCSTORAGE__SPANNER_EMULATOR_HOST=localhost:9010 make run_spanner

0 commit comments

Comments
 (0)