@@ -601,9 +601,7 @@ export class PostgresStorageAdapter {
601
601
}
602
602
603
603
classExists ( name ) {
604
- return this . _client . one ( `SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)` , [ name ] ) . then ( ( res ) => {
605
- return res . exists ;
606
- } ) ;
604
+ return this . _client . one ( 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)' , [ name ] , a => a . exists ) ;
607
605
}
608
606
609
607
setClassLevelPermissions ( className , CLPs ) {
@@ -655,17 +653,19 @@ export class PostgresStorageAdapter {
655
653
if ( deletedIndexes . length > 0 ) {
656
654
deletePromise = this . dropIndexes ( className , deletedIndexes , conn ) ;
657
655
}
658
- return deletePromise
659
- . then ( ( ) => insertPromise )
660
- . then ( ( ) => this . _ensureSchemaCollectionExists ( ) )
661
- . then ( ( ) => {
662
- const values = [ className , 'schema' , 'indexes' , JSON . stringify ( existingIndexes ) ]
663
- return conn . none ( `UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1 ` , values ) ;
664
- } ) ;
656
+ return conn . task ( t => {
657
+ const values = [ className , 'schema' , 'indexes' , JSON . stringify ( existingIndexes ) ] ;
658
+ return t . batch ( [
659
+ deletePromise ,
660
+ insertPromise ,
661
+ this . _ensureSchemaCollectionExists ( t ) ,
662
+ t . none ( 'UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1' , values )
663
+ ] ) ;
664
+ } ) ;
665
665
}
666
666
667
667
createClass ( className , schema ) {
668
- return this . _client . tx ( t => {
668
+ return this . _client . tx ( 'create-class' , t => {
669
669
const q1 = this . createTable ( className , schema , t ) ;
670
670
const q2 = t . none ( 'INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)' , { className, schema } ) ;
671
671
const q3 = this . setIndexesWithSchemaFormat ( className , schema . indexes , { } , schema . fields , t ) ;
@@ -727,15 +727,17 @@ export class PostgresStorageAdapter {
727
727
} ) ;
728
728
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${ patternsArray . join ( ',' ) } )` ;
729
729
const values = [ className , ...valuesArray ] ;
730
- return this . _ensureSchemaCollectionExists ( conn )
731
- . then ( ( ) => conn . none ( qs , values ) )
732
- . catch ( error => {
733
- if ( error . code === PostgresDuplicateRelationError ) {
734
- // Table already exists, must have been created by a different request. Ignore error.
735
- } else {
736
- throw error ;
737
- }
738
- } ) . then ( ( ) => {
730
+ return conn . task ( t => {
731
+ return this . _ensureSchemaCollectionExists ( t )
732
+ . then ( ( ) => conn . none ( qs , values ) )
733
+ . catch ( error => {
734
+ if ( error . code === PostgresDuplicateRelationError ) {
735
+ // Table already exists, must have been created by a different request. Ignore error.
736
+ } else {
737
+ throw error ;
738
+ } } )
739
+ } )
740
+ . then ( ( ) => {
739
741
return conn . tx ( 'create-relation-tables' , t => {
740
742
const queries = relations . map ( ( fieldName ) => {
741
743
return t . none ( 'CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )' , { joinTable : `_Join:${ fieldName } :${ className } ` } ) ;
@@ -748,7 +750,7 @@ export class PostgresStorageAdapter {
748
750
addFieldIfNotExists ( className , fieldName , type ) {
749
751
// TODO: Must be revised for invalid logic...
750
752
debug ( 'addFieldIfNotExists' , { className, fieldName, type} ) ;
751
- return this . _client . tx ( "addFieldIfNotExists" , t => {
753
+ return this . _client . tx ( 'add-field-if-not-exists' , t => {
752
754
let promise = Promise . resolve ( ) ;
753
755
if ( type . type !== 'Relation' ) {
754
756
promise = t . none ( 'ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>' , {
0 commit comments