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: docs/src/introduction.md
+38-4Lines changed: 38 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,7 +141,34 @@ Note, that unlike MySQL, there is no automatic migrations facility. Currently, t
141
141
142
142
#### Emulator
143
143
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:
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`:
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:
155
182
156
183
```sh
157
184
DDL_STATEMENTS=$(
@@ -163,6 +190,13 @@ DDL_STATEMENTS=$(
163
190
)
164
191
```
165
192
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`
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>`).
177
211
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:
179
213
180
214
```sh
181
215
SYNC_SYNCSTORAGE__SPANNER_EMULATOR_HOST=localhost:9010 make run_spanner
0 commit comments