sigma

Cloud SQL Google Cloud SQL

Google Cloud SQL is a service that offers fully-managed SQL DBMS instances for use in cloud and non-cloud applications. It currently supports MySQL and PostgreSQL, and instance capacities ranging up to 64 CPU cores and 400 GB of RAM.

The Cloud SQL model comprises

This model allows multiple databases to be hosted in the same instance, and the same DB to be accessible via multiple users (similar to the model used by engines like MySQL).

Cloud SQL includes dynamic storage expansion (optional), data imports and exports, and advanced features like replication, automatic backups and scheduled maintenance.

Cloud SQL for Operations

From the developer’s point of view, cloud SQL ultimately boils down to SQL. Sigma generates Cloud SQL code using a FaaS-friendly SQL wrapper library slappforge-sdk-gcp. This allows you to easily run SQL queries and transactions within your serverless function against a desired instance, while the library takes care of DB connection management transparently.

Cloud SQL is supported on all platforms.

NOTE:

Prerequisites

  1. Create a Cloud SQL instance.
  2. Create a database under the instance.
  3. Create a user under the instance. and associate him with the database with appropriate privileges. The user should be able to access the database from all hosts (%) since we would be accessing it from cloud functions that do not have fixed hostnames/IP addresses.
  4. Allow connectivity to your Cloud SQL instance from all IP addresses (for the same reason as above).
  5. Assign a public IP to your Cloud SQL instance, so that your client code can connect to it.

NOTE:

Adding a Cloud SQL Operation

  1. Drag a Cloud SQL entry from the GCP Resources pane on to the desired line in the editor.
  2. Switch to the Existing Instance tab if you are not already on it.
  3. Under Cloud SQL Instance, pick your desired instance.
  4. SQL Database field would be populated automatically with databases available in the selected instance. Under it, select the appropriate database.
  5. SQL User field would be populated automatically with users available in the selected instance. Under it, select the appropriate user.
  6. Enter the password for the selected user. This will be stored as a non-persistent environment variable.
  7. Select the appropriate SQL operation (Run Query or Begin Transaction).
  8. For Run Query, enter an appropriate parameterized query under the Query field, and the respective parameter values as a comma-separated list under Inserts.
    • Use quotes ("JohnDoe") to denote literal strings.
    • Use simple variable names and JS snippets (event.id) to pass variables directly into the parameter list.
    • Use the @ notation ("@{person.first_name} @{person.last_name}") to generate composite strings.
  9. Click Inject (or Update).

NOTE:

Available Cloud SQL Operations

Currently Sigma supports the following Cloud SQL operations.

Begin Transaction

Starts a new SQL transaction against the selected instance and database. This does not need any parameter configurations.

The callback function will receive either:

Run Query

Runs a SQL query (read or update) against the selected instance and database

NOTE: If you already have a DB connection object (e.g. one that was obtained from a begin transaction call) you should pass it as the third parameter of the auto-generated code block in order to use it during the operation. If not, a new connection will be created for the selected DB instance. This is particularly important when you want to run queries inside a previously initiated transaction.

Field Required Supports Variables Description
Query :white_check_mark: :white_check_mark: The SQL query to run, can be parameterized if you want to pass external parameter values
Inserts :x: :white_check_mark: Comma-separated list of parameters for the query (of any type, not only for inserts); use variables and literals as described under Adding a Cloud SQL Operation

The callback function will receive (in that order):

NOTE: If you are inside a transaction, make sure to call connection.commit() or connection.rollback() appropriately within the callback.