migrator.cjs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, config) {
  27. const migrations = (0, import_migrator.readMigrationFiles)(config);
  28. const migrationsTable = 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.session.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 statementToBatch = [];
  42. for (const migration of migrations) {
  43. if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
  44. for (const stmt of migration.sql) {
  45. statementToBatch.push(db.run(import_sql.sql.raw(stmt)));
  46. }
  47. statementToBatch.push(
  48. db.run(
  49. import_sql.sql`INSERT INTO ${import_sql.sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${import_sql.sql.raw(`'${migration.hash}'`)}, ${import_sql.sql.raw(`${migration.folderMillis}`)})`
  50. )
  51. );
  52. }
  53. }
  54. if (statementToBatch.length > 0) {
  55. await db.session.batch(statementToBatch);
  56. }
  57. }
  58. // Annotate the CommonJS export names for ESM import in node:
  59. 0 && (module.exports = {
  60. migrate
  61. });
  62. //# sourceMappingURL=migrator.cjs.map