Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Refactor migrations.

Mike Cao3122bab419ed
diff --git a/.gitignore b/.gitignore
index e6b354414..6414cc5fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,7 +11,7 @@
# next.js
/.next/
/out/
-/prisma/schema.prisma
+/prisma/
# production
/build
diff --git a/README.md b/README.md
index 38958133d..3a14a010c 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/prisma/mysql/migrations/20210320112658_init/migration.sql b/db/mysql/migrations/20210320112658_init/migration.sql
similarity index 100%
rename from prisma/mysql/migrations/20210320112658_init/migration.sql
rename to db/mysql/migrations/20210320112658_init/migration.sql
diff --git a/prisma/mysql/migrations/migration_lock.toml b/db/mysql/migrations/migration_lock.toml
similarity index 100%
rename from prisma/mysql/migrations/migration_lock.toml
rename to db/mysql/migrations/migration_lock.toml
diff --git a/prisma/schema.mysql.prisma b/db/mysql/schema.prisma
similarity index 100%
rename from prisma/schema.mysql.prisma
rename to db/mysql/schema.prisma
diff --git a/prisma/postgresql/migrations/20210320112717_init/migration.sql b/db/postgresql/migrations/20210320112717_init/migration.sql
similarity index 100%
rename from prisma/postgresql/migrations/20210320112717_init/migration.sql
rename to db/postgresql/migrations/20210320112717_init/migration.sql
diff --git a/prisma/postgresql/migrations/migration_lock.toml b/db/postgresql/migrations/migration_lock.toml
similarity index 100%
rename from prisma/postgresql/migrations/migration_lock.toml
rename to db/postgresql/migrations/migration_lock.toml
diff --git a/prisma/schema.postgresql.prisma b/db/postgresql/schema.prisma
similarity index 100%
rename from prisma/schema.postgresql.prisma
rename to db/postgresql/schema.prisma
diff --git a/lib/db.js b/lib/db.js
index 35948998e..755696f4a 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -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;
diff --git a/package.json b/package.json
index 20e3d05e5..f5386cb58 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/prisma/mysql/schema.mysql.prisma b/prisma/mysql/schema.mysql.prisma
deleted file mode 120000
index fec72c832..000000000
--- a/prisma/mysql/schema.mysql.prisma
+++ /dev/null
@@ -1 +0,0 @@
-../schema.mysql.prisma
\ No newline at end of file
diff --git a/prisma/postgresql/schema.postgresql.prisma b/prisma/postgresql/schema.postgresql.prisma
deleted file mode 120000
index 6c19dc779..000000000
--- a/prisma/postgresql/schema.postgresql.prisma
+++ /dev/null
@@ -1 +0,0 @@
-../schema.postgresql.prisma
\ No newline at end of file
diff --git a/prisma/seed.js b/prisma/seed.js
deleted file mode 100644
index 12f59004f..000000000
--- a/prisma/seed.js
+++ /dev/null
@@ -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();
- });
diff --git a/scripts/check-db.js b/scripts/check-db.js
new file mode 100644
index 000000000..118ebd219
--- /dev/null
+++ b/scripts/check-db.js
@@ -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();
+ });
diff --git a/scripts/copy-db-schema.js b/scripts/copy-db-files.js
similarity index 56%
rename from scripts/copy-db-schema.js
rename to scripts/copy-db-files.js
index 9c4ed6db0..9bba914d5 100644
--- a/scripts/copy-db-schema.js
+++ b/scripts/copy-db-files.js
@@ -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}`);