88use Generator ;
99use Keboola \TableBackendUtils \Column \ColumnCollection ;
1010use Keboola \TableBackendUtils \Column \Teradata \TeradataColumn ;
11+ use Keboola \TableBackendUtils \Escaping \Teradata \TeradataQuote ;
1112use Keboola \TableBackendUtils \Table \Teradata \TeradataTableDefinition ;
1213use Keboola \TableBackendUtils \Table \Teradata \TeradataTableQueryBuilder ;
1314use Keboola \TableBackendUtils \Table \Teradata \TeradataTableReflection ;
@@ -107,12 +108,13 @@ public function testGetDropTableCommand(): void
107108 * @dataProvider createTableTestSqlProvider
108109 */
109110 public function testGetCreateCommand (
110- array $ columns ,
111- array $ primaryKeys ,
112- array $ expectedColumnNames ,
113- array $ expectedPKs ,
111+ array $ columns ,
112+ array $ primaryKeys ,
113+ array $ expectedColumnNames ,
114+ array $ expectedPKs ,
114115 string $ expectedSql
115- ): void {
116+ ): void
117+ {
116118 $ sql = $ this ->qb ->getCreateTableCommand (
117119 $ this ->getDatabaseName (),
118120 self ::TABLE_GENERIC ,
@@ -215,7 +217,7 @@ public function createTableTestSqlProvider(): Generator
215217CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
216218("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
217219"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
218- PRIMARY KEY ("col1"));
220+ CONSTRAINT kbc_pk PRIMARY KEY ("col1"));
219221EOT
220222 ,
221223 ];
@@ -231,7 +233,7 @@ public function createTableTestSqlProvider(): Generator
231233CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
232234("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
233235"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
234- PRIMARY KEY ("col1", "col2"));
236+ CONSTRAINT kbc_pk PRIMARY KEY ("col1", "col2"));
235237EOT
236238 ,
237239 ];
@@ -288,7 +290,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
288290CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
289291("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
290292"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
291- PRIMARY KEY ("col1"));
293+ CONSTRAINT kbc_pk PRIMARY KEY ("col1"));
292294EOT
293295 ,
294296 'createPrimaryKeys ' => true ,
@@ -310,7 +312,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
310312CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
311313("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
312314"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
313- PRIMARY KEY ("col1", "col2"));
315+ CONSTRAINT kbc_pk PRIMARY KEY ("col1", "col2"));
314316EOT
315317 ,
316318 'createPrimaryKeys ' => true ,
@@ -344,9 +346,10 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
344346 */
345347 public function testGetCreateTableCommandFromDefinition (
346348 TeradataTableDefinition $ definition ,
347- string $ expectedSql ,
348- bool $ createPrimaryKeys
349- ): void {
349+ string $ expectedSql ,
350+ bool $ createPrimaryKeys
351+ ): void
352+ {
350353 $ this ->cleanDatabase ($ this ->getDatabaseName ());
351354 $ this ->createDatabase ($ this ->getDatabaseName ());
352355 $ sql = $ this ->qb ->getCreateTableCommandFromDefinition ($ definition , $ createPrimaryKeys );
@@ -366,4 +369,46 @@ public function testGetCreateTableCommandFromDefinition(
366369 self ::assertSame ([], $ tableReflection ->getPrimaryKeysNames ());
367370 }
368371 }
372+
373+ public function testAddAndDropPK ()
374+ {
375+ $ testDb = $ this ->getDatabaseName ();
376+ $ tableName = self ::TABLE_GENERIC ;
377+
378+ // definition for table
379+ $ definition = new TeradataTableDefinition (
380+ $ testDb ,
381+ $ tableName ,
382+ false ,
383+ new ColumnCollection (
384+ [
385+ TeradataColumn::createGenericColumn ('col1 ' ),
386+ TeradataColumn::createGenericColumn ('col2 ' ),
387+ ]
388+ ),
389+ ['col1 ' ]
390+ );
391+
392+ // create table
393+ $ sql = $ this ->qb ->getCreateTableCommandFromDefinition ($ definition , true );
394+ $ this ->connection ->executeQuery ($ sql );
395+
396+ // drop PK - test that PK created in CREATE TABLE can be dropped
397+ $ sql = $ this ->qb ->getDropPrimaryKeyCommand ($ testDb , $ tableName );
398+ $ this ->connection ->executeQuery ($ sql );
399+ $ ref1 = new TeradataTableReflection ($ this ->connection , $ testDb , $ tableName );
400+ $ this ->assertEmpty ($ ref1 ->getPrimaryKeysNames ());
401+
402+ // add PK
403+ $ sql = $ this ->qb ->getAddPrimaryKeyCommand ($ testDb , $ tableName , ['col2 ' ]);
404+ $ this ->connection ->executeQuery ($ sql );
405+ $ ref1 = new TeradataTableReflection ($ this ->connection , $ testDb , $ tableName );
406+ $ this ->assertEquals (['col2 ' ], $ ref1 ->getPrimaryKeysNames ());
407+
408+ // drop again
409+ $ sql = $ this ->qb ->getDropPrimaryKeyCommand ($ testDb , $ tableName );
410+ $ this ->connection ->executeQuery ($ sql );
411+ $ ref1 = new TeradataTableReflection ($ this ->connection , $ testDb , $ tableName );
412+ $ this ->assertEmpty ($ ref1 ->getPrimaryKeysNames ());
413+ }
369414}
0 commit comments