@@ -608,9 +608,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
608
608
}
609
609
610
610
classExists ( name : string ) {
611
- return this . _client . one ( `SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)` , [ name ] ) . then ( ( res ) => {
612
- return res . exists ;
613
- } ) ;
611
+ return this . _client . one ( 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)' , [ name ] , a => a . exists ) ;
614
612
}
615
613
616
614
setClassLevelPermissions ( className : string , CLPs : any ) {
@@ -662,17 +660,19 @@ export class PostgresStorageAdapter implements StorageAdapter {
662
660
if ( deletedIndexes . length > 0 ) {
663
661
deletePromise = this . dropIndexes ( className , deletedIndexes , conn ) ;
664
662
}
665
- return deletePromise
666
- . then ( ( ) => insertPromise )
667
- . then ( ( ) => this . _ensureSchemaCollectionExists ( ) )
668
- . then ( ( ) => {
669
- const values = [ className , 'schema' , 'indexes' , JSON . stringify ( existingIndexes ) ]
670
- return conn . none ( `UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1 ` , values ) ;
671
- } ) ;
663
+ return conn . task ( t => {
664
+ const values = [ className , 'schema' , 'indexes' , JSON . stringify ( existingIndexes ) ] ;
665
+ return t . batch ( [
666
+ deletePromise ,
667
+ insertPromise ,
668
+ this . _ensureSchemaCollectionExists ( t ) ,
669
+ t . none ( 'UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1' , values )
670
+ ] ) ;
671
+ } ) ;
672
672
}
673
673
674
674
createClass ( className : string , schema : SchemaType ) {
675
- return this . _client . tx ( t => {
675
+ return this . _client . tx ( 'create-class' , t => {
676
676
const q1 = this . createTable ( className , schema , t ) ;
677
677
const q2 = t . none ( 'INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)' , { className, schema } ) ;
678
678
const q3 = this . setIndexesWithSchemaFormat ( className , schema . indexes , { } , schema . fields , t ) ;
@@ -734,15 +734,17 @@ export class PostgresStorageAdapter implements StorageAdapter {
734
734
} ) ;
735
735
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${ patternsArray . join ( ',' ) } )` ;
736
736
const values = [ className , ...valuesArray ] ;
737
- return this . _ensureSchemaCollectionExists ( conn )
738
- . then ( ( ) => conn . none ( qs , values ) )
739
- . catch ( error => {
740
- if ( error . code === PostgresDuplicateRelationError ) {
741
- // Table already exists, must have been created by a different request. Ignore error.
742
- } else {
743
- throw error ;
744
- }
745
- } ) . then ( ( ) => {
737
+ return conn . task ( t => {
738
+ return this . _ensureSchemaCollectionExists ( t )
739
+ . then ( ( ) => conn . none ( qs , values ) )
740
+ . catch ( error => {
741
+ if ( error . code === PostgresDuplicateRelationError ) {
742
+ // Table already exists, must have been created by a different request. Ignore error.
743
+ } else {
744
+ throw error ;
745
+ } } )
746
+ } )
747
+ . then ( ( ) => {
746
748
return conn . tx ( 'create-relation-tables' , t => {
747
749
const queries = relations . map ( ( fieldName ) => {
748
750
return t . none ( 'CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )' , { joinTable : `_Join:${ fieldName } :${ className } ` } ) ;
@@ -755,7 +757,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
755
757
addFieldIfNotExists ( className : string , fieldName : string , type : any ) {
756
758
// TODO: Must be revised for invalid logic...
757
759
debug ( 'addFieldIfNotExists' , { className, fieldName, type} ) ;
758
- return this . _client . tx ( "addFieldIfNotExists" , t => {
760
+ return this . _client . tx ( 'add-field-if-not-exists' , t => {
759
761
let promise = Promise . resolve ( ) ;
760
762
if ( type . type !== 'Relation' ) {
761
763
promise = t . none ( 'ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>' , {
0 commit comments