-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for dropping the database #67
Conversation
const execa = require("execa"); | ||
module.exports = {}; | ||
|
||
async function createAppAndStartServer() { | ||
async function init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just keeping the main method calls in sync across scripts.
|
||
fs.writeFile("./tmp/tmpDir", tmpDir, () => { | ||
console.log("tmpDir location stored."); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're storing the tmp directory location so we can pick it up easily if needed.
template/package.json.ejs
Outdated
@@ -11,6 +11,7 @@ | |||
"cypress:local": "CYPRESS_LOCAL=true CYPRESS_BASE_URL=http://localhost:3000 cypress", | |||
"db:migrate": "yarn -s prisma migrate up --experimental", | |||
"db:setup": "yarn db:migrate && yarn prisma generate", | |||
"db:drop": "source prisma/.env && dropdb $(echo $DATABASE_NAME)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dropdb
is a command line helper for postgres. Comes with every install - more info here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about putting this into scripts/dropDatabase
? If we did that we could do:
import { Client } from 'pg';
const connectionString = process.env.DATABASE_URL;
const db = new Client({ connectionString });
const { database } = db.connectionParameters;
async function drop() {
await db.connect();
console.log(`dropping the ${database} database`);
await db.query(`DROP DATABASE IF EXISTS ${database}`);
await db.end();
}
drop();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the big advantage here I think is that we can just use the existing DATABASE_URL
and not have to manage another variable. we should have pg
included as a dependency already.
@cball I wasn't sure if we wanted to use Mocha for unit tests - but went down that route to at least get something running. Let me know if you have a library pref and I can update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome @alvincrespo! It might be good to stick with jest
since that's what we are leveraging once the app is generated. Looking at what you have for Mocha I don't think it will be much of an update.
Along the lines of what you mentioned, I'd be curious if our unit tests need to go through the whole app generation process, but we can circle back to that.
The biggest suggestion I have is around creating a script to drop the db. If we go that route, you'll want something like this:
{
"db:drop": "DOTENV_CONFIG_PATH=./prisma/.env ts-node -r dotenv/config ./scripts/dropdb",
}
.github/workflows/release.yml
Outdated
@@ -31,6 +31,9 @@ jobs: | |||
- name: Install packages | |||
run: | | |||
yarn install --frozen-lockfile | |||
- name: Run Prisma Unit Tests | |||
run: | | |||
yarn test:prisma |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we make this generic unit tests?
package.json
Outdated
@@ -44,6 +45,7 @@ | |||
"@semantic-release/git": "^9.0.0", | |||
"cypress": "^5.1.0", | |||
"ejs-lint": "^1.1.0", | |||
"mocha": "^8.1.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I mentioned in the comment I think we should keep this as jest
for continuity sake. glad you got this in though!
return path.join(tmpdir, name); | ||
} | ||
|
||
init(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
template/prisma/_.env.ejs
Outdated
@@ -5,3 +5,4 @@ | |||
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings | |||
|
|||
DATABASE_URL="postgresql://<%= db.dev.user %><%= db.dev.password %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.dev.name %>?schema=public" | |||
DATABASE_NAME="<%= db.dev.name %>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we go the scripts route, may not need this
@cball Awesome recommendations! This makes sense to me. I'll go ahead and implement. |
@cball Ready for another round of review. |
# [1.7.0-canary.1](v1.6.4...v1.7.0-canary.1) (2020-10-02) ### Features * add support for dropping the database ([#67](#67)) ([b0d8424](b0d8424))
🎉 This PR is included in version 1.7.0-canary.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This PR is included in version 1.7.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This PR ensures that a developer can drop their database from the script
db:drop
Changes
db:drop
to a generated appspackage.json
createApp
script to create the app but NOT start it (to verify setup)DATABASE_NAME
is properly generated inprisma/.env
Release
workflow to include unit teststest:prisma
script for independently testing prisma setupChecklist