Skip to content

Commit 5a45fb5

Browse files
committed
test(spanner): add IF NOT EXISTS to PostgreSQL DDL statements
1 parent ed0b1e6 commit 5a45fb5

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

Spanner/tests/System/PgBatchTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,15 @@ public static function setUpTestFixtures(): void
5353
self::$tableName = uniqid(self::TESTING_PREFIX);
5454

5555
self::$database->updateDdl(sprintf(
56-
'CREATE TABLE %s (
56+
'CREATE TABLE IF NOT EXISTS %s (
5757
id INTEGER PRIMARY KEY,
5858
decade INTEGER NOT NULL
5959
)',
6060
self::$tableName
6161
))->pollUntilComplete();
6262

6363
if (self::$database->info()['databaseDialect'] == DatabaseDialect::POSTGRESQL) {
64-
$statements = [
65-
sprintf('CREATE ROLE %s', self::$dbRole),
66-
sprintf('CREATE ROLE %s', self::$restrictiveDbRole),
67-
];
64+
$statements = [];
6865

6966
if (!self::isEmulatorUsed()) {
7067
$statements[] = sprintf(
@@ -119,6 +116,9 @@ public function testBatchWithDbRole($dbRole, $expected)
119116
try {
120117
$partitions = $snapshot->partitionQuery($query, ['parameters' => $parameters]);
121118
} catch (ServiceException $e) {
119+
if (is_null($expected)) {
120+
throw $e;
121+
}
122122
$error = $e;
123123
}
124124

Spanner/tests/System/PgBatchWriteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public static function setUpTestFixtures(): void
4040
self::setUpTestDatabase();
4141

4242
self::$database->updateDdlBatch([
43-
'CREATE TABLE Singers (
43+
'CREATE TABLE IF NOT EXISTS Singers (
4444
singerid bigint NOT NULL,
4545
firstname varchar(1024),
4646
lastname varchar(1024),
4747
PRIMARY KEY (singerid)
4848
)',
49-
'CREATE TABLE Albums (
49+
'CREATE TABLE IF NOT EXISTS Albums (
5050
singerid bigint NOT NULL,
5151
albumid bigint NOT NULL,
5252
albumtitle varchar(1024),

Spanner/tests/System/PgQueryTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Google\Cloud\Spanner\Database;
2626
use Google\Cloud\Spanner\Date;
2727
use Google\Cloud\Spanner\Interval;
28+
use Google\Cloud\Spanner\KeySet;
2829
use Google\Cloud\Spanner\PgJsonb;
2930
use Google\Cloud\Spanner\PgNumeric;
3031
use Google\Cloud\Spanner\Timestamp;
@@ -52,7 +53,7 @@ public static function setUpTestFixtures(): void
5253
self::setUpTestDatabase();
5354

5455
self::$database->updateDdl(
55-
'CREATE TABLE ' . self::TABLE_NAME . ' (
56+
'CREATE TABLE IF NOT EXISTS ' . self::TABLE_NAME . ' (
5657
id bigint NOT NULL,
5758
name varchar(1024),
5859
registered bool,
@@ -69,6 +70,8 @@ public static function setUpTestFixtures(): void
6970

7071
self::$timestampVal = new Timestamp(new \DateTime());
7172

73+
self::$database->delete(self::TABLE_NAME, new KeySet(['all' => true]));
74+
7275
self::$database->insertOrUpdateBatch(self::TABLE_NAME, [
7376
[
7477
'id' => 1,

Spanner/tests/System/PgWriteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function setUpTestFixtures(): void
5454
self::setUpTestDatabase();
5555

5656
self::$database->updateDdlBatch([
57-
'CREATE TABLE ' . self::TABLE_NAME . ' (
57+
'CREATE TABLE IF NOT EXISTS ' . self::TABLE_NAME . ' (
5858
id bigint NOT NULL,
5959
boolfield boolean,
6060
bytesfield bytea,
@@ -78,7 +78,7 @@ public static function setUpTestFixtures(): void
7878
arraypgjsonbfield jsonb[],
7979
PRIMARY KEY (id)
8080
)',
81-
'CREATE TABLE ' . self::COMMIT_TIMESTAMP_TABLE_NAME . ' (
81+
'CREATE TABLE IF NOT EXISTS ' . self::COMMIT_TIMESTAMP_TABLE_NAME . ' (
8282
id bigint NOT NULL,
8383
commitTimestamp SPANNER.COMMIT_TIMESTAMP NOT NULL,
8484
PRIMARY KEY (id, commitTimestamp)

0 commit comments

Comments
 (0)