diff --git a/db/mysql/migrations/03_remove_cascade_delete/migration.sql b/db/mysql/migrations/03_remove_cascade_delete/migration.sql index 542784a9..f5ea97c0 100644 --- a/db/mysql/migrations/03_remove_cascade_delete/migration.sql +++ b/db/mysql/migrations/03_remove_cascade_delete/migration.sql @@ -1,35 +1,59 @@ -- DropForeignKey -ALTER TABLE `event` DROP FOREIGN KEY `event_ibfk_2`; +ALTER TABLE + `event` DROP FOREIGN KEY `event_ibfk_2`; -- DropForeignKey -ALTER TABLE `event` DROP FOREIGN KEY `event_ibfk_1`; +ALTER TABLE + `event` DROP FOREIGN KEY `event_ibfk_1`; -- DropForeignKey -ALTER TABLE `pageview` DROP FOREIGN KEY `pageview_ibfk_2`; +ALTER TABLE + `pageview` DROP FOREIGN KEY `pageview_ibfk_2`; -- DropForeignKey -ALTER TABLE `pageview` DROP FOREIGN KEY `pageview_ibfk_1`; +ALTER TABLE + `pageview` DROP FOREIGN KEY `pageview_ibfk_1`; -- DropForeignKey -ALTER TABLE `session` DROP FOREIGN KEY `session_ibfk_1`; +ALTER TABLE + `session` DROP FOREIGN KEY `session_ibfk_1`; -- DropForeignKey -ALTER TABLE `website` DROP FOREIGN KEY `website_ibfk_1`; +ALTER TABLE + `website` DROP FOREIGN KEY `website_ibfk_1`; -- AddForeignKey -ALTER TABLE `event` ADD CONSTRAINT `event_session_id_fkey` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE + `event` +ADD + CONSTRAINT `event_session_id_fkey` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `event` ADD CONSTRAINT `event_website_id_fkey` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE + `event` +ADD + CONSTRAINT `event_website_id_fkey` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `pageview` ADD CONSTRAINT `pageview_session_id_fkey` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE + `pageview` +ADD + CONSTRAINT `pageview_session_id_fkey` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `pageview` ADD CONSTRAINT `pageview_website_id_fkey` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE + `pageview` +ADD + CONSTRAINT `pageview_website_id_fkey` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `session` ADD CONSTRAINT `session_website_id_fkey` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE + `session` +ADD + CONSTRAINT `session_website_id_fkey` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `website` ADD CONSTRAINT `website_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `account`(`user_id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE + `website` +ADD + CONSTRAINT `website_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `account`(`user_id`) ON DELETE RESTRICT ON UPDATE CASCADE; \ No newline at end of file diff --git a/db/mysql/migrations/04_edit_session_model/migration.sql b/db/mysql/migrations/04_edit_session_model/migration.sql new file mode 100644 index 00000000..c519d8d7 --- /dev/null +++ b/db/mysql/migrations/04_edit_session_model/migration.sql @@ -0,0 +1,54 @@ +/* + Warnings: + + - The primary key for the `event_data` table will be changed. If it partially fails, the table could be left without primary key constraint. + - You are about to alter the column `event_data_id` on the `event_data` table. The data in that column could be lost. The data in that column will be cast from `Int` to `UnsignedInt`. + - You are about to drop the `_event_old` table. If the table is not empty, all the data it contains will be lost. + + */ +-- DropForeignKey +ALTER TABLE + `event` DROP FOREIGN KEY `event_session_id_fkey`; + +-- DropForeignKey +ALTER TABLE + `event` DROP FOREIGN KEY `event_website_id_fkey`; + +-- DropForeignKey +ALTER TABLE + `event_data` DROP FOREIGN KEY `event_data_event_id_fkey`; + +-- DropForeignKey +ALTER TABLE + `pageview` DROP FOREIGN KEY `pageview_session_id_fkey`; + +-- DropForeignKey +ALTER TABLE + `pageview` DROP FOREIGN KEY `pageview_website_id_fkey`; + +-- DropForeignKey +ALTER TABLE + `session` DROP FOREIGN KEY `session_website_id_fkey`; + +-- DropForeignKey +ALTER TABLE + `website` DROP FOREIGN KEY `website_user_id_fkey`; + +-- AlterTable +ALTER TABLE + `event_data` DROP PRIMARY KEY, +MODIFY + `event_data_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, +ADD + PRIMARY KEY (`event_data_id`); + +-- AlterTable +ALTER TABLE + `session` +ADD + COLUMN `ip` VARCHAR(255) NULL, +ADD + COLUMN `user_agent` TEXT NULL; + +-- DropTable +DROP TABLE `_event_old`; \ No newline at end of file diff --git a/db/mysql/schema.prisma b/db/mysql/schema.prisma index fac97143..317e0cba 100644 --- a/db/mysql/schema.prisma +++ b/db/mysql/schema.prisma @@ -1,10 +1,12 @@ generator client { provider = "prisma-client-js" + previewFeatures = ["referentialIntegrity"] } datasource db { provider = "mysql" url = env("DATABASE_URL") + referentialIntegrity = "prisma" } model account { diff --git a/package.json b/package.json index be7fd7d1..643a57d9 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,14 @@ "start-server": "node server.js", "build-app": "next build", "build-tracker": "rollup -c rollup.tracker.config.js", - "build-db": "npm-run-all copy-db-files build-db-client", + "build-db": "npm-run-all copy-db-files prisma:generate", "build-lang": "npm-run-all format-lang compile-lang download-country-names download-language-names", "build-geo": "node scripts/build-geo.js", - "build-db-schema": "prisma db pull", - "build-db-client": "prisma generate", + "prisma:pull": "prisma db pull", + "prisma:generate": "prisma generate", "update-tracker": "node scripts/update-tracker.js", - "update-db": "prisma migrate deploy", + "prisma:dev": "prisma migrate dev", + "prisma:deploy": "prisma migrate deploy", "check-db": "node scripts/check-db.js", "copy-db-files": "node scripts/copy-db-files.js", "generate-lang": "npm-run-all extract-lang merge-lang", @@ -56,7 +57,7 @@ }, "dependencies": { "@fontsource/inter": "4.5.7", - "@prisma/client": "4.3.1", + "@prisma/client": "^4.4.0", "chalk": "^4.1.1", "chart.js": "^2.9.4", "classnames": "^2.3.1", @@ -125,7 +126,7 @@ "postcss-preset-env": "7.4.3", "postcss-rtlcss": "^3.6.1", "prettier": "^2.6.2", - "prisma": "4.3.1", + "prisma": "^4.4.0", "prompts": "2.4.2", "rollup": "^2.70.1", "rollup-plugin-terser": "^7.0.2", diff --git a/sql/schema.mysql.sql b/sql/schema.mysql.sql index 0ae7775a..f6875ccb 100644 --- a/sql/schema.mysql.sql +++ b/sql/schema.mysql.sql @@ -6,10 +6,9 @@ CREATE TABLE `account` ( `is_admin` BOOLEAN NOT NULL DEFAULT false, `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0), `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0), - UNIQUE INDEX `username`(`username`), PRIMARY KEY (`user_id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- CreateTable CREATE TABLE `event` ( @@ -20,12 +19,11 @@ CREATE TABLE `event` ( `url` VARCHAR(500) NOT NULL, `event_type` VARCHAR(50) NOT NULL, `event_value` VARCHAR(50) NOT NULL, - INDEX `event_created_at_idx`(`created_at`), INDEX `event_session_id_idx`(`session_id`), INDEX `event_website_id_idx`(`website_id`), PRIMARY KEY (`event_id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- CreateTable CREATE TABLE `pageview` ( @@ -35,14 +33,13 @@ CREATE TABLE `pageview` ( `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0), `url` VARCHAR(500) NOT NULL, `referrer` VARCHAR(500) NULL, - INDEX `pageview_created_at_idx`(`created_at`), INDEX `pageview_session_id_idx`(`session_id`), INDEX `pageview_website_id_created_at_idx`(`website_id`, `created_at`), INDEX `pageview_website_id_idx`(`website_id`), INDEX `pageview_website_id_session_id_created_at_idx`(`website_id`, `session_id`, `created_at`), PRIMARY KEY (`view_id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- CreateTable CREATE TABLE `session` ( @@ -57,12 +54,13 @@ CREATE TABLE `session` ( `screen` VARCHAR(11) NULL, `language` VARCHAR(35) NULL, `country` CHAR(2) NULL, - + `ip` VARCHAR(255) NULL, + `user_agent` TEXT NULL, UNIQUE INDEX `session_uuid`(`session_uuid`), INDEX `session_created_at_idx`(`created_at`), INDEX `session_website_id_idx`(`website_id`), PRIMARY KEY (`session_id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- CreateTable CREATE TABLE `website` ( @@ -73,30 +71,18 @@ CREATE TABLE `website` ( `domain` VARCHAR(500) NULL, `share_id` VARCHAR(64) NULL, `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0), - UNIQUE INDEX `website_uuid`(`website_uuid`), UNIQUE INDEX `share_id`(`share_id`), INDEX `website_user_id_idx`(`user_id`), PRIMARY KEY (`website_id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- AddForeignKey -ALTER TABLE `event` ADD CONSTRAINT `event_ibfk_2` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE CASCADE ON UPDATE NO ACTION; - --- AddForeignKey -ALTER TABLE `event` ADD CONSTRAINT `event_ibfk_1` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE CASCADE ON UPDATE NO ACTION; - --- AddForeignKey -ALTER TABLE `pageview` ADD CONSTRAINT `pageview_ibfk_2` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE CASCADE ON UPDATE NO ACTION; - --- AddForeignKey -ALTER TABLE `pageview` ADD CONSTRAINT `pageview_ibfk_1` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE CASCADE ON UPDATE NO ACTION; - --- AddForeignKey -ALTER TABLE `session` ADD CONSTRAINT `session_ibfk_1` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE CASCADE ON UPDATE NO ACTION; - --- AddForeignKey -ALTER TABLE `website` ADD CONSTRAINT `website_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `account`(`user_id`) ON DELETE CASCADE ON UPDATE NO ACTION; +) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- CreateAdminUser -INSERT INTO account (username, password, is_admin) values ('admin', '$2b$10$BUli0c.muyCW1ErNJc3jL.vFRFtFJWrT8/GcR4A.sUdCznaXiqFXa', true); +INSERT INTO + account (username, password, is_admin) +values + ( + 'admin', + '$2b$10$BUli0c.muyCW1ErNJc3jL.vFRFtFJWrT8/GcR4A.sUdCznaXiqFXa', + true + ); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index f4b49dc1..81b116bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1445,22 +1445,22 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@prisma/client@4.3.1": - version "4.3.1" - resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.3.1.tgz#b9aad9bd9bd43e7f715ec1d763c8bd9273688800" - integrity sha512-FA0/d1VMJNWqzU7WVWTNWJ+lGOLR9JUBnF73GdIPAEVo/6dWk4gHx0EmgeU+SMv4MZoxgOeTBJF2azhg7x0hMw== +"@prisma/client@^4.4.0": + version "4.4.0" + resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.4.0.tgz#45f59c172dd3621ecc92d7cf9bc765d85e6c7d56" + integrity sha512-ciKOP246x1xwr04G9ajHlJ4pkmtu9Q6esVyqVBO0QJihaKQIUvbPjClp17IsRJyxqNpFm4ScbOc/s9DUzKHINQ== dependencies: - "@prisma/engines-version" "4.3.0-32.c875e43600dfe042452e0b868f7a48b817b9640b" + "@prisma/engines-version" "4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6" -"@prisma/engines-version@4.3.0-32.c875e43600dfe042452e0b868f7a48b817b9640b": - version "4.3.0-32.c875e43600dfe042452e0b868f7a48b817b9640b" - resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.3.0-32.c875e43600dfe042452e0b868f7a48b817b9640b.tgz#a564dd6fb6f25405fe06164989ebd8a1e4bb3508" - integrity sha512-8yWpXkQRmiSfsi2Wb/ZS5D3RFbeu/btL9Pm/gdF4phB0Lo5KGsDFMxFMgaD64mwED2nHc8ZaEJg/+4Jymb9Znw== +"@prisma/engines-version@4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6": + version "4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6" + resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6.tgz#00875863bb30b670a586a5b5794a000f7f3ad976" + integrity sha512-P5v/PuEIJLYXZUZBvOLPqoyCW+m6StNqHdiR6te++gYVODpPdLakks5HVx3JaZIY+LwR02juJWFlwpc9Eog/ug== -"@prisma/engines@4.3.1": - version "4.3.1" - resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-4.3.1.tgz#fb0ad69eded13827fb4cd81995a351c234e8327f" - integrity sha512-4JF/uMaEDAPdcdZNOrnzE3BvrbGpjgV0FcPT3EVoi6I86fWkloqqxBt+KcK/+fIRR0Pxj66uGR9wVH8U1Y13JA== +"@prisma/engines@4.4.0": + version "4.4.0" + resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-4.4.0.tgz#6ca7d3ce8eee08dcfa82311b0a02f5ccaac7dc0c" + integrity sha512-Fpykccxlt9MHrAs/QpPGpI2nOiRxuLA+LiApgA59ibbf24YICZIMWd3SI2YD+q0IAIso0jCGiHhirAIbxK3RyQ== "@react-spring/animated@~9.5.2": version "9.5.2" @@ -5400,12 +5400,12 @@ prettier@^2.6.2: resolved "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz" integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g== -prisma@4.3.1: - version "4.3.1" - resolved "https://registry.yarnpkg.com/prisma/-/prisma-4.3.1.tgz#9e4c24e49710392be40ee75fb3d9341d7487881e" - integrity sha512-90xo06wtqil76Xsi3mNpc4Js3SdDRR5g4qb9h+4VWY4Y8iImJY6xc3PX+C9xxTSt1lr0Q89A0MLkJjd8ax6KiQ== +prisma@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/prisma/-/prisma-4.4.0.tgz#0c53324bf6a29474636b3e1964e0d72e0277bf8f" + integrity sha512-l/QKLmLcKJQFuc+X02LyICo0NWTUVaNNZ00jKJBqwDyhwMAhboD1FWwYV50rkH4Wls0RviAJSFzkC2ZrfawpfA== dependencies: - "@prisma/engines" "4.3.1" + "@prisma/engines" "4.4.0" progress@^2.0.0: version "2.0.3"