forked from deptagency/algomart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add drop scripts * Add drop command to docs
- Loading branch information
1 parent
85364ae
commit 8333ccd
Showing
8 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"scripts": { | ||
"drop": "./scripts/drop.mjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env node | ||
|
||
import 'dotenv/config' | ||
import Knex from 'knex' | ||
import { readlineAsync } from './utils.mjs' | ||
|
||
const dbUrl = new URL(process.env.DATABASE_URL) | ||
|
||
const knex = Knex({ | ||
client: 'pg', | ||
connection: `${dbUrl.origin}/postgres`, | ||
searchPath: process.env.DB_SEARCH_PATH, | ||
}) | ||
|
||
async function main() { | ||
console.log( | ||
'This operation will drop your API database. Are you sure you want to proceed?' | ||
) | ||
if ((await readlineAsync('> y/N: ')) !== 'y') { | ||
console.log('Operation canceled.') | ||
process.exit(0) | ||
} | ||
const dbName = dbUrl.pathname.substring(1) | ||
await knex.raw(`DROP DATABASE IF EXISTS "${dbName}";`) | ||
await knex.raw(`CREATE DATABASE "${dbName}";`) | ||
console.log('Done!') | ||
} | ||
|
||
main(process.argv) | ||
.catch((err) => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
.finally(() => { | ||
return knex.destroy() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env node | ||
|
||
import 'dotenv/config' | ||
|
||
import { createInterface } from 'readline' | ||
|
||
/** Prompt individual CLI user input. */ | ||
export function readlineAsync(prompt) { | ||
return new Promise((resolve) => { | ||
const rl = createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}) | ||
rl.question(prompt, (answer) => { | ||
rl.close() | ||
resolve(answer) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"scripts": { | ||
"seed": "./scripts/seed.mjs" | ||
"seed": "./scripts/seed.mjs", | ||
"drop": "./scripts/drop.mjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env node | ||
|
||
import 'dotenv/config' | ||
import Knex from 'knex' | ||
import { readlineAsync } from './utils.mjs' | ||
|
||
const dbUrl = new URL(process.env.DB_CONNECTION_STRING) | ||
|
||
const knex = Knex({ | ||
client: 'pg', | ||
connection: `${dbUrl.origin}/postgres`, | ||
searchPath: process.env.DB_SEARCH_PATH, | ||
}) | ||
|
||
async function main() { | ||
console.log( | ||
'This operation will drop your CMS database. Are you sure you want to proceed?' | ||
) | ||
if ((await readlineAsync('> y/N: ')) !== 'y') { | ||
console.log('Operation canceled.') | ||
process.exit(0) | ||
} | ||
const dbName = dbUrl.pathname.substring(1) | ||
await knex.raw(`DROP DATABASE IF EXISTS "${dbName}";`) | ||
await knex.raw(`CREATE DATABASE "${dbName}";`) | ||
console.log('Done!') | ||
} | ||
|
||
main(process.argv) | ||
.catch((err) => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
.finally(() => { | ||
return knex.destroy() | ||
}) |