question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem

Sometimes the database just has to go, to be recreated from nothing. Would be nice if Prisma CLI would help me with that.

Suggested solution

prisma db drop or similar. Probably with a lot of checks that you are dropping the correct one and really, really understand what you are doing.

(On the other hand, normal credentials should not be able to do this anyway, but who knows… better safe than sorry.)

Alternatives

  • Use another tool
  • Maybe $executeRaw?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Jolg42commented, Nov 5, 2021

@jagged3dge Why the first one doesn’t work is actually documented here: https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#using-variables

You cannot pass a table or column name into a tagged template placeholder.

1reaction
jagged3dgecommented, Nov 4, 2021

@Jolg42 Yes, sir. This is defnitely defined. This is part of a startup script for Jest testing-rig.

For completeness, here’s the script in itself:

// .env
# DATABASE_URL="mysql://lunr:lunr@localhost:3306/lunr-db"
DATABASE_URL="mysql://lunr-testr:lunr@localhost:3306/lunr-test"
// tests/env.ts
import { ProjectConfig } from '@jest/types/build/Config';
import { PrismaClient } from '@prisma/client';
import NodeEnvironment from 'jest-environment-node';
import { promisify } from 'util';
import { exec } from 'child_process';

const execAsync = promisify(exec);

export class PrismaTestEnvironment extends NodeEnvironment {
  dbName = 'lunr-test';
  dbURL: string;
  client: PrismaClient;

  constructor(config: ProjectConfig) {
    super(config);

    this.client = new PrismaClient({
      log: ['query'],
    });
  }

  async setup() {
    // Drop the database before the tests start
    await this.client.$executeRaw`DROP DATABASE IF EXISTS ${this.dbName};`;

    // Re-create the database and seed it
    await execAsync(
      'yarn prisma db push --skip-generate && yarn prisma db seed'
    );

    await super.setup();
  }

  async teardown() {
    await this.client.$disconnect();
  }
}

module.exports = PrismaTestEnvironment;

For now, I’ve manually changed the DATABASE_URL in the project’s .env file to match the dbURL as in the above code. So, the shell command has access to the same DATABASE_URL value to push the prisma schema and run seeds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL DROP DATABASE Statement - W3Schools
The DROP DATABASE statement is used to drop an existing SQL database. Syntax. DROP DATABASE databasename;. Note: Be careful before dropping a database....
Read more >
SQL - DROP or DELETE Database - Tutorialspoint
The SQL DROP DATABASE statement is used to drop an existing database in SQL schema. Syntax. The basic syntax of DROP DATABASE statement...
Read more >
MySQL 8.0 Reference Manual :: 13.1.24 DROP DATABASE ...
DROP DATABASE drops all tables in the database and deletes the database. Be very careful with this statement! To use DROP DATABASE ,...
Read more >
DROP DATABASE (Transact-SQL) - Microsoft Learn
Dropping a database deletes the database from an instance of SQL Server and deletes the physical disk files used by the database. If...
Read more >
MySQL DROP DATABASE - How to Delete a Database in ...
DROP DATABASE using MySQL Workbench ... First, launch the MySQL workbench and log in to the MySQL Server. Second, right-click the database that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found