@@ -98,24 +98,22 @@ export class StartedPostgreSqlContainer extends AbstractStartedContainer {
9898 *
9999 * If a snapshot already exists under the given/default name, it will be overwritten with the new snapshot.
100100 *
101- * @param opts Optional snapshot configuration options
101+ * @param snapshotName Optional name for the snapshot, defaults to this.snapshotName
102102 * @returns Promise resolving when snapshot is complete
103103 */
104- public async snapshot ( snapshotName ?: string ) : Promise < void > {
105- const name = snapshotName ?? this . snapshotName ;
106-
107- this . snapshotSanityCheck ( name ) ;
104+ public async snapshot ( snapshotName = this . snapshotName ) : Promise < void > {
105+ this . snapshotSanityCheck ( snapshotName ) ;
108106
109107 // Execute the commands to create the snapshot, in order
110108 await this . execCommandsSQL ( [
111109 // Update pg_database to remove the template flag, then drop the database if it exists.
112110 // This is needed because dropping a template database will fail.
113- `UPDATE pg_database SET datistemplate = FALSE WHERE datname = '${ name } '` ,
114- `DROP DATABASE IF EXISTS "${ name } "` ,
111+ `UPDATE pg_database SET datistemplate = FALSE WHERE datname = '${ snapshotName } '` ,
112+ `DROP DATABASE IF EXISTS "${ snapshotName } "` ,
115113 // Create a copy of the database to another database to use as a template now that it was fully migrated
116- `CREATE DATABASE "${ name } " WITH TEMPLATE "${ this . getDatabase ( ) } " OWNER "${ this . getUsername ( ) } "` ,
114+ `CREATE DATABASE "${ snapshotName } " WITH TEMPLATE "${ this . getDatabase ( ) } " OWNER "${ this . getUsername ( ) } "` ,
117115 // Snapshot the template database so we can restore it onto our original database going forward
118- `ALTER DATABASE "${ name } " WITH is_template = TRUE` ,
116+ `ALTER DATABASE "${ snapshotName } " WITH is_template = TRUE` ,
119117 ] ) ;
120118 }
121119
@@ -126,17 +124,15 @@ export class StartedPostgreSqlContainer extends AbstractStartedContainer {
126124 * @param opts Optional snapshot configuration options
127125 * @returns Promise resolving when restore is complete
128126 */
129- public async restore ( snapshotName ?: string ) : Promise < void > {
130- const name = snapshotName ?? this . snapshotName ;
131-
132- this . snapshotSanityCheck ( name ) ;
127+ public async restore ( snapshotName = this . snapshotName ) : Promise < void > {
128+ this . snapshotSanityCheck ( snapshotName ) ;
133129
134130 // Execute the commands to restore the snapshot, in order
135131 await this . execCommandsSQL ( [
136132 // Drop the entire database by connecting to the postgres global database
137133 `DROP DATABASE "${ this . getDatabase ( ) } " WITH (FORCE)` ,
138134 // Then restore the previous snapshot
139- `CREATE DATABASE "${ this . getDatabase ( ) } " WITH TEMPLATE "${ name } " OWNER "${ this . getUsername ( ) } "` ,
135+ `CREATE DATABASE "${ this . getDatabase ( ) } " WITH TEMPLATE "${ snapshotName } " OWNER "${ this . getUsername ( ) } "` ,
140136 ] ) ;
141137 }
142138
0 commit comments