migrator.cjs 2.7 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 migrationsSchema = config.migrationsSchema ?? "drizzle";
  30. const migrationTableCreate = import_sql.sql`
  31. CREATE TABLE IF NOT EXISTS ${import_sql.sql.identifier(migrationsSchema)}.${import_sql.sql.identifier(migrationsTable)} (
  32. id SERIAL PRIMARY KEY,
  33. hash text NOT NULL,
  34. created_at bigint
  35. )
  36. `;
  37. await db.session.execute(import_sql.sql`CREATE SCHEMA IF NOT EXISTS ${import_sql.sql.identifier(migrationsSchema)}`);
  38. await db.session.execute(migrationTableCreate);
  39. const dbMigrations = await db.session.all(
  40. import_sql.sql`select id, hash, created_at from ${import_sql.sql.identifier(migrationsSchema)}.${import_sql.sql.identifier(migrationsTable)} order by created_at desc limit 1`
  41. );
  42. const lastDbMigration = dbMigrations[0];
  43. const rowsToInsert = [];
  44. for await (const migration of migrations) {
  45. if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
  46. for (const stmt of migration.sql) {
  47. await db.session.execute(import_sql.sql.raw(stmt));
  48. }
  49. rowsToInsert.push(
  50. import_sql.sql`insert into ${import_sql.sql.identifier(migrationsSchema)}.${import_sql.sql.identifier(migrationsTable)} ("hash", "created_at") values(${migration.hash}, ${migration.folderMillis})`
  51. );
  52. }
  53. }
  54. for await (const rowToInsert of rowsToInsert) {
  55. await db.session.execute(rowToInsert);
  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