Skip to content

Commit dd1680d

Browse files
committed
address review
1 parent 4e7dbaf commit dd1680d

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

cloud_sql/mysql/pdo/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Once the proxy is ready, use one of the following commands to start the proxy in
2121

2222
You may connect to your instance via either unix sockets or TCP. To connect using a socket, you must provide the `-dir` option when starting the proxy. To connect via TCP, you must provide a port as part of the instance name. Both are demonstrated below.
2323

24-
#### Unix Socket mode
24+
### Unix Socket mode
2525

2626
```bash
2727
$ ./cloud_sql_proxy -dir=/cloudsql \
@@ -31,18 +31,19 @@ $ ./cloud_sql_proxy -dir=/cloudsql \
3131

3232
Note: Make sure to run the command under a user with write access in the `/cloudsql` directory. This proxy will use this folder to create a unix socket the application will use to connect to Cloud SQL.
3333

34-
#### TCP mode
34+
### TCP mode
3535

3636
```bash
3737
$ ./cloud_sql_proxy \
3838
--instances=$CLOUD_SQL_CONNECTION_NAME=tcp:3306 \
3939
--credential_file=$GOOGLE_APPLICATION_CREDENTIALS
4040
```
4141

42+
### Set Configuration Values
4243
Set the required environment variables for your connection to Cloud SQL. If you are using TCP mode as described above, do not set the `CLOUD_SQL_CONNECTION_NAME` variable.
4344

4445
```bash
45-
export CLOUD_SQL_CONNECTION_NAME='::DATABASE>'
46+
export CLOUD_SQL_CONNECTION_NAME='::INSTANCE>'
4647
export DB_USER='my-db-user'
4748
export DB_PASS='my-db-pass'
4849
export DB_NAME='my-db-name'

cloud_sql/mysql/pdo/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ runtime: php72
1818
# something like https://cloud.google.com/secret-manager/ to help keep secrets
1919
# secret.
2020
env_variables:
21-
CLOUD_SQL_CONNECTION_NAME: ::DATABASE>
21+
CLOUD_SQL_CONNECTION_NAME: ::INSTANCE>
2222
DB_USER: my-db-user
2323
DB_PASS: my-db-pass
2424
DB_NAME: my-db

cloud_sql/mysql/pdo/src/app.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,56 @@
3535

3636
// Setup the database connection in the container.
3737
$container['db'] = function () {
38-
$username = getenv("DB_USER");
39-
$password = getenv("DB_PASS");
40-
$schema = getenv("DB_NAME");
41-
$hostname = getenv("DB_HOSTNAME") ?: "127.0.0.1";
42-
$cloud_sql_connection_name = getenv("CLOUD_SQL_CONNECTION_NAME");
38+
$username = getenv('DB_USER');
39+
$password = getenv('DB_PASS');
40+
$dbName = getenv('DB_NAME');
41+
$hostname = getenv('DB_HOSTNAME') ?: '127.0.0.1';
42+
$cloud_sql_connection_name = getenv('CLOUD_SQL_CONNECTION_NAME');
4343

4444
try {
4545
// # [START cloud_sql_mysql_pdo_create]
4646
// // $username = 'your_db_user';
4747
// // $password = 'yoursupersecretpassword';
48-
// // $schema = 'your_db_name';
48+
// // $dbName = 'your_db_name';
4949
// // $cloud_sql_connection_name = getenv("CLOUD_SQL_CONNECTION_NAME");
50+
// // $hostname = "127.0.0.1"; // Only used in TCP mode.
5051

5152
if ($cloud_sql_connection_name) {
5253
// Connect using UNIX sockets
5354
$dsn = sprintf(
5455
'mysql:dbname=%s;unix_socket=/cloudsql/%s',
55-
$schema,
56+
$dbName,
5657
$cloud_sql_connection_name
5758
);
5859
} else {
5960
// Connect using TCP
60-
// $hostname = '127.0.0.1';
61-
$dsn = sprintf('mysql:dbname=%s;host=%s', $schema, $hostname);
61+
$dsn = sprintf('mysql:dbname=%s;host=%s', $dbName, $hostname);
6262
}
6363

6464
$conn = new PDO($dsn, $username, $password);
6565
# [END cloud_sql_mysql_pdo_create]
66+
} catch (TypeError $e) {
67+
throw new RuntimeException(
68+
sprintf(
69+
'Invalid or missing configuration! Make sure you have set ' .
70+
'$username, $password, $dbName, and $hostname (for TCP mode) ' .
71+
'or $cloud_sql_connection_name (for UNIX socket mode). ' .
72+
'The PHP error was %s',
73+
$e->getMessage()
74+
),
75+
$e->getCode(),
76+
$e
77+
);
6678
} catch (PDOException $e) {
6779
throw new RuntimeException(
68-
"Could not connect to the Cloud SQL Database. " .
69-
"Refer to https://cloud.google.com/sql/docs/mysql/connect-admin-proxy " .
70-
"for more assistance. The PDO error was " . $e->getMessage(),
80+
sprintf(
81+
'Could not connect to the Cloud SQL Database. Check that ' .
82+
'your username and password are correct, that the Cloud SQL ' .
83+
'proxy is running, and that the database exists and is ready ' .
84+
'for use. For more assistance, refer to %s. The PDO error was %s',
85+
'https://cloud.google.com/sql/docs/mysql/connect-external-app',
86+
$e->getMessage()
87+
),
7188
$e->getCode(),
7289
$e
7390
);

cloud_sql/mysql/pdo/views/template.twig

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,25 @@ limitations under the License.
5959
div>
6060
<h4 class="header center">Recent Votesh4>
6161
<ul class="container collection center">
62-
{% for vote in votes %}
63-
<li class="collection-item avatar">
64-
{% if vote.vote_value == "TABS" %}
65-
<i class="material-icons circle green">keyboard_tabi>
66-
{% elseif vote.vote_value == "SPACES" %}
67-
<i class="material-icons circle blue">space_bari>
68-
{% endif %}
69-
<span class="title">
70-
A vote for <b>{{ vote.vote_value }}b>
71-
span>
72-
<p>was cast at {{ vote.time_cast }}p>
73-
li>
74-
{% endfor %}
62+
{% if votes %}
63+
{% for vote in votes %}
64+
<li class="collection-item avatar">
65+
{% if vote.vote_value == "TABS" %}
66+
<i class="material-icons circle green">keyboard_tabi>
67+
{% elseif vote.vote_value == "SPACES" %}
68+
<i class="material-icons circle blue">space_bari>
69+
{% endif %}
70+
<span class="title">
71+
A vote for <b>{{ vote.vote_value }}b>
72+
span>
73+
<p>was cast at {{ vote.time_cast }}p>
74+
li>
75+
{% endfor %}
76+
{% else %}
77+
<li class="collection-item">
78+
<span class="title">No votes have been cast!span>
79+
li>
80+
{% endif %}
7581
ul>
7682
div>
7783
<script>

0 commit comments

Comments
 (0)