Skip to content

Commit

Permalink
Add drop scripts (deptagency#299)
Browse files Browse the repository at this point in the history
* Add drop scripts

* Add drop command to docs
  • Loading branch information
davidjmurphyjr authored Mar 1, 2022
1 parent 85364ae commit 8333ccd
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ To run eslint for all projects:
npm run lint
```
To initialize the databases:
```bash
nx drop api &&\
nx run api:migrate:latest &&\
nx drop cms &&\
nx bootstrap cms &&\
nx import cms
```

### Running with docker-compose

The `docker-compose` [configuration](./docker-compose.yml) includes service definitions for the API service,
Expand Down
7 changes: 7 additions & 0 deletions apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ Finally, apply migrations:
nx run api:migrate:latest
```

To initialize the API database:

```bash
nx drop api &&\
nx run api:migrate:latest
```

Per the `.env`, you'll also need to be connected to an Algorand node, whether in development or production.

### Creating a local Algorand Sandbox account
Expand Down
5 changes: 5 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"drop": "./scripts/drop.mjs"
}
}
36 changes: 36 additions & 0 deletions apps/api/scripts/drop.mjs
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()
})
19 changes: 19 additions & 0 deletions apps/api/scripts/utils.mjs
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)
})
})
}
8 changes: 8 additions & 0 deletions apps/cms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ npm run seed

> TODO: setup seed Nx executor
To initialize the CMS database:

```bash
nx drop cms &&\
nx bootstrap cms &&\
nx import cms
```

Once the database is set up, it can be run in conjunction with the other monorepo packages from the root of the repository.

## Make files publicly viewable
Expand Down
3 changes: 2 additions & 1 deletion apps/cms/package.json
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"
}
}
36 changes: 36 additions & 0 deletions apps/cms/scripts/drop.mjs
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()
})

0 comments on commit 8333ccd

Please sign in to comment.