migrator.cjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_migrator = require("../migrator.cjs");
  25. var import_sql = require("../sql/sql.cjs");
  26. async function migrate(db, callback, config) {
  27. const migrations = (0, import_migrator.readMigrationFiles)(config);
  28. const migrationsTable = typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
  29. const migrationTableCreate = import_sql.sql`
  30. CREATE TABLE IF NOT EXISTS ${import_sql.sql.identifier(migrationsTable)} (
  31. id SERIAL PRIMARY KEY,
  32. hash text NOT NULL,
  33. created_at numeric
  34. )
  35. `;
  36. await db.run(migrationTableCreate);
  37. const dbMigrations = await db.values(
  38. import_sql.sql`SELECT id, hash, created_at FROM ${import_sql.sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
  39. );
  40. const lastDbMigration = dbMigrations[0] ?? void 0;
  41. const queriesToRun = [];
  42. for (const migration of migrations) {
  43. if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
  44. queriesToRun.push(
  45. ...migration.sql,
  46. `INSERT INTO \`${migrationsTable}\` ("hash", "created_at") VALUES('${migration.hash}', '${migration.folderMillis}')`
  47. );
  48. }
  49. }
  50. await callback(queriesToRun);
  51. }
  52. // Annotate the CommonJS export names for ESM import in node:
  53. 0 && (module.exports = {
  54. migrate
  55. });
  56. //# sourceMappingURL=migrator.cjs.map