Skip to content

Commit 519cb6c

Browse files
authored
Add samples for CloudSQL postgres (GoogleCloudPlatform#390) and renames mysql to cloudsql-mysql
1 parent af8c0e1 commit 519cb6c

File tree

20 files changed

+2988
-30
lines changed

20 files changed

+2988
-30
lines changed

appengine/flexible/cloudsql/README.md renamed to appengine/flexible/cloudsql-mysql/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Before you can run or deploy the sample, you will need to do the following:
3636

3737
$ mysql -h 127.0.0.1 -u root -p -e "CREATE DATABASE <YOUR_DATABASE_NAME>;"
3838

39-
4039
## Deploy to App Engine
4140

4241
**Prerequisites**
@@ -70,19 +69,19 @@ in your browser.
7069
installed and running.
7170

7271
1. Set the following environment variables with the configuration values for
73-
`USER`, `PASSWORD`, and `DATABASE` you used during setup:
72+
`USER`, `PASSWORD`, `DATABASE`, and `CONNECTION_NAME` you used during setup:
7473

7574
```sh
7675
# set local mysql connection parameters
7776
export MYSQL_DSN="mysql:host=127.0.0.1;port=3306;dbname=DATABASE"
78-
export MYSQL_USERNAME=USER
77+
export MYSQL_USER=USER
7978
export MYSQL_PASSWORD=PASSWORD
8079
```
8180

8281
1. Run the application
8382

8483
```sh
85-
cd php-docs-samples/appengine/standard/cloudsql
84+
cd php-docs-samples/appengine/flexible/cloudsql
8685
php -S localhost:8080
8786
```
8887

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
// create the Silex application
2424
$app = new Application();
2525

26-
$app['pdo'] = function ($app) {
27-
$pdo = new PDO(
28-
$app['mysql.dsn'],
29-
$app['mysql.user'],
30-
$app['mysql.password']
31-
);
32-
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
33-
$pdo->query('CREATE TABLE IF NOT EXISTS visits ' .
34-
'(time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, user_ip CHAR(64))');
35-
return $pdo;
36-
};
26+
// Create the PDO object for CloudSQL MySQL.
27+
$dsn = getenv('MYSQL_DSN');
28+
$user = getenv('MYSQL_USER');
29+
$password = getenv('MYSQL_PASSWORD');
30+
$pdo = new PDO($dsn, $user, $password);
31+
32+
// Create the database if it doesn't exist
33+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
34+
$pdo->query('CREATE TABLE IF NOT EXISTS visits ' .
35+
'(time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, user_ip CHAR(64))');
36+
37+
// Add the PDO object to our Silex application.
38+
$app['pdo'] = $pdo;
3739

3840
$app->get('/', function (Application $app, Request $request) {
3941
$ip = $request->GetClientIp();

appengine/flexible/cloudsql/app.yaml renamed to appengine/flexible/cloudsql-mysql/app.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ env: flex
55
env_variables:
66
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
77
# values obtained when configuring your Cloud SQL instance.
8-
MYSQL_DSN: mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME
98
MYSQL_USER: USER
109
MYSQL_PASSWORD: PASSWORD
10+
MYSQL_DSN: mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME
1111
#[END env]
1212

1313
#[START cloudsql_settings]
1414
# Use the connection name obtained when configuring your Cloud SQL instance.
1515
beta_settings:
1616
cloud_sql_instances: "CONNECTION_NAME"
17-
#[END cloudsql_settings]
17+
#[END cloudsql_settings]
File renamed without changes.
File renamed without changes.

appengine/flexible/cloudsql/index.php renamed to appengine/flexible/cloudsql-mysql/index.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,4 @@
2424
// Run the app!
2525
// use "gcloud app deploy"
2626
$app['debug'] = true;
27-
$app['mysql.dsn'] = getenv('MYSQL_DSN');
28-
$app['mysql.user'] = getenv('MYSQL_USER');
29-
$app['mysql.password'] = getenv('MYSQL_PASSWORD');
3027
$app->run();
File renamed without changes.

appengine/flexible/cloudsql/test/DeployTest.php renamed to appengine/flexible/cloudsql-mysql/test/DeployTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,26 @@ public function testIndex()
3232
$this->assertContains("Last 10 visits:", (string) $resp->getBody());
3333
}
3434

35-
public function beforeDeploy()
35+
public static function beforeDeploy()
3636
{
3737
$tmpDir = FileUtil::cloneDirectoryIntoTmp(__DIR__ . '/..');
3838
self::$gcloudWrapper->setDir($tmpDir);
3939
chdir($tmpDir);
4040

41-
$connectionName = getenv('CLOUDSQL_CONNECTION_NAME');
41+
$connectionName = getenv('CLOUDSQL_CONNECTION_NAME_MYSQL');
4242
$user = getenv('MYSQL_USER');
43+
$database = getenv('MYSQL_DATABASE');
4344
$password = getenv('MYSQL_PASSWORD');
4445

45-
$appYaml = Yaml::parse(file_get_contents('app.yaml'));
46+
$appYamlContents = file_get_contents('app.yaml');
47+
48+
$appYaml = Yaml::parse($appYamlContents);
4649
$appYaml['env_variables']['MYSQL_USER'] = $user;
4750
$appYaml['env_variables']['MYSQL_PASSWORD'] = $password;
4851
$appYaml['beta_settings']['cloud_sql_instances'] = $connectionName;
4952
$appYaml['env_variables']['MYSQL_DSN'] = str_replace(
5053
['DATABASE', 'CONNECTION_NAME'],
51-
['cloud_samples_tests_php', $connectionName],
54+
[$database, $connectionName],
5255
$appYaml['env_variables']['MYSQL_DSN']
5356
);
5457

appengine/flexible/cloudsql/test/LocalTest.php renamed to appengine/flexible/cloudsql-mysql/test/LocalTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ public function setUp()
2929
public function createApplication()
3030
{
3131
$app = require __DIR__ . '/../app.php';
32-
$app['mysql.dsn'] = getenv('MYSQL_DSN');
33-
$app['mysql.user'] = getenv('MYSQL_USER');
34-
$app['mysql.password'] = getenv('MYSQL_PASSWORD');
35-
if ($app['mysql.dsn'] === false ||
36-
$app['mysql.user'] === false ||
37-
$app['mysql.password'] === false) {
32+
if (getenv('MYSQL_DSN') === false ||
33+
getenv('MYSQL_USER') === false ||
34+
getenv('MYSQL_PASSWORD') === false) {
3835
$this->markTestSkipped('set the MYSQL_DSN, MYSQL_USER and MYSQL_PASSWORD environment variables');
3936
}
4037
return $app;
File renamed without changes.

0 commit comments

Comments
 (0)