d1s 0.2.11 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.next/.next/BUILD_ID +1 -1
- package/dist/.next/.next/app-build-manifest.json +28 -28
- package/dist/.next/.next/app-path-routes-manifest.json +1 -1
- package/dist/.next/.next/build-manifest.json +3 -3
- package/dist/.next/.next/cache/.rscinfo +1 -1
- package/dist/.next/.next/cache/.tsbuildinfo +1 -1
- package/dist/.next/.next/cache/webpack/client-production/0.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/1.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/10.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/11.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/12.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/2.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/3.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/4.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/5.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/6.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/7.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/8.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/9.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/index.pack +0 -0
- package/dist/.next/.next/cache/webpack/client-production/index.pack.old +0 -0
- package/dist/.next/.next/cache/webpack/edge-server-production/index.pack +0 -0
- package/dist/.next/.next/cache/webpack/server-production/0.pack +0 -0
- package/dist/.next/.next/cache/webpack/server-production/1.pack +0 -0
- package/dist/.next/.next/cache/webpack/server-production/2.pack +0 -0
- package/dist/.next/.next/cache/webpack/server-production/3.pack +0 -0
- package/dist/.next/.next/cache/webpack/server-production/index.pack +0 -0
- package/dist/.next/.next/cache/webpack/server-production/index.pack.old +0 -0
- package/dist/.next/.next/next-minimal-server.js.nft.json +1 -1
- package/dist/.next/.next/next-server.js.nft.json +1 -1
- package/dist/.next/.next/prerender-manifest.json +1 -1
- package/dist/.next/.next/required-server-files.json +1 -1
- package/dist/.next/.next/server/app/_not-found/page.js +1 -1
- package/dist/.next/.next/server/app/_not-found/page.js.nft.json +1 -1
- package/dist/.next/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/_not-found.html +1 -1
- package/dist/.next/.next/server/app/_not-found.rsc +4 -4
- package/dist/.next/.next/server/app/api/cloudflare/route.js +1 -1
- package/dist/.next/.next/server/app/api/cloudflare/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/databases/route.js +1 -1
- package/dist/.next/.next/server/app/api/databases/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/migrations/sync/route.js +5 -5
- package/dist/.next/.next/server/app/api/migrations/sync/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/migrations/sync-files/route.js +3 -3
- package/dist/.next/.next/server/app/api/migrations/sync-files/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/pull-data/confirm/route.js +1 -1
- package/dist/.next/.next/server/app/api/pull-data/confirm/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/pull-remote/confirm/route.js +2 -2
- package/dist/.next/.next/server/app/api/pull-remote/confirm/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/pull-remote/preview/route.js +4 -4
- package/dist/.next/.next/server/app/api/pull-remote/preview/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/push-remote/confirm/route.js +5 -5
- package/dist/.next/.next/server/app/api/push-remote/confirm/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/push-remote/preview/route.js +4 -4
- package/dist/.next/.next/server/app/api/push-remote/preview/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/query/route.js +3 -3
- package/dist/.next/.next/server/app/api/query/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/tables/[name]/route.js +5 -5
- package/dist/.next/.next/server/app/api/tables/[name]/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/tables/route.js +2 -2
- package/dist/.next/.next/server/app/api/tables/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/api/update/route.js +1 -1
- package/dist/.next/.next/server/app/api/update/route_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app/index.html +2 -2
- package/dist/.next/.next/server/app/index.rsc +5 -5
- package/dist/.next/.next/server/app/page.js +7 -7
- package/dist/.next/.next/server/app/page.js.nft.json +1 -1
- package/dist/.next/.next/server/app/page_client-reference-manifest.js +1 -1
- package/dist/.next/.next/server/app-paths-manifest.json +1 -1
- package/dist/.next/.next/server/chunks/226.js +12 -12
- package/dist/.next/.next/server/chunks/391.js +1 -1
- package/dist/.next/.next/server/chunks/572.js +1 -0
- package/dist/.next/.next/server/chunks/992.js +3 -3
- package/dist/.next/.next/server/middleware-build-manifest.js +1 -1
- package/dist/.next/.next/server/next-font-manifest.js +1 -1
- package/dist/.next/.next/server/next-font-manifest.json +1 -1
- package/dist/.next/.next/server/pages/404.html +1 -1
- package/dist/.next/.next/server/pages/500.html +1 -1
- package/dist/.next/.next/server/server-reference-manifest.json +1 -1
- package/dist/.next/.next/static/chunks/{576-c4daf274bb6469a1.js → 576-18a2209e1ed26aad.js} +1 -1
- package/dist/.next/.next/static/chunks/{92-c4466e0f1ca8a3a8.js → 92-5f7522943fcb72a8.js} +1 -1
- package/dist/.next/.next/static/chunks/app/{layout-be6ee67cf0c92654.js → layout-40ec07d04681ba81.js} +1 -1
- package/dist/.next/.next/static/chunks/app/page-f05cd0fc436bf9bc.js +1 -0
- package/dist/.next/.next/static/chunks/app/page-f2bc307c0d8acdaa.js +1 -0
- package/dist/.next/.next/static/chunks/{main-app-fa35024de9bd9284.js → main-app-4506e88db72db4b7.js} +1 -1
- package/dist/.next/.next/static/css/{9f6c25c5400f83d3.css → 7c16f57d325d0d7d.css} +1 -1
- package/dist/.next/.next/static/ejYPqJ2kVicVp5EFWOZIC/_buildManifest.js +1 -0
- package/dist/.next/.next/static/ejYPqJ2kVicVp5EFWOZIC/_ssgManifest.js +1 -0
- package/dist/.next/.next/static/gABGrAoO8IPArDo_Ns_7g/_buildManifest.js +1 -0
- package/dist/.next/.next/static/gABGrAoO8IPArDo_Ns_7g/_ssgManifest.js +1 -0
- package/dist/.next/.next/static/gh-QfIUVo4FTTduYCVKT6/_buildManifest.js +1 -0
- package/dist/.next/.next/static/gh-QfIUVo4FTTduYCVKT6/_ssgManifest.js +1 -0
- package/dist/.next/.next/static/media/8d697b304b401681-s.woff2 +0 -0
- package/dist/.next/.next/static/media/9610d9e46709d722-s.woff2 +0 -0
- package/dist/.next/.next/static/thaCy2qVeNqYYhdayr13C/_buildManifest.js +1 -0
- package/dist/.next/.next/static/thaCy2qVeNqYYhdayr13C/_ssgManifest.js +1 -0
- package/dist/.next/.next/static/ybCx6L6gHqtODbKmlhU-d/_buildManifest.js +1 -0
- package/dist/.next/.next/static/ybCx6L6gHqtODbKmlhU-d/_ssgManifest.js +1 -0
- package/dist/.next/.next/trace +3 -3
- package/dist/.next/.next/types/app/api/cloudflare/route.ts +1 -1
- package/dist/.next/.next/types/app/api/databases/route.ts +1 -1
- package/dist/.next/.next/types/app/api/migrations/sync/route.ts +1 -1
- package/dist/.next/.next/types/app/api/migrations/sync-files/route.ts +1 -1
- package/dist/.next/.next/types/app/api/pull-data/confirm/route.ts +1 -1
- package/dist/.next/.next/types/app/api/pull-remote/confirm/route.ts +1 -1
- package/dist/.next/.next/types/app/api/pull-remote/preview/route.ts +1 -1
- package/dist/.next/.next/types/app/api/push-remote/confirm/route.ts +1 -1
- package/dist/.next/.next/types/app/api/push-remote/preview/route.ts +1 -1
- package/dist/.next/.next/types/app/api/query/route.ts +1 -1
- package/dist/.next/.next/types/app/api/tables/[name]/route.ts +1 -1
- package/dist/.next/.next/types/app/api/tables/route.ts +1 -1
- package/dist/.next/.next/types/app/api/update/route.ts +1 -1
- package/dist/.next/.next/types/app/page.ts +1 -1
- package/dist/.next/BUILD_ID +1 -1
- package/dist/.next/app-build-manifest.json +28 -28
- package/dist/.next/app-path-routes-manifest.json +1 -1
- package/dist/.next/build-manifest.json +3 -3
- package/dist/.next/cache/.rscinfo +1 -1
- package/dist/.next/cache/.tsbuildinfo +1 -1
- package/dist/.next/cache/webpack/client-production/0.pack +0 -0
- package/dist/.next/cache/webpack/client-production/1.pack +0 -0
- package/dist/.next/cache/webpack/client-production/2.pack +0 -0
- package/dist/.next/cache/webpack/client-production/index.pack +0 -0
- package/dist/.next/cache/webpack/client-production/index.pack.old +0 -0
- package/dist/.next/cache/webpack/edge-server-production/index.pack +0 -0
- package/dist/.next/cache/webpack/server-production/0.pack +0 -0
- package/dist/.next/cache/webpack/server-production/index.pack +0 -0
- package/dist/.next/next-minimal-server.js.nft.json +1 -1
- package/dist/.next/next-server.js.nft.json +1 -1
- package/dist/.next/prerender-manifest.json +1 -1
- package/dist/.next/required-server-files.json +1 -1
- package/dist/.next/server/app/_not-found/page.js +1 -1
- package/dist/.next/server/app/_not-found/page.js.nft.json +1 -1
- package/dist/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/_not-found.html +1 -1
- package/dist/.next/server/app/_not-found.rsc +4 -4
- package/dist/.next/server/app/api/cloudflare/route.js +1 -1
- package/dist/.next/server/app/api/cloudflare/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/databases/route.js +1 -1
- package/dist/.next/server/app/api/databases/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/migrations/sync/route.js +5 -5
- package/dist/.next/server/app/api/migrations/sync/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/migrations/sync-files/route.js +3 -3
- package/dist/.next/server/app/api/migrations/sync-files/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/pull-data/confirm/route.js +1 -1
- package/dist/.next/server/app/api/pull-data/confirm/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/pull-remote/confirm/route.js +2 -2
- package/dist/.next/server/app/api/pull-remote/confirm/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/pull-remote/preview/route.js +4 -4
- package/dist/.next/server/app/api/pull-remote/preview/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/push-remote/confirm/route.js +5 -5
- package/dist/.next/server/app/api/push-remote/confirm/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/push-remote/preview/route.js +4 -4
- package/dist/.next/server/app/api/push-remote/preview/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/query/route.js +3 -3
- package/dist/.next/server/app/api/query/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/tables/[name]/route.js +5 -5
- package/dist/.next/server/app/api/tables/[name]/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/tables/route.js +2 -2
- package/dist/.next/server/app/api/tables/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/api/update/route.js +1 -1
- package/dist/.next/server/app/api/update/route_client-reference-manifest.js +1 -1
- package/dist/.next/server/app/index.html +2 -2
- package/dist/.next/server/app/index.rsc +5 -5
- package/dist/.next/server/app/page.js +7 -7
- package/dist/.next/server/app/page.js.nft.json +1 -1
- package/dist/.next/server/app/page_client-reference-manifest.js +1 -1
- package/dist/.next/server/app-paths-manifest.json +1 -1
- package/dist/.next/server/chunks/226.js +12 -12
- package/dist/.next/server/chunks/391.js +1 -1
- package/dist/.next/server/chunks/572.js +1 -0
- package/dist/.next/server/chunks/992.js +3 -3
- package/dist/.next/server/middleware-build-manifest.js +1 -1
- package/dist/.next/server/next-font-manifest.js +1 -1
- package/dist/.next/server/next-font-manifest.json +1 -1
- package/dist/.next/server/pages/404.html +1 -1
- package/dist/.next/server/pages/500.html +1 -1
- package/dist/.next/server/server-reference-manifest.json +1 -1
- package/dist/.next/static/IX-QLbm7scYnyqWeeimal/_buildManifest.js +1 -0
- package/dist/.next/static/IX-QLbm7scYnyqWeeimal/_ssgManifest.js +1 -0
- package/dist/.next/static/chunks/{576-c4daf274bb6469a1.js → 576-18a2209e1ed26aad.js} +1 -1
- package/dist/.next/static/chunks/{92-c4466e0f1ca8a3a8.js → 92-5f7522943fcb72a8.js} +1 -1
- package/dist/.next/static/chunks/app/{layout-be6ee67cf0c92654.js → layout-40ec07d04681ba81.js} +1 -1
- package/dist/.next/static/chunks/app/page-f05cd0fc436bf9bc.js +1 -0
- package/dist/.next/static/chunks/{main-app-fa35024de9bd9284.js → main-app-4506e88db72db4b7.js} +1 -1
- package/dist/.next/static/css/{9f6c25c5400f83d3.css → 7c16f57d325d0d7d.css} +1 -1
- package/dist/.next/static/media/8d697b304b401681-s.woff2 +0 -0
- package/dist/.next/static/media/9610d9e46709d722-s.woff2 +0 -0
- package/dist/.next/trace +3 -14
- package/dist/.next/types/app/api/cloudflare/route.ts +1 -1
- package/dist/.next/types/app/api/databases/route.ts +1 -1
- package/dist/.next/types/app/api/migrations/sync/route.ts +1 -1
- package/dist/.next/types/app/api/migrations/sync-files/route.ts +1 -1
- package/dist/.next/types/app/api/pull-data/confirm/route.ts +1 -1
- package/dist/.next/types/app/api/pull-remote/confirm/route.ts +1 -1
- package/dist/.next/types/app/api/pull-remote/preview/route.ts +1 -1
- package/dist/.next/types/app/api/push-remote/confirm/route.ts +1 -1
- package/dist/.next/types/app/api/push-remote/preview/route.ts +1 -1
- package/dist/.next/types/app/api/query/route.ts +1 -1
- package/dist/.next/types/app/api/tables/[name]/route.ts +1 -1
- package/dist/.next/types/app/api/tables/route.ts +1 -1
- package/dist/.next/types/app/api/update/route.ts +1 -1
- package/dist/.next/types/app/page.ts +1 -1
- package/dist/app/api/tables/[name]/route.js +22 -10
- package/dist/app/globals.css +39 -0
- package/dist/d1-studio.js +2 -2
- package/dist/lib/data-type-handlers.js +426 -0
- package/dist/lib/sql-operations.js +32 -8
- package/dist/lib/sql-sanitizer.js +233 -0
- package/package.json +3 -9
- package/dist/.next/.next/server/chunks/90.js +0 -1
- package/dist/.next/.next/static/chunks/app/page-36ed30b2266617f3.js +0 -1
- package/dist/.next/server/chunks/90.js +0 -1
- package/dist/.next/static/chunks/app/page-36ed30b2266617f3.js +0 -1
- package/dist/app/app/api/cloudflare/route.js +0 -150
- package/dist/app/app/api/databases/route.js +0 -71
- package/dist/app/app/api/migrations/sync/route.js +0 -174
- package/dist/app/app/api/migrations/sync-files/route.js +0 -172
- package/dist/app/app/api/pull-data/confirm/route.js +0 -230
- package/dist/app/app/api/pull-remote/confirm/route.js +0 -278
- package/dist/app/app/api/pull-remote/preview/route.js +0 -144
- package/dist/app/app/api/push-remote/confirm/route.js +0 -196
- package/dist/app/app/api/push-remote/preview/route.js +0 -179
- package/dist/app/app/api/query/route.js +0 -168
- package/dist/app/app/api/tables/[name]/route.js +0 -174
- package/dist/app/app/api/tables/route.js +0 -178
- package/dist/app/app/api/update/route.js +0 -45
- package/dist/app/app/globals.css +0 -93
- package/dist/app/app/layout.jsx +0 -35
- package/dist/app/app/page.jsx +0 -34
- package/next.config.mjs +0 -18
- package/postcss.config.mjs +0 -8
- package/scripts/studio.js +0 -199
- package/scripts/test.js +0 -366
- package/src/app/api/cloudflare/route.js +0 -150
- package/src/app/api/databases/route.js +0 -71
- package/src/app/api/migrations/sync/route.js +0 -174
- package/src/app/api/migrations/sync-files/route.js +0 -172
- package/src/app/api/pull-data/confirm/route.js +0 -230
- package/src/app/api/pull-remote/confirm/route.js +0 -278
- package/src/app/api/pull-remote/preview/route.js +0 -144
- package/src/app/api/push-remote/confirm/route.js +0 -196
- package/src/app/api/push-remote/preview/route.js +0 -179
- package/src/app/api/query/route.js +0 -168
- package/src/app/api/tables/[name]/route.js +0 -174
- package/src/app/api/tables/route.js +0 -178
- package/src/app/api/update/route.js +0 -45
- package/src/app/globals.css +0 -93
- package/src/app/layout.jsx +0 -35
- package/src/app/page.jsx +0 -34
- package/src/components/cloudflare-credentials-modal.jsx +0 -94
- package/src/components/column-selector.jsx +0 -133
- package/src/components/column-sorting.jsx +0 -65
- package/src/components/delete-records-dialog.jsx +0 -64
- package/src/components/download-button.jsx +0 -205
- package/src/components/edit-error-alert.jsx +0 -30
- package/src/components/editable-cell.jsx +0 -163
- package/src/components/editable-row.jsx +0 -169
- package/src/components/environment-handler.jsx +0 -332
- package/src/components/environment-select.jsx +0 -62
- package/src/components/pull-preview-toast.jsx +0 -215
- package/src/components/push-error-alert.jsx +0 -72
- package/src/components/push-preview-toast.jsx +0 -183
- package/src/components/query-results.jsx +0 -112
- package/src/components/refresh-button.jsx +0 -61
- package/src/components/resize-columns.jsx +0 -101
- package/src/components/sidebar.jsx +0 -254
- package/src/components/sql-editor.jsx +0 -411
- package/src/components/sql-error-display.jsx +0 -167
- package/src/components/sync-migrations-button.jsx +0 -87
- package/src/components/table-error-dialog.jsx +0 -65
- package/src/components/table-toolbar.jsx +0 -285
- package/src/components/table-view.jsx +0 -843
- package/src/components/ui/alert.jsx +0 -47
- package/src/components/ui/button.jsx +0 -48
- package/src/components/ui/card.jsx +0 -50
- package/src/components/ui/checkbox.jsx +0 -24
- package/src/components/ui/collapsible.jsx +0 -11
- package/src/components/ui/dialog.jsx +0 -96
- package/src/components/ui/dropdown-menu.jsx +0 -158
- package/src/components/ui/input.jsx +0 -19
- package/src/components/ui/scroll-area.jsx +0 -40
- package/src/components/ui/select.jsx +0 -121
- package/src/components/ui/separator.jsx +0 -25
- package/src/components/ui/sheet.jsx +0 -109
- package/src/components/ui/sidebar.jsx +0 -620
- package/src/components/ui/skeleton.jsx +0 -14
- package/src/components/ui/table.jsx +0 -86
- package/src/components/ui/tabs.jsx +0 -43
- package/src/components/ui/toast.jsx +0 -86
- package/src/components/ui/toaster.jsx +0 -35
- package/src/components/ui/tooltip.jsx +0 -28
- package/src/components/update-banner.jsx +0 -93
- package/src/context/database-context.js +0 -510
- package/src/hooks/use-mobile.jsx +0 -19
- package/src/hooks/use-sql-error.ts +0 -23
- package/src/hooks/use-toast.js +0 -155
- package/src/lib/database-queue.js +0 -37
- package/src/lib/error.js +0 -20
- package/src/lib/find-wrangler-config.js +0 -74
- package/src/lib/license.js +0 -99
- package/src/lib/logger.js +0 -58
- package/src/lib/migrations.js +0 -415
- package/src/lib/parse-sql-error.js +0 -200
- package/src/lib/request-deduplication.js +0 -24
- package/src/lib/schema-operations.js +0 -106
- package/src/lib/sql-error-types.js +0 -14
- package/src/lib/sql-operations.js +0 -647
- package/src/lib/utils.js +0 -6
- package/src/lib/version-utils.js +0 -34
- /package/dist/.next/.next/static/{OUHnbB-64afrrZQL0Srn5 → FEsQ9jwPUQv5A3fwg6Opl}/_buildManifest.js +0 -0
- /package/dist/.next/.next/static/{OUHnbB-64afrrZQL0Srn5 → FEsQ9jwPUQv5A3fwg6Opl}/_ssgManifest.js +0 -0
- /package/dist/.next/.next/static/{ZBTCAyp1kO5FdEHMU-Dw2 → Y9mYu4WBghepP5oT_3OdR}/_buildManifest.js +0 -0
- /package/dist/.next/.next/static/{ZBTCAyp1kO5FdEHMU-Dw2 → Y9mYu4WBghepP5oT_3OdR}/_ssgManifest.js +0 -0
- /package/dist/.next/.next/static/{bHYW7fgIBDF8-Q33_WPoE → ae3gNmoSGm-R5LJC4UagI}/_buildManifest.js +0 -0
- /package/dist/.next/.next/static/{bHYW7fgIBDF8-Q33_WPoE → ae3gNmoSGm-R5LJC4UagI}/_ssgManifest.js +0 -0
- /package/dist/.next/{static/ZBTCAyp1kO5FdEHMU-Dw2 → .next/static/bKfUJeuvAQlBmR3TjrfkH}/_buildManifest.js +0 -0
- /package/dist/.next/{static/ZBTCAyp1kO5FdEHMU-Dw2 → .next/static/bKfUJeuvAQlBmR3TjrfkH}/_ssgManifest.js +0 -0
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
// app/api/migrations/sync-files/route.js
|
|
2
|
-
|
|
3
|
-
import { NextResponse } from 'next/server';
|
|
4
|
-
import { findWranglerConfig } from '@/lib/find-wrangler-config.js';
|
|
5
|
-
import { readFile, readdir } from 'fs/promises';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
import { promisify } from 'util';
|
|
8
|
-
import { exec } from 'child_process';
|
|
9
|
-
import { createChecksum } from '@/lib/migrations';
|
|
10
|
-
|
|
11
|
-
const execAsync = promisify(exec);
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Retrieves the list of migrations recorded in the D1 migrations table.
|
|
15
|
-
*/
|
|
16
|
-
async function getDatabaseMigrations(projectDir, databaseName) {
|
|
17
|
-
try {
|
|
18
|
-
const { stdout } = await execAsync(
|
|
19
|
-
`wrangler d1 execute ${databaseName} --local --command="SELECT name, checksum FROM d1_migrations;" --json`,
|
|
20
|
-
{ cwd: projectDir }
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
const result = JSON.parse(stdout);
|
|
24
|
-
return result.results?.[0]?.results || [];
|
|
25
|
-
} catch (error) {
|
|
26
|
-
console.error('Error getting database migrations:', error);
|
|
27
|
-
throw new Error('Failed to get database migrations');
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Reads migration files from the local migrations directory.
|
|
33
|
-
*/
|
|
34
|
-
async function getMigrationFiles(projectDir) {
|
|
35
|
-
try {
|
|
36
|
-
const migrationsDir = path.join(projectDir, 'migrations');
|
|
37
|
-
const files = await readdir(migrationsDir);
|
|
38
|
-
|
|
39
|
-
const migrations = await Promise.all(
|
|
40
|
-
files
|
|
41
|
-
.filter(f => f.endsWith('.sql'))
|
|
42
|
-
.map(async name => {
|
|
43
|
-
const filePath = path.join(migrationsDir, name);
|
|
44
|
-
const content = await readFile(filePath, 'utf8');
|
|
45
|
-
return {
|
|
46
|
-
name,
|
|
47
|
-
path: filePath,
|
|
48
|
-
content,
|
|
49
|
-
checksum: createChecksum(content)
|
|
50
|
-
};
|
|
51
|
-
})
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
return migrations.sort((a, b) => a.name.localeCompare(b.name));
|
|
55
|
-
} catch (error) {
|
|
56
|
-
if (error.code === 'ENOENT') {
|
|
57
|
-
return [];
|
|
58
|
-
}
|
|
59
|
-
throw new Error('Failed to read migration files');
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Executes a database query via Wrangler.
|
|
65
|
-
*/
|
|
66
|
-
async function executeQuery(projectDir, databaseName, query) {
|
|
67
|
-
try {
|
|
68
|
-
await execAsync(
|
|
69
|
-
`wrangler d1 execute ${databaseName} --local --command="${query.replace(/"/g, '\\"')}"`,
|
|
70
|
-
{ cwd: projectDir }
|
|
71
|
-
);
|
|
72
|
-
return true;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
console.error('Query execution error:', error);
|
|
75
|
-
throw new Error('Failed to execute database query');
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* POST handler to sync migration files with the D1 migrations table.
|
|
81
|
-
* Uses the centralized helper to load the Wrangler configuration.
|
|
82
|
-
*/
|
|
83
|
-
export async function POST(req) {
|
|
84
|
-
try {
|
|
85
|
-
const projectDir = process.env.D1_PROJECT_DIR;
|
|
86
|
-
if (!projectDir) {
|
|
87
|
-
return NextResponse.json({ error: 'Project directory not configured' }, { status: 500 });
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Use the helper to get the Wrangler configuration
|
|
91
|
-
const wranglerResult = await findWranglerConfig(projectDir);
|
|
92
|
-
if (!wranglerResult || !wranglerResult.config) {
|
|
93
|
-
return NextResponse.json({ error: 'Could not find Wrangler configuration' }, { status: 404 });
|
|
94
|
-
}
|
|
95
|
-
const config = wranglerResult.config;
|
|
96
|
-
if (!config.d1_databases?.length) {
|
|
97
|
-
return NextResponse.json({ error: 'No D1 databases configured' }, { status: 404 });
|
|
98
|
-
}
|
|
99
|
-
// Get the first D1 database name from the configuration
|
|
100
|
-
const databaseName = config.d1_databases[0].database_name;
|
|
101
|
-
|
|
102
|
-
// Get migrations from the database and from the local file system
|
|
103
|
-
const [dbMigrations, fileMigrations] = await Promise.all([
|
|
104
|
-
getDatabaseMigrations(projectDir, databaseName),
|
|
105
|
-
getMigrationFiles(projectDir)
|
|
106
|
-
]);
|
|
107
|
-
|
|
108
|
-
const dbMigrationsMap = new Map(dbMigrations.map(m => [m.name, m]));
|
|
109
|
-
const fileMigrationsMap = new Map(fileMigrations.map(m => [m.name, m]));
|
|
110
|
-
|
|
111
|
-
const changes = {
|
|
112
|
-
added: [], // New files to add to d1_migrations
|
|
113
|
-
removed: [], // Records to remove from d1_migrations
|
|
114
|
-
unchanged: [] // Files that match with records
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
// Identify migrations to remove (record exists in DB but file is missing)
|
|
118
|
-
for (const [name] of dbMigrationsMap) {
|
|
119
|
-
if (!fileMigrationsMap.has(name)) {
|
|
120
|
-
changes.removed.push(name);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Remove records for missing files
|
|
125
|
-
if (changes.removed.length > 0) {
|
|
126
|
-
const names = changes.removed.map(name => `'${name}'`).join(',');
|
|
127
|
-
await executeQuery(
|
|
128
|
-
projectDir,
|
|
129
|
-
databaseName,
|
|
130
|
-
`DELETE FROM d1_migrations WHERE name IN (${names});`
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Process each migration file: add new records and mark unchanged
|
|
135
|
-
for (const [name, fileMigration] of fileMigrationsMap) {
|
|
136
|
-
const dbMigration = dbMigrationsMap.get(name);
|
|
137
|
-
|
|
138
|
-
if (!dbMigration) {
|
|
139
|
-
// Add new record for file
|
|
140
|
-
changes.added.push(name);
|
|
141
|
-
await executeQuery(
|
|
142
|
-
projectDir,
|
|
143
|
-
databaseName,
|
|
144
|
-
`INSERT OR REPLACE INTO d1_migrations (name, checksum) VALUES ('${name}', '${fileMigration.checksum}');`
|
|
145
|
-
);
|
|
146
|
-
} else {
|
|
147
|
-
changes.unchanged.push(name);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return NextResponse.json({
|
|
152
|
-
success: true,
|
|
153
|
-
details: {
|
|
154
|
-
added: changes.added,
|
|
155
|
-
removed: changes.removed,
|
|
156
|
-
unchanged: changes.unchanged
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
} catch (error) {
|
|
161
|
-
console.error('Migration file sync error:', error);
|
|
162
|
-
return NextResponse.json({
|
|
163
|
-
error: error.message || 'Failed to sync migrations'
|
|
164
|
-
}, {
|
|
165
|
-
status: 500
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export async function OPTIONS() {
|
|
171
|
-
return new NextResponse(null, { status: 204 });
|
|
172
|
-
}
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
// api/pull-data/confirm/route.js
|
|
2
|
-
|
|
3
|
-
import { NextResponse } from 'next/server';
|
|
4
|
-
import { executeSQL } from '@/lib/sql-operations';
|
|
5
|
-
import { getCurrentSchema } from '@/lib/schema-operations';
|
|
6
|
-
import { findWranglerConfig } from '@/lib/find-wrangler-config.js';
|
|
7
|
-
import logger from '@/lib/logger';
|
|
8
|
-
import {
|
|
9
|
-
fetchRemoteTableData as fetchRemoteDataHelper,
|
|
10
|
-
generateBulkInsertSQL
|
|
11
|
-
} from '@/lib/sql-operations';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Helper to extract the ordering column (primary key) from a table's schema.
|
|
15
|
-
* Assumes that each table’s schema object contains a "columns" array with an "isPrimaryKey" flag.
|
|
16
|
-
*
|
|
17
|
-
* @param {object} tableSchema - Schema info for the table.
|
|
18
|
-
* @returns {string} The column name to use for ordering.
|
|
19
|
-
*/
|
|
20
|
-
function getOrderingColumn(tableSchema) {
|
|
21
|
-
const pkColumn = tableSchema.columns?.find(col => col.isPrimaryKey);
|
|
22
|
-
const orderingCol = pkColumn ? pkColumn.name : 'rowid';
|
|
23
|
-
logger.debug(`Determined ordering column for table ${tableSchema.name || 'unknown'}: ${orderingCol}`);
|
|
24
|
-
return orderingCol;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Computes an insertion order for tables based on their foreign key dependencies.
|
|
29
|
-
* The returned array is sorted so that if table A is referenced by table B,
|
|
30
|
-
* then A appears earlier in the list.
|
|
31
|
-
*
|
|
32
|
-
* @param {Array<object>} tableSchemas - Array of table schema objects.
|
|
33
|
-
* @returns {Array<string>} Ordered array of table names.
|
|
34
|
-
*/
|
|
35
|
-
function computeInsertionOrder(tableSchemas) {
|
|
36
|
-
// Build a dependency graph: key = table name, value = set of tables that this table depends on.
|
|
37
|
-
const graph = {};
|
|
38
|
-
tableSchemas.forEach(table => {
|
|
39
|
-
// Initialize graph for this table.
|
|
40
|
-
graph[table.name] = new Set();
|
|
41
|
-
// Use a regex to find FOREIGN KEY references in the table's SQL.
|
|
42
|
-
// This regex assumes the foreign key clause looks like:
|
|
43
|
-
// FOREIGN KEY (...) REFERENCES referencedTable(...)
|
|
44
|
-
const fkRegex = /FOREIGN KEY\s*\([^)]*\)\s+REFERENCES\s+(\w+)/gi;
|
|
45
|
-
let match;
|
|
46
|
-
while ((match = fkRegex.exec(table.sql)) !== null) {
|
|
47
|
-
const referencedTable = match[1];
|
|
48
|
-
graph[table.name].add(referencedTable);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
logger.debug('Computed dependency graph:', graph);
|
|
52
|
-
|
|
53
|
-
// Topological sort (depth-first search) to compute insertion order.
|
|
54
|
-
const sorted = [];
|
|
55
|
-
const visited = {}; // 'temp' = visiting, 'perm' = done
|
|
56
|
-
|
|
57
|
-
function visit(tableName) {
|
|
58
|
-
if (visited[tableName] === 'temp') {
|
|
59
|
-
throw new Error(`Cycle detected in dependencies at table ${tableName}`);
|
|
60
|
-
}
|
|
61
|
-
if (!visited[tableName]) {
|
|
62
|
-
visited[tableName] = 'temp';
|
|
63
|
-
// Visit all tables that this table depends on.
|
|
64
|
-
graph[tableName].forEach(dep => {
|
|
65
|
-
// Only visit if the dependency is in our graph (i.e. among our tables)
|
|
66
|
-
if (graph[dep]) {
|
|
67
|
-
visit(dep);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
visited[tableName] = 'perm';
|
|
71
|
-
sorted.push(tableName);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
Object.keys(graph).forEach(tableName => {
|
|
76
|
-
if (!visited[tableName]) {
|
|
77
|
-
visit(tableName);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
logger.debug('Computed insertion order (parent tables first):', sorted);
|
|
82
|
-
return sorted;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Applies the data pull by:
|
|
87
|
-
* 1. Fetching the local schema to determine table names and primary keys.
|
|
88
|
-
* 2. Computing the insertion order so that parent tables are inserted before child tables.
|
|
89
|
-
* 3. For each table (in insertion order), fetching the latest 100 records from the remote database.
|
|
90
|
-
* 4. Generating a bulk insert statement and executing it.
|
|
91
|
-
*
|
|
92
|
-
* This function aborts the entire process if any insert fails.
|
|
93
|
-
*
|
|
94
|
-
* @param {string} projectDir
|
|
95
|
-
* @param {string} accountId
|
|
96
|
-
* @param {string} apiToken
|
|
97
|
-
* @param {string} databaseName
|
|
98
|
-
* @returns {object} Details of data pulled.
|
|
99
|
-
*/
|
|
100
|
-
async function applyDataPull(projectDir, accountId, apiToken, databaseName) {
|
|
101
|
-
logger.debug('Starting data pull process...');
|
|
102
|
-
const env = {
|
|
103
|
-
...process.env,
|
|
104
|
-
CLOUDFLARE_API_TOKEN: apiToken,
|
|
105
|
-
CLOUDFLARE_ACCOUNT_ID: accountId
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
logger.debug('Fetching current schema for data pull...');
|
|
109
|
-
const currentSchema = await getCurrentSchema(projectDir, databaseName, env, false);
|
|
110
|
-
// Filter for table objects that have a name.
|
|
111
|
-
const tableSchemas = currentSchema.filter(obj => obj.type === 'table' && obj.name);
|
|
112
|
-
logger.debug('Local table schema for data pull:', tableSchemas.map(t => t.name));
|
|
113
|
-
|
|
114
|
-
// Compute insertion order based on foreign key dependencies.
|
|
115
|
-
const insertionOrder = computeInsertionOrder(tableSchemas);
|
|
116
|
-
logger.debug('Final computed insertion order:', insertionOrder);
|
|
117
|
-
|
|
118
|
-
// Process each table in the computed insertion order.
|
|
119
|
-
for (const tableName of insertionOrder) {
|
|
120
|
-
const table = tableSchemas.find(t => t.name === tableName);
|
|
121
|
-
if (!table) {
|
|
122
|
-
logger.debug(`Skipping table ${tableName} as it is not in the current schema list.`);
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
logger.debug(`Processing table: ${tableName}`);
|
|
126
|
-
const orderingColumn = getOrderingColumn(table);
|
|
127
|
-
logger.debug(`Fetching data for table: ${tableName} ordered by ${orderingColumn}`);
|
|
128
|
-
|
|
129
|
-
// Fetch remote data.
|
|
130
|
-
const rows = await fetchRemoteDataHelper(
|
|
131
|
-
projectDir,
|
|
132
|
-
databaseName,
|
|
133
|
-
tableName,
|
|
134
|
-
env,
|
|
135
|
-
75,
|
|
136
|
-
orderingColumn
|
|
137
|
-
);
|
|
138
|
-
logger.debug(`Fetched ${rows.length} records for table: ${tableName}`);
|
|
139
|
-
|
|
140
|
-
if (rows.length > 0) {
|
|
141
|
-
const insertSQL = generateBulkInsertSQL(tableName, rows);
|
|
142
|
-
logger.debug(`Generated bulk insert SQL for ${tableName}: ${insertSQL}`);
|
|
143
|
-
|
|
144
|
-
try {
|
|
145
|
-
// Execute the insert SQL directly.
|
|
146
|
-
await executeSQL(projectDir, databaseName, insertSQL, env, false);
|
|
147
|
-
logger.debug(`Bulk inserted ${rows.length} records into table: ${tableName}`);
|
|
148
|
-
} catch (insertError) {
|
|
149
|
-
logger.error(`Error inserting data into table ${tableName}:`, insertError);
|
|
150
|
-
// Abort the entire process to preserve data integrity.
|
|
151
|
-
throw new Error(`Data pull failed for table ${tableName}: ${insertError.message}`);
|
|
152
|
-
}
|
|
153
|
-
} else {
|
|
154
|
-
logger.debug(`No data fetched for table: ${tableName}`);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
logger.debug('Data pull process completed successfully.');
|
|
158
|
-
return { message: 'All data pulled successfully.' };
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export async function POST(req) {
|
|
162
|
-
try {
|
|
163
|
-
logger.debug('Received request for data pull.');
|
|
164
|
-
const { accountId, apiToken } = await req.json();
|
|
165
|
-
const projectDir = process.env.D1_PROJECT_DIR;
|
|
166
|
-
if (!projectDir) {
|
|
167
|
-
return NextResponse.json(
|
|
168
|
-
{
|
|
169
|
-
success: false,
|
|
170
|
-
error: {
|
|
171
|
-
type: 'VALIDATION_ERROR',
|
|
172
|
-
message: 'Project directory not configured'
|
|
173
|
-
}
|
|
174
|
-
},
|
|
175
|
-
{ status: 500 }
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
const wranglerResult = await findWranglerConfig(projectDir);
|
|
179
|
-
if (!wranglerResult || !wranglerResult.config) {
|
|
180
|
-
return NextResponse.json(
|
|
181
|
-
{
|
|
182
|
-
success: false,
|
|
183
|
-
error: {
|
|
184
|
-
type: 'VALIDATION_ERROR',
|
|
185
|
-
message: 'Could not find Wrangler configuration'
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
{ status: 400 }
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
|
-
const config = wranglerResult.config;
|
|
192
|
-
if (!config.d1_databases?.length) {
|
|
193
|
-
return NextResponse.json(
|
|
194
|
-
{
|
|
195
|
-
success: false,
|
|
196
|
-
error: {
|
|
197
|
-
type: 'VALIDATION_ERROR',
|
|
198
|
-
message: 'No D1 databases configured'
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
{ status: 400 }
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
const databaseName = config.d1_databases[0].database_name;
|
|
205
|
-
logger.debug(`Initiating data pull for database: ${databaseName}`);
|
|
206
|
-
const result = await applyDataPull(projectDir, accountId, apiToken, databaseName);
|
|
207
|
-
logger.debug('Data pull result:', result);
|
|
208
|
-
return NextResponse.json({
|
|
209
|
-
success: true,
|
|
210
|
-
message: result.message,
|
|
211
|
-
details: result
|
|
212
|
-
});
|
|
213
|
-
} catch (error) {
|
|
214
|
-
logger.error('Pull data confirm error:', error);
|
|
215
|
-
return NextResponse.json(
|
|
216
|
-
{
|
|
217
|
-
success: false,
|
|
218
|
-
error: {
|
|
219
|
-
type: error.type || 'EXECUTION_ERROR',
|
|
220
|
-
message: error.message || 'Failed to apply data pull'
|
|
221
|
-
}
|
|
222
|
-
},
|
|
223
|
-
{ status: 500 }
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export async function OPTIONS() {
|
|
229
|
-
return new NextResponse(null, { status: 204 });
|
|
230
|
-
}
|
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
// api/pull-remote/confirm/route.js
|
|
2
|
-
|
|
3
|
-
import { NextResponse } from 'next/server';
|
|
4
|
-
import { writeFile, mkdir, readdir, unlink } from 'fs/promises';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
import { findWranglerConfig } from '@/lib/find-wrangler-config.js';
|
|
7
|
-
import {
|
|
8
|
-
executeSQL,
|
|
9
|
-
ErrorTypes,
|
|
10
|
-
formatSQLStatement,
|
|
11
|
-
getMigrationFileContent
|
|
12
|
-
} from '@/lib/sql-operations';
|
|
13
|
-
import { getCurrentSchema, analyzeDependencies } from '@/lib/schema-operations';
|
|
14
|
-
import { initMigrationsTable, recordMigration, getRemoteMigrations } from '@/lib/migrations.js';
|
|
15
|
-
import logger from '@/lib/logger';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Applies the pull migrations by:
|
|
19
|
-
* 1. Fetching remote migration metadata.
|
|
20
|
-
* 2. For each remote migration, fetching the migration file content from remote.
|
|
21
|
-
* 3. Staging the migration files.
|
|
22
|
-
* 4. Performing a destructive update on local objects.
|
|
23
|
-
* 5. Resetting the local migrations table.
|
|
24
|
-
* 6. Applying staged migrations sequentially (writing files, recording and executing them).
|
|
25
|
-
* 7. Replacing the local migrations folder with the staged files.
|
|
26
|
-
*
|
|
27
|
-
* This completely replaces the local state with the remote state.
|
|
28
|
-
*
|
|
29
|
-
* @param {string} projectDir - The project directory.
|
|
30
|
-
* @param {string} accountId - Cloudflare account ID.
|
|
31
|
-
* @param {string} apiToken - Cloudflare API token.
|
|
32
|
-
* @param {string} databaseName - The target D1 database name.
|
|
33
|
-
* @returns {object} Details of applied migrations.
|
|
34
|
-
*/
|
|
35
|
-
async function applyPullMigrations(projectDir, accountId, apiToken, databaseName) {
|
|
36
|
-
const env = {
|
|
37
|
-
...process.env,
|
|
38
|
-
CLOUDFLARE_API_TOKEN: apiToken,
|
|
39
|
-
CLOUDFLARE_ACCOUNT_ID: accountId
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
// Define paths for the main migrations folder and a temporary staging folder.
|
|
44
|
-
const migrationsDir = path.join(projectDir, 'migrations');
|
|
45
|
-
const stagingDir = path.join(projectDir, 'migrations-temp');
|
|
46
|
-
|
|
47
|
-
// Ensure both directories exist.
|
|
48
|
-
await mkdir(migrationsDir, { recursive: true });
|
|
49
|
-
await mkdir(stagingDir, { recursive: true });
|
|
50
|
-
|
|
51
|
-
// Clear out any existing files in the staging folder.
|
|
52
|
-
const stagedFilesList = await readdir(stagingDir).catch(() => []);
|
|
53
|
-
await Promise.all(stagedFilesList.map(file => unlink(path.join(stagingDir, file))));
|
|
54
|
-
|
|
55
|
-
// --- STEP 1: Fetch remote migration metadata ---
|
|
56
|
-
logger.debug('Fetching remote migration metadata...');
|
|
57
|
-
const remoteMigrations = await getRemoteMigrations(projectDir, databaseName, env);
|
|
58
|
-
logger.debug('Remote migration metadata:', remoteMigrations);
|
|
59
|
-
|
|
60
|
-
// --- STEP 2: For each remote migration, fetch its file content ---
|
|
61
|
-
logger.debug('Fetching migration file content from remote...');
|
|
62
|
-
const stagedFiles = new Map();
|
|
63
|
-
for (const migration of remoteMigrations) {
|
|
64
|
-
try {
|
|
65
|
-
const migrationFileName = migration.name; // Use the remote migration's name.
|
|
66
|
-
const content = await getMigrationFileContent(projectDir, databaseName, migrationFileName, env);
|
|
67
|
-
stagedFiles.set(migrationFileName, content);
|
|
68
|
-
logger.debug(`Staged migration file: ${migrationFileName}`);
|
|
69
|
-
await writeFile(path.join(stagingDir, migrationFileName), content, 'utf8');
|
|
70
|
-
} catch (fileError) {
|
|
71
|
-
logger.error(`Error fetching content for migration ${migration.name}:`, fileError);
|
|
72
|
-
throw new Error(`Failed to fetch migration file content for ${migration.name}`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// --- STEP 3: Fetch local schema for destructive update ---
|
|
77
|
-
logger.debug('Fetching local schema for destructive update...');
|
|
78
|
-
const currentSchema = await getCurrentSchema(projectDir, databaseName, env, false);
|
|
79
|
-
const tableSchema = currentSchema.filter(obj =>
|
|
80
|
-
obj.type === 'table' && obj.name && obj.name.toLowerCase() !== 'if'
|
|
81
|
-
);
|
|
82
|
-
logger.debug('Local table schema:', tableSchema);
|
|
83
|
-
const dropOrder = tableSchema.length > 0
|
|
84
|
-
? analyzeDependencies(
|
|
85
|
-
tableSchema.map(obj => ({
|
|
86
|
-
tableName: obj.name,
|
|
87
|
-
sql: obj.sql,
|
|
88
|
-
type: obj.type
|
|
89
|
-
}))
|
|
90
|
-
)
|
|
91
|
-
: [];
|
|
92
|
-
logger.debug('Computed drop order:', dropOrder);
|
|
93
|
-
|
|
94
|
-
// --- STEP 4: Destructive update (all-or-nothing) ---
|
|
95
|
-
logger.debug('Beginning destructive update...');
|
|
96
|
-
try {
|
|
97
|
-
logger.debug('Disabling foreign keys...');
|
|
98
|
-
await executeSQL(projectDir, databaseName, 'PRAGMA foreign_keys=OFF;', env, false);
|
|
99
|
-
|
|
100
|
-
// Drop triggers.
|
|
101
|
-
const triggers = currentSchema.filter(obj => obj.type === 'trigger');
|
|
102
|
-
for (const trigger of triggers) {
|
|
103
|
-
logger.debug(`Dropping trigger: ${trigger.name}`);
|
|
104
|
-
await executeSQL(
|
|
105
|
-
projectDir,
|
|
106
|
-
databaseName,
|
|
107
|
-
formatSQLStatement(`DROP TRIGGER IF EXISTS ${trigger.name}`),
|
|
108
|
-
env,
|
|
109
|
-
false
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
// Drop indexes.
|
|
113
|
-
const indexes = currentSchema.filter(obj => obj.type === 'index');
|
|
114
|
-
for (const index of indexes) {
|
|
115
|
-
logger.debug(`Dropping index: ${index.name}`);
|
|
116
|
-
await executeSQL(
|
|
117
|
-
projectDir,
|
|
118
|
-
databaseName,
|
|
119
|
-
formatSQLStatement(`DROP INDEX IF EXISTS ${index.name}`),
|
|
120
|
-
env,
|
|
121
|
-
false
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
// Drop tables in reverse dependency order.
|
|
125
|
-
for (const tableName of dropOrder.reverse()) {
|
|
126
|
-
logger.debug(`Dropping table: ${tableName}`);
|
|
127
|
-
await executeSQL(
|
|
128
|
-
projectDir,
|
|
129
|
-
databaseName,
|
|
130
|
-
formatSQLStatement(`DROP TABLE IF EXISTS ${tableName}`),
|
|
131
|
-
env,
|
|
132
|
-
false
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
logger.debug('Re-enabling foreign keys...');
|
|
136
|
-
await executeSQL(projectDir, databaseName, 'PRAGMA foreign_keys=ON;', env, false);
|
|
137
|
-
} catch (dropError) {
|
|
138
|
-
throw new Error(`Error dropping existing objects: ${dropError.message}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// --- STEP 5: Reset the local migrations table ---
|
|
142
|
-
try {
|
|
143
|
-
logger.debug('Resetting local migrations table...');
|
|
144
|
-
await initMigrationsTable(projectDir, databaseName, 'local', true);
|
|
145
|
-
} catch (initError) {
|
|
146
|
-
throw new Error(`Error resetting migrations table: ${initError.message}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// --- STEP 6: Apply staged migrations ---
|
|
150
|
-
// (Transactions removed because D1 operates in auto-commit mode)
|
|
151
|
-
const appliedPreview = { preview: [] };
|
|
152
|
-
let currentFilename = '';
|
|
153
|
-
try {
|
|
154
|
-
for (const [filename, content] of stagedFiles.entries()) {
|
|
155
|
-
currentFilename = filename;
|
|
156
|
-
logger.debug(`Applying migration: ${filename}`);
|
|
157
|
-
// Write the migration file to the main migrations folder.
|
|
158
|
-
await writeFile(path.join(migrationsDir, filename), content, 'utf8');
|
|
159
|
-
|
|
160
|
-
// Compute a checksum from the content.
|
|
161
|
-
const checksum = require('crypto')
|
|
162
|
-
.createHash('sha256')
|
|
163
|
-
.update(content)
|
|
164
|
-
.digest('hex');
|
|
165
|
-
|
|
166
|
-
logger.debug(`Recording migration: ${filename} with checksum ${checksum}`);
|
|
167
|
-
await recordMigration(
|
|
168
|
-
projectDir,
|
|
169
|
-
databaseName,
|
|
170
|
-
{ name: filename, checksum },
|
|
171
|
-
env,
|
|
172
|
-
false
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
// Execute the migration SQL on the local database.
|
|
176
|
-
await executeSQL(projectDir, databaseName, content, env, false);
|
|
177
|
-
|
|
178
|
-
appliedPreview.preview.push({ migration: filename, action: 'applied' });
|
|
179
|
-
}
|
|
180
|
-
} catch (applyError) {
|
|
181
|
-
throw new Error(`Error applying migration ${currentFilename}: ${applyError.message}`);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// --- STEP 7: Replace main migrations folder with staged files ---
|
|
185
|
-
logger.debug('Replacing main migrations folder with staged files...');
|
|
186
|
-
const mainFiles = await readdir(migrationsDir).catch(() => []);
|
|
187
|
-
if (mainFiles.length) {
|
|
188
|
-
for (const file of mainFiles) {
|
|
189
|
-
logger.debug(`Deleting local migration file: ${file}`);
|
|
190
|
-
await unlink(path.join(migrationsDir, file));
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
for (const [filename, content] of stagedFiles.entries()) {
|
|
194
|
-
logger.debug(`Copying staged file ${filename} to main migrations folder`);
|
|
195
|
-
await writeFile(path.join(migrationsDir, filename), content, 'utf8');
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
logger.debug('Pull migration applied successfully.');
|
|
199
|
-
return {
|
|
200
|
-
appliedMigrations: Array.from(stagedFiles.keys()),
|
|
201
|
-
details: appliedPreview
|
|
202
|
-
};
|
|
203
|
-
} catch (error) {
|
|
204
|
-
logger.error('Pull migration error:', error);
|
|
205
|
-
throw error;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* POST handler for pull-remote confirm.
|
|
211
|
-
* Reads the Wrangler configuration (via findWranglerConfig) to obtain the D1 database name,
|
|
212
|
-
* then applies the pull migrations.
|
|
213
|
-
*/
|
|
214
|
-
export async function POST(req) {
|
|
215
|
-
try {
|
|
216
|
-
const { accountId, apiToken } = await req.json();
|
|
217
|
-
const projectDir = process.env.D1_PROJECT_DIR;
|
|
218
|
-
if (!projectDir) {
|
|
219
|
-
return NextResponse.json({
|
|
220
|
-
success: false,
|
|
221
|
-
error: {
|
|
222
|
-
type: ErrorTypes.VALIDATION_ERROR,
|
|
223
|
-
message: 'Project directory not configured'
|
|
224
|
-
}
|
|
225
|
-
}, { status: 500 });
|
|
226
|
-
}
|
|
227
|
-
const wranglerResult = await findWranglerConfig(projectDir);
|
|
228
|
-
if (!wranglerResult || !wranglerResult.config) {
|
|
229
|
-
return NextResponse.json({
|
|
230
|
-
success: false,
|
|
231
|
-
error: {
|
|
232
|
-
type: ErrorTypes.VALIDATION_ERROR,
|
|
233
|
-
message: 'Could not find Wrangler configuration'
|
|
234
|
-
}
|
|
235
|
-
}, { status: 400 });
|
|
236
|
-
}
|
|
237
|
-
const config = wranglerResult.config;
|
|
238
|
-
if (!config.d1_databases?.length) {
|
|
239
|
-
return NextResponse.json({
|
|
240
|
-
success: false,
|
|
241
|
-
error: {
|
|
242
|
-
type: ErrorTypes.VALIDATION_ERROR,
|
|
243
|
-
message: 'No D1 databases configured'
|
|
244
|
-
}
|
|
245
|
-
}, { status: 400 });
|
|
246
|
-
}
|
|
247
|
-
const databaseName = config.d1_databases[0].database_name;
|
|
248
|
-
const result = await applyPullMigrations(projectDir, accountId, apiToken, databaseName);
|
|
249
|
-
return NextResponse.json({
|
|
250
|
-
success: true,
|
|
251
|
-
message: 'Pull migrations applied successfully',
|
|
252
|
-
details: result
|
|
253
|
-
});
|
|
254
|
-
} catch (error) {
|
|
255
|
-
logger.error('Pull remote confirm error:', error);
|
|
256
|
-
return NextResponse.json({
|
|
257
|
-
success: false,
|
|
258
|
-
error: {
|
|
259
|
-
type: error.type || ErrorTypes.EXECUTION_ERROR,
|
|
260
|
-
message: error.message || 'Failed to apply pull migrations',
|
|
261
|
-
details: error.details || {
|
|
262
|
-
suggestions: [
|
|
263
|
-
'Check your Cloudflare credentials',
|
|
264
|
-
'Verify database permissions',
|
|
265
|
-
'Ensure all migration files are accessible'
|
|
266
|
-
]
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}, { status: 500 });
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* OPTIONS handler for pre-flight requests.
|
|
275
|
-
*/
|
|
276
|
-
export async function OPTIONS() {
|
|
277
|
-
return new NextResponse(null, { status: 204 });
|
|
278
|
-
}
|