1.0.0-beta9.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import db from "@server/db";
  2. import {
  3. emailVerificationCodes,
  4. passwordResetTokens,
  5. resourceOtp,
  6. resourceWhitelist,
  7. userInvites,
  8. users
  9. } from "@server/db/schema";
  10. import { APP_PATH, configFilePath1, configFilePath2 } from "@server/lib/consts";
  11. import { sql } from "drizzle-orm";
  12. import fs from "fs";
  13. import yaml from "js-yaml";
  14. import path from "path";
  15. import { z } from "zod";
  16. import { fromZodError } from "zod-validation-error";
  17. export default async function migration() {
  18. console.log("Running setup script 1.0.0-beta.9...");
  19. try {
  20. await db.transaction(async (trx) => {
  21. trx.run(sql`UPDATE ${users} SET email = LOWER(email);`);
  22. trx.run(
  23. sql`UPDATE ${emailVerificationCodes} SET email = LOWER(email);`
  24. );
  25. trx.run(
  26. sql`UPDATE ${passwordResetTokens} SET email = LOWER(email);`
  27. );
  28. trx.run(sql`UPDATE ${userInvites} SET email = LOWER(email);`);
  29. trx.run(sql`UPDATE ${resourceWhitelist} SET email = LOWER(email);`);
  30. trx.run(sql`UPDATE ${resourceOtp} SET email = LOWER(email);`);
  31. });
  32. } catch (error) {
  33. console.log(
  34. "We were unable to make all emails lower case in the database."
  35. );
  36. console.error(error);
  37. }
  38. try {
  39. // Determine which config file exists
  40. const filePaths = [configFilePath1, configFilePath2];
  41. let filePath = "";
  42. for (const path of filePaths) {
  43. if (fs.existsSync(path)) {
  44. filePath = path;
  45. break;
  46. }
  47. }
  48. if (!filePath) {
  49. throw new Error(
  50. `No config file found (expected config.yml or config.yaml).`
  51. );
  52. }
  53. // Read and parse the YAML file
  54. let rawConfig: any;
  55. const fileContents = fs.readFileSync(filePath, "utf8");
  56. rawConfig = yaml.load(fileContents);
  57. rawConfig.server.resource_session_request_param = "p_session_request";
  58. rawConfig.server.session_cookie_name = "p_session_token"; // rename to prevent conflicts
  59. delete rawConfig.server.resource_session_cookie_name;
  60. // Write the updated YAML back to the file
  61. const updatedYaml = yaml.dump(rawConfig);
  62. fs.writeFileSync(filePath, updatedYaml, "utf8");
  63. } catch (e) {
  64. console.log(
  65. `Failed to add resource_session_request_param to config. Please add it manually. https://docs.fossorial.io/Pangolin/Configuration/config`
  66. );
  67. throw e;
  68. }
  69. try {
  70. const traefikPath = path.join(
  71. APP_PATH,
  72. "traefik",
  73. "traefik_config.yml"
  74. );
  75. const schema = z.object({
  76. experimental: z.object({
  77. plugins: z.object({
  78. badger: z.object({
  79. moduleName: z.string(),
  80. version: z.string()
  81. })
  82. })
  83. })
  84. });
  85. const traefikFileContents = fs.readFileSync(traefikPath, "utf8");
  86. const traefikConfig = yaml.load(traefikFileContents) as any;
  87. const parsedConfig = schema.safeParse(traefikConfig);
  88. if (!parsedConfig.success) {
  89. throw new Error(fromZodError(parsedConfig.error).toString());
  90. }
  91. traefikConfig.experimental.plugins.badger.version = "v1.0.0-beta.3";
  92. const updatedTraefikYaml = yaml.dump(traefikConfig);
  93. fs.writeFileSync(traefikPath, updatedTraefikYaml, "utf8");
  94. console.log(
  95. "Updated the version of Badger in your Traefik configuration to v1.0.0-beta.3."
  96. );
  97. } catch (e) {
  98. console.log(
  99. "We were unable to update the version of Badger in your Traefik configuration. Please update it manually."
  100. );
  101. console.error(e);
  102. }
  103. try {
  104. await db.transaction(async (trx) => {
  105. trx.run(sql`ALTER TABLE 'resourceSessions' ADD 'isRequestToken' integer;`);
  106. trx.run(sql`ALTER TABLE 'resourceSessions' ADD 'userSessionId' text REFERENCES session(id);`);
  107. });
  108. } catch (e) {
  109. console.log(
  110. "We were unable to add columns to the resourceSessions table."
  111. );
  112. throw e;
  113. }
  114. console.log("Done.");
  115. }