Skip to content

Commit

Permalink
Refactor migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Jun 19, 2022
1 parent 5899a8d commit 3122bab
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# next.js
/.next/
/out/
/prisma/schema.prisma
/prisma/

# production
/build
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ See [Running on Railway](https://umami.is/docs/running-on-railway) to get starte

### Requirements

- A server with Node.js 12 or newer
- A database (MySQL or Postgresql)
- A server with Node.js version 12 or newer
- A database. Umami supports [MySQL](https://www.mysql.com/) and [Postgresql](https://www.postgresql.org/) databases.

### Install Yarn (if needed)
### Install Yarn

```
npm install -g yarn
Expand All @@ -33,12 +33,6 @@ cd umami
yarn install
```

### Database

Umami supports [MySQL](https://www.mysql.com/) and [Postgresql](https://www.postgresql.org/).
The database structure will automatically be applied on the first start of Umami.
This will also create a login account with username **admin** and password **umami**.

### Configure umami

Create an `.env` file with the following
Expand All @@ -57,6 +51,15 @@ mysql://username:mypassword@localhost:3306/mydb

The `HASH_SALT` is used to generate unique values for your installation.

### Check database

```bash
yarn check-db
```

The database structure will automatically be applied on the first start of Umami.
This will also create a login account with username **admin** and password **umami**.

### Build the application

```bash
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ const options = {
};

function logQuery(e) {
if (process.env.LOG_QUERY) {
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
}
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
}

let prisma;

if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient(options);
prisma.$on('query', logQuery);
} else {
if (!global.prisma) {
global.prisma = new PrismaClient(options);
global.prisma.$on('query', logQuery);
}

prisma = global.prisma;
}

if (process.env.LOG_QUERY) {
prisma.$on('query', logQuery);
}

export default prisma;
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
"build": "npm-run-all build-tracker build-geo build-db build-app",
"start": "next start",
"start-env": "node -r dotenv/config scripts/start-env.js",
"production": "prisma migrate deploy && node server.js",
"start-server": "node server.js",
"build-app": "next build",
"build-tracker": "rollup -c rollup.tracker.config.js",
"build-db": "npm-run-all copy-db-schema build-db-client",
"build-db": "npm-run-all copy-db-files build-db-client",
"build-lang": "npm-run-all format-lang compile-lang",
"build-geo": "node scripts/build-geo.js",
"build-db-schema": "dotenv prisma introspect",
"build-db-client": "dotenv prisma generate",
"build-mysql-schema": "dotenv prisma db pull -- --schema=./prisma/schema.mysql.prisma",
"build-mysql-client": "dotenv prisma generate -- --schema=./prisma/schema.mysql.prisma",
"build-postgresql-schema": "dotenv prisma db pull -- --schema=./prisma/schema.postgresql.prisma",
"build-postgresql-client": "dotenv prisma generate -- --schema=./prisma/schema.postgresql.prisma",
"copy-db-schema": "node scripts/copy-db-schema.js",
"build-db-schema": "prisma db pull",
"build-db-client": "prisma generate",
"test-db": "node scripts/test-db.js",
"check-db": "prisma migrate status",
"update-db": "prisma migrate deploy",
"copy-db-files": "node scripts/copy-db-files.js",
"generate-lang": "npm-run-all extract-lang merge-lang",
"extract-lang": "formatjs extract \"{pages,components}/**/*.js\" --out-file build/messages.json",
"merge-lang": "node scripts/merge-lang.js",
Expand Down Expand Up @@ -62,6 +61,7 @@
"classnames": "^2.3.1",
"colord": "^2.9.2",
"cors": "^2.8.5",
"cross-spawn": "^7.0.3",
"date-fns": "^2.23.0",
"date-fns-tz": "^1.1.4",
"del": "^6.0.0",
Expand Down
1 change: 0 additions & 1 deletion prisma/mysql/schema.mysql.prisma

This file was deleted.

1 change: 0 additions & 1 deletion prisma/postgresql/schema.postgresql.prisma

This file was deleted.

29 changes: 0 additions & 29 deletions prisma/seed.js

This file was deleted.

16 changes: 16 additions & 0 deletions scripts/check-db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function check() {
await prisma.account.findMany({ limit: 1 });
}

check()
.catch(e => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
13 changes: 3 additions & 10 deletions scripts/copy-db-schema.js → scripts/copy-db-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ if (!databaseType || !['mysql', 'postgresql'].includes(databaseType)) {

console.log(`Database type detected: ${databaseType}`);

const src = path.resolve(__dirname, `../prisma/schema.${databaseType}.prisma`);
const dest = path.resolve(__dirname, '../prisma/schema.prisma');
const src = path.resolve(__dirname, `../db/${databaseType}`);
const dest = path.resolve(__dirname, '../prisma');

fse.copyFileSync(src, dest);
fse.copySync(src, dest);

console.log(`Copied ${src} to ${dest}`);

const srcMigrations = path.resolve(__dirname, `../prisma/${databaseType}/migrations`);
const destMigrations = path.resolve(__dirname, `../prisma/migrations`);

fse.copySync(srcMigrations, destMigrations);

console.log(`Copied ${srcMigrations} to ${destMigrations}`);

0 comments on commit 3122bab

Please sign in to comment.