migrator.cjs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var migrator_exports = {};
  20. __export(migrator_exports, {
  21. migrate: () => migrate
  22. });
  23. module.exports = __toCommonJS(migrator_exports);
  24. var import_sql = require("../sql/index.cjs");
  25. function readMigrationFiles({ journal, migrations }) {
  26. const migrationQueries = [];
  27. for (const journalEntry of journal.entries) {
  28. const query = migrations[`m${journalEntry.idx.toString().padStart(4, "0")}`];
  29. if (!query) {
  30. throw new Error(`Missing migration: ${journalEntry.tag}`);
  31. }
  32. try {
  33. const result = query.split("--> statement-breakpoint").map((it) => {
  34. return it;
  35. });
  36. migrationQueries.push({
  37. sql: result,
  38. bps: journalEntry.breakpoints,
  39. folderMillis: journalEntry.when,
  40. hash: ""
  41. });
  42. } catch {
  43. throw new Error(`Failed to parse migration: ${journalEntry.tag}`);
  44. }
  45. }
  46. return migrationQueries;
  47. }
  48. async function migrate(db, config) {
  49. const migrations = readMigrationFiles(config);
  50. db.transaction((tx) => {
  51. try {
  52. const migrationsTable = "__drizzle_migrations";
  53. const migrationTableCreate = import_sql.sql`
  54. CREATE TABLE IF NOT EXISTS ${import_sql.sql.identifier(migrationsTable)} (
  55. id SERIAL PRIMARY KEY,
  56. hash text NOT NULL,
  57. created_at numeric
  58. )
  59. `;
  60. db.run(migrationTableCreate);
  61. const dbMigrations = db.values(
  62. import_sql.sql`SELECT id, hash, created_at FROM ${import_sql.sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
  63. );
  64. const lastDbMigration = dbMigrations[0] ?? void 0;
  65. for (const migration of migrations) {
  66. if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
  67. for (const stmt of migration.sql) {
  68. db.run(import_sql.sql.raw(stmt));
  69. }
  70. db.run(
  71. import_sql.sql`INSERT INTO ${import_sql.sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`
  72. );
  73. }
  74. }
  75. } catch (error) {
  76. tx.rollback();
  77. throw error;
  78. }
  79. });
  80. }
  81. // Annotate the CommonJS export names for ESM import in node:
  82. 0 && (module.exports = {
  83. migrate
  84. });
  85. //# sourceMappingURL=migrator.cjs.map