Skip to content

Commit

Permalink
merge if not exist
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
  • Loading branch information
wzrdtales committed Sep 10, 2023
2 parents c3588aa + e0d3b34 commit 71d4e44
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,38 @@ var PgDriver = Base.extend({
createDatabase: function (dbName, options, callback) {
var spec = '';

if (typeof options === 'function') callback = options;
if (typeof options === 'function') {
callback = options;
options = {};
}

this.runSql(
util.format('CREATE DATABASE %s %s', this.escapeDDL(dbName), spec),
callback
);
if (options.ifNotExists) {
this.runSql(
`
DO
$do$
DECLARE
_db TEXT := '${ dbName }';

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected space(s) after '${'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected space(s) before '}'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected space(s) after '${'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected space(s) before '}'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space(s) after '${'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space(s) before '}'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (latest)

Unexpected space(s) after '${'

Check failure on line 103 in index.js

View workflow job for this annotation

GitHub Actions / build (latest)

Unexpected space(s) before '}'
BEGIN
CREATE EXTENSION IF NOT EXISTS dblink;
IF EXISTS (SELECT 1 FROM pg_database WHERE datname = _db) THEN
RAISE NOTICE 'Database "%" already exists, skipping creation.', _db;
ELSE
PERFORM dblink_connect('dbname=' || current_database());
PERFORM dblink_exec('CREATE DATABASE ' || _db || ' ${ spec }');

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected space(s) after '${'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected space(s) before '}'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected space(s) after '${'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected space(s) before '}'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space(s) after '${'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space(s) before '}'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (latest)

Unexpected space(s) after '${'

Check failure on line 110 in index.js

View workflow job for this annotation

GitHub Actions / build (latest)

Unexpected space(s) before '}'
END IF;
END
$do$
`,
callback
);
}

Check failure on line 117 in index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Closing curly brace does not appear on the same line as the subsequent block

Check failure on line 117 in index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Closing curly brace does not appear on the same line as the subsequent block

Check failure on line 117 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Closing curly brace does not appear on the same line as the subsequent block

Check failure on line 117 in index.js

View workflow job for this annotation

GitHub Actions / build (latest)

Closing curly brace does not appear on the same line as the subsequent block
else {
this.runSql(
util.format('CREATE DATABASE %s %s', this.escapeDDL(dbName), spec),
callback
);
}
},

dropDatabase: function (dbName, options, callback) {
Expand Down

0 comments on commit 71d4e44

Please sign in to comment.