Refactor migrations.

This commit is contained in:
Mike Cao 2022-06-19 00:07:01 -07:00
parent 5899a8d45c
commit 3122bab419
15 changed files with 46 additions and 65 deletions

2
.gitignore vendored
View File

@ -11,7 +11,7 @@
# next.js
/.next/
/out/
/prisma/schema.prisma
/prisma/
# production
/build

View File

@ -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
@ -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
@ -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

View File

@ -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;

View File

@ -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",
@ -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",

View File

@ -1 +0,0 @@
../schema.mysql.prisma

View File

@ -1 +0,0 @@
../schema.postgresql.prisma

View File

@ -1,29 +0,0 @@
const bcrypt = require('bcryptjs');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const SALT_ROUNDS = 10;
const hashPassword = password => {
return bcrypt.hashSync(password, SALT_ROUNDS);
};
async function main() {
await prisma.account.upsert({
where: { username: 'admin' },
update: {},
create: {
username: 'admin',
password: hashPassword(process.env.ADMIN_PASSWORD || 'umami'),
is_admin: true,
},
});
}
main()
.catch(e => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});

16
scripts/check-db.js Normal file
View File

@ -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();
});

View File

@ -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}`);