mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Refactor migrations.
This commit is contained in:
parent
5899a8d45c
commit
3122bab419
2
.gitignore
vendored
2
.gitignore
vendored
@ -11,7 +11,7 @@
|
|||||||
# next.js
|
# next.js
|
||||||
/.next/
|
/.next/
|
||||||
/out/
|
/out/
|
||||||
/prisma/schema.prisma
|
/prisma/
|
||||||
|
|
||||||
# production
|
# production
|
||||||
/build
|
/build
|
||||||
|
21
README.md
21
README.md
@ -16,10 +16,10 @@ See [Running on Railway](https://umami.is/docs/running-on-railway) to get starte
|
|||||||
|
|
||||||
### Requirements
|
### Requirements
|
||||||
|
|
||||||
- A server with Node.js 12 or newer
|
- A server with Node.js version 12 or newer
|
||||||
- A database (MySQL or Postgresql)
|
- 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
|
npm install -g yarn
|
||||||
@ -33,12 +33,6 @@ cd umami
|
|||||||
yarn install
|
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
|
### Configure umami
|
||||||
|
|
||||||
Create an `.env` file with the following
|
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.
|
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
|
### Build the application
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
10
lib/db.js
10
lib/db.js
@ -11,23 +11,23 @@ const options = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function logQuery(e) {
|
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;
|
let prisma;
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
prisma = new PrismaClient(options);
|
prisma = new PrismaClient(options);
|
||||||
prisma.$on('query', logQuery);
|
|
||||||
} else {
|
} else {
|
||||||
if (!global.prisma) {
|
if (!global.prisma) {
|
||||||
global.prisma = new PrismaClient(options);
|
global.prisma = new PrismaClient(options);
|
||||||
global.prisma.$on('query', logQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prisma = global.prisma;
|
prisma = global.prisma;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.LOG_QUERY) {
|
||||||
|
prisma.$on('query', logQuery);
|
||||||
|
}
|
||||||
|
|
||||||
export default prisma;
|
export default prisma;
|
||||||
|
18
package.json
18
package.json
@ -14,19 +14,18 @@
|
|||||||
"build": "npm-run-all build-tracker build-geo build-db build-app",
|
"build": "npm-run-all build-tracker build-geo build-db build-app",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"start-env": "node -r dotenv/config scripts/start-env.js",
|
"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-app": "next build",
|
||||||
"build-tracker": "rollup -c rollup.tracker.config.js",
|
"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-lang": "npm-run-all format-lang compile-lang",
|
||||||
"build-geo": "node scripts/build-geo.js",
|
"build-geo": "node scripts/build-geo.js",
|
||||||
"build-db-schema": "dotenv prisma introspect",
|
"build-db-schema": "prisma db pull",
|
||||||
"build-db-client": "dotenv prisma generate",
|
"build-db-client": "prisma generate",
|
||||||
"build-mysql-schema": "dotenv prisma db pull -- --schema=./prisma/schema.mysql.prisma",
|
"test-db": "node scripts/test-db.js",
|
||||||
"build-mysql-client": "dotenv prisma generate -- --schema=./prisma/schema.mysql.prisma",
|
"check-db": "prisma migrate status",
|
||||||
"build-postgresql-schema": "dotenv prisma db pull -- --schema=./prisma/schema.postgresql.prisma",
|
"update-db": "prisma migrate deploy",
|
||||||
"build-postgresql-client": "dotenv prisma generate -- --schema=./prisma/schema.postgresql.prisma",
|
"copy-db-files": "node scripts/copy-db-files.js",
|
||||||
"copy-db-schema": "node scripts/copy-db-schema.js",
|
|
||||||
"generate-lang": "npm-run-all extract-lang merge-lang",
|
"generate-lang": "npm-run-all extract-lang merge-lang",
|
||||||
"extract-lang": "formatjs extract \"{pages,components}/**/*.js\" --out-file build/messages.json",
|
"extract-lang": "formatjs extract \"{pages,components}/**/*.js\" --out-file build/messages.json",
|
||||||
"merge-lang": "node scripts/merge-lang.js",
|
"merge-lang": "node scripts/merge-lang.js",
|
||||||
@ -62,6 +61,7 @@
|
|||||||
"classnames": "^2.3.1",
|
"classnames": "^2.3.1",
|
||||||
"colord": "^2.9.2",
|
"colord": "^2.9.2",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
"cross-spawn": "^7.0.3",
|
||||||
"date-fns": "^2.23.0",
|
"date-fns": "^2.23.0",
|
||||||
"date-fns-tz": "^1.1.4",
|
"date-fns-tz": "^1.1.4",
|
||||||
"del": "^6.0.0",
|
"del": "^6.0.0",
|
||||||
|
@ -1 +0,0 @@
|
|||||||
../schema.mysql.prisma
|
|
@ -1 +0,0 @@
|
|||||||
../schema.postgresql.prisma
|
|
@ -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
16
scripts/check-db.js
Normal 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();
|
||||||
|
});
|
@ -22,16 +22,9 @@ if (!databaseType || !['mysql', 'postgresql'].includes(databaseType)) {
|
|||||||
|
|
||||||
console.log(`Database type detected: ${databaseType}`);
|
console.log(`Database type detected: ${databaseType}`);
|
||||||
|
|
||||||
const src = path.resolve(__dirname, `../prisma/schema.${databaseType}.prisma`);
|
const src = path.resolve(__dirname, `../db/${databaseType}`);
|
||||||
const dest = path.resolve(__dirname, '../prisma/schema.prisma');
|
const dest = path.resolve(__dirname, '../prisma');
|
||||||
|
|
||||||
fse.copyFileSync(src, dest);
|
fse.copySync(src, dest);
|
||||||
|
|
||||||
console.log(`Copied ${src} to ${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}`);
|
|
Loading…
x
Reference in New Issue
Block a user