-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for dropping the database (#67)
- Loading branch information
1 parent
11428a2
commit b0d8424
Showing
10 changed files
with
1,820 additions
and
38 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,6 @@ cypress/screenshots* | |
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
yarn-error.log* | ||
|
||
tmp |
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 |
---|---|---|
|
@@ -8,3 +8,4 @@ describe("Creating a new app", () => { | |
}); | ||
}); | ||
}); | ||
|
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,49 @@ | ||
const { promisify } = require("util"); | ||
const fs = require("fs"); | ||
const os = require("os"); | ||
const path = require("path"); | ||
const mkdtemp = promisify(fs.mkdtemp); | ||
const execa = require("execa"); | ||
module.exports = {}; | ||
|
||
async function init() { | ||
const args = process.argv.slice(2); | ||
return await createApp(args); | ||
} | ||
|
||
async function createTmpDir() { | ||
const tmpDir = await mkdtemp(`${os.tmpdir()}${path.sep}`); | ||
|
||
if (!fs.existsSync("./tmp")) { | ||
await fs.promises.mkdir("./tmp"); | ||
} | ||
|
||
fs.writeFile("./tmp/tmpDir", tmpDir, () => { | ||
console.log("tmpDir location stored."); | ||
}); | ||
|
||
return tmpDir; | ||
} | ||
|
||
async function createApp(args) { | ||
console.log("my args are", args); | ||
const tmpdir = await createTmpDir(); | ||
const cliPath = path.join(__dirname, "..", "cli.js"); | ||
|
||
const cliOptions = args.length ? args : ["myapp", "--acceptDefaults"]; | ||
const name = cliOptions.splice(0, 1)[0]; | ||
|
||
console.log("cli options are", cliOptions); | ||
console.log("cli path", cliPath); | ||
console.log("installing to:", tmpdir); | ||
console.log("nodeargs", [cliPath, name, ...cliOptions]); | ||
|
||
await execa("node", [cliPath, name, ...cliOptions], { | ||
cwd: tmpdir, | ||
stdio: "inherit", | ||
}); | ||
|
||
return path.join(tmpdir, name); | ||
} | ||
|
||
init(); |
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,24 @@ | ||
export {}; | ||
|
||
const url = require('url'); | ||
const Client = require('pg').Client; | ||
|
||
const connectionString = process.env.DATABASE_URL; | ||
const { auth: user, hostname: host, port, path } = url.parse(connectionString); | ||
const db = new Client({ user, host, port }); | ||
const dbName = path.slice(1).substring(0, path.slice(1).lastIndexOf('?')); | ||
|
||
async function drop() { | ||
await db.connect(); | ||
console.log(`dropping the ${dbName} database`); | ||
|
||
try { | ||
await db.query(`DROP DATABASE "${dbName}"`); | ||
} catch (error) { | ||
console.log({ error }); | ||
} | ||
|
||
await db.end(); | ||
} | ||
|
||
drop(); |
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,28 @@ | ||
const { promisify } = require("util"); | ||
const fs = require("fs"); | ||
|
||
async function getTmpDir() { | ||
const readFile = promisify(fs.readFile); | ||
|
||
const tmpDir = await readFile("./tmp/tmpDir"); | ||
|
||
return tmpDir; | ||
} | ||
|
||
beforeEach(async () => { | ||
const path = await getTmpDir(); | ||
|
||
require("dotenv").config({ | ||
path: `${path}/foo/prisma/.env`, | ||
}); | ||
}); | ||
|
||
describe("Prisma", () => { | ||
describe("Environment Variables", () => { | ||
it("correctly defines a DATABASE_URL env var", () => { | ||
expect(process.env.DATABASE_URL).toBe( | ||
"postgresql://postgres@localhost:5432/foo_dev?schema=public" | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.