Skip to content

Commit 75d80e3

Browse files
kurtisvgbshaffer
authored andcommitted
Refactors and adds connect region tags to Cloud SQL sample. (GoogleCloudPlatform#965)
1 parent 4d0fd4e commit 75d80e3

File tree

6 files changed

+36
-52
lines changed

6 files changed

+36
-52
lines changed

cloud_sql/postgres/pdo/README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export CLOUD_SQL_CONNECTION_NAME='::'
1414
export DB_USER='my-db-user'
1515
export DB_PASS='my-db-pass'
1616
export DB_NAME='my-db-name'
17-
export DB_HOSTNAME='localhost' # If connecting using cloud_sql_proxy
17+
export DB_HOSTNAME='localhost' # If connecting using TCP instead of Unix Sockets
1818
```
1919

2020
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe.
@@ -31,21 +31,10 @@ $ ./cloud_sql_proxy -dir=/cloudsql --instances=$CLOUD_SQL_CONNECTION_NAME --cred
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-
Next, install the dependencies using Composer:
35-
36-
```bash
37-
$ composer install
38-
```
39-
OR
40-
41-
```bash
42-
$ php composer.phar install
43-
```
44-
4534
Execute the following:
4635

4736
```bash
48-
$ php -S localhost:8080 index.php
37+
$ php -S localhost:8080
4938
```
5039

5140
Navigate towards http://localhost:8080 to verify your application is running correctly.

cloud_sql/postgres/pdo/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ runtime: php72
1717
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
1818
# something like https://cloud.google.com/kms/ to help keep secrets secret.
1919
env_variables:
20-
CLOUD_SQL_INSTANCE_NAME: ::
20+
CLOUD_SQL_CONNECTION_NAME: ::
2121
DB_USER: my-db-user
2222
DB_PASS: my-db-pass
2323
DB_NAME: my_db

cloud_sql/postgres/pdo/composer.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

cloud_sql/postgres/pdo/index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
require 'vendor/autoload.php';
16+
require_once 'src/DB.php';
17+
require_once 'src/Votes.php';
1718

1819
use Google\Cloud\Samples\CloudSQL\Postgres\DB;
1920
use Google\Cloud\Samples\CloudSQL\Postgres\Votes;
2021

21-
$votes = new Votes(new DB());
22+
$votes = new Votes(DB::createPdoConnection());
2223

2324
if ($_SERVER['REQUEST_URI'] == '/' && $_SERVER['REQUEST_METHOD'] == 'GET') {
2425
$list = $votes->list();

cloud_sql/postgres/pdo/src/DB.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,33 @@
1919

2020
class DB
2121
{
22-
private $connection;
23-
24-
public function __construct()
25-
{
26-
$config = [
27-
"username" => getenv("DB_USER"),
28-
"password" => getenv("DB_PASS"),
29-
"schema" => getenv("DB_NAME"),
30-
"hostname" => getenv("DB_HOSTNAME") ?: "127.0.0.1",
31-
"cloud_sql_instance_name" => getenv("CLOUD_SQL_INSTANCE_NAME")
32-
];
33-
34-
$this->connection = $this->connect($config);
35-
}
36-
37-
private function connect($config)
22+
public static function createPdoConnection()
3823
{
39-
$dsn = "pgsql:dbname={$config['schema']};host={$config['hostname']}";
40-
41-
if ($config["cloud_sql_instance_name"] != "") {
42-
$dsn = "pgsql:dbname={$config['schema']};host=/cloudsql/{$config['cloud_sql_instance_name']}";
24+
$username = getenv("DB_USER");
25+
$password = getenv("DB_PASS");
26+
$schema = getenv("DB_NAME");
27+
$hostname = getenv("DB_HOSTNAME") ?: "127.0.0.1";
28+
$cloud_sql_connection_name = getenv("CLOUD_SQL_CONNECTION_NAME");
29+
# [START cloud_sql_postgres_pdo_create]
30+
// $username = 'your_db_user';
31+
// $password = 'yoursupersecretpassword';
32+
// $schema = 'your_db_name';
33+
// $cloud_sql_connection_name = 'Your Cloud SQL Connection name';
34+
35+
if ($cloud_sql_connection_name) {
36+
// Connect using UNIX sockets
37+
$dsn = sprintf(
38+
'pgsql:dbname=%s;host=/cloudsql/%s',
39+
$schema,
40+
$cloud_sql_connection_name
41+
);
42+
} else {
43+
// Connect using TCP
44+
// $hostname = '127.0.0.1';
45+
$dsn = sprintf('pgsql:dbname=%s;host=%s', $schema, $hostname);
4346
}
4447

45-
return new PDO($dsn, $config['username'], $config['password']);
46-
}
47-
48-
public function get_connection()
49-
{
50-
return $this->connection;
48+
return new PDO($dsn, $username, $password);
49+
# [END cloud_sql_postgres_pdo_create]
5150
}
5251
}

cloud_sql/postgres/pdo/src/Votes.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
namespace Google\Cloud\Samples\CloudSQL\Postgres;
1717

18+
use PDO;
19+
1820
class Votes
1921
{
2022
private $connection;
2123

22-
public function __construct(DB $db)
24+
public function __construct(PDO $connection)
2325
{
24-
$this->connection = $db->get_connection();
26+
$this->connection = $connection;
2527
$this->create_table();
2628
}
2729

@@ -42,7 +44,7 @@ public function list()
4244
$sql = "SELECT candidate, time_cast FROM votes ORDER BY time_cast DESC LIMIT 5";
4345
$statement = $this->connection->prepare($sql);
4446
$statement->execute();
45-
47+
4648
return $statement->fetchAll();
4749
}
4850

0 commit comments

Comments
 (0)