tinyzoo 0.0.0-stage → 0.2.1
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/LICENSE +10 -0
- package/README.md +46 -2
- package/app/app.config.ts +35 -0
- package/app/assets/brand/characters/bright/bird.svg +1 -0
- package/app/assets/brand/characters/bright/bunny.svg +1 -0
- package/app/assets/brand/characters/bright/elephant.svg +1 -0
- package/app/assets/brand/characters/bright/fox.svg +1 -0
- package/app/assets/brand/characters/bright/frog.svg +1 -0
- package/app/assets/brand/characters/bright/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/bee.svg +1 -0
- package/app/assets/brand/characters/muted/bird.svg +1 -0
- package/app/assets/brand/characters/muted/butterfly.svg +1 -0
- package/app/assets/brand/characters/muted/chick.svg +1 -0
- package/app/assets/brand/characters/muted/cow.svg +1 -0
- package/app/assets/brand/characters/muted/deer.svg +1 -0
- package/app/assets/brand/characters/muted/duck.svg +1 -0
- package/app/assets/brand/characters/muted/elephant.svg +1 -0
- package/app/assets/brand/characters/muted/fox.svg +1 -0
- package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
- package/app/assets/brand/characters/muted/koala.svg +1 -0
- package/app/assets/brand/characters/muted/ladybug.svg +1 -0
- package/app/assets/brand/characters/muted/lion.svg +1 -0
- package/app/assets/brand/characters/muted/mouse.svg +1 -0
- package/app/assets/brand/characters/muted/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/owl.svg +1 -0
- package/app/assets/brand/characters/muted/panda.svg +1 -0
- package/app/assets/brand/characters/muted/penguin.svg +1 -0
- package/app/assets/brand/characters/muted/pig.svg +1 -0
- package/app/assets/brand/characters/muted/poodle.svg +1 -0
- package/app/assets/brand/characters/muted/raccoon.svg +1 -0
- package/app/assets/brand/characters/muted/seal.svg +1 -0
- package/app/assets/brand/characters/muted/sheep.svg +1 -0
- package/app/assets/brand/characters/muted/snail.svg +1 -0
- package/app/assets/brand/characters/muted/turtle.svg +1 -0
- package/app/assets/brand/characters/muted/whale.svg +1 -0
- package/app/assets/brand/logo/favicon.svg +1 -0
- package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
- package/app/assets/brand/logo/logo-primary.svg +1 -0
- package/app/assets/brand/logo/mascot-reversed.svg +1 -0
- package/app/assets/brand/logo/mascot.svg +1 -0
- package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
- package/app/assets/brand/logo/wordmark.svg +1 -0
- package/app/assets/css/cms-admin.css +607 -0
- package/app/components/CmsBlocks.vue +30 -0
- package/app/components/CmsCell.vue +16 -0
- package/app/components/CmsCellBadge.vue +18 -0
- package/app/components/CmsCellBlocks.vue +9 -0
- package/app/components/CmsCellBoolean.vue +12 -0
- package/app/components/CmsCellCode.vue +7 -0
- package/app/components/CmsCellColor.vue +13 -0
- package/app/components/CmsCellDate.vue +10 -0
- package/app/components/CmsCellExcerpt.vue +21 -0
- package/app/components/CmsCellGallery.vue +10 -0
- package/app/components/CmsCellGroup.vue +23 -0
- package/app/components/CmsCellNumber.vue +7 -0
- package/app/components/CmsCellRelation.vue +8 -0
- package/app/components/CmsCellText.vue +12 -0
- package/app/components/CmsCellThumb.vue +18 -0
- package/app/components/CmsConfirmDialog.vue +55 -0
- package/app/components/CmsContent.vue +103 -0
- package/app/components/CmsDayPicker.vue +44 -0
- package/app/components/CmsEmptyState.vue +35 -0
- package/app/components/CmsField.vue +111 -0
- package/app/components/CmsFieldBlocks.vue +353 -0
- package/app/components/CmsFieldBoolean.vue +8 -0
- package/app/components/CmsFieldColor.vue +42 -0
- package/app/components/CmsFieldDate.vue +131 -0
- package/app/components/CmsFieldGallery.vue +114 -0
- package/app/components/CmsFieldGroup.vue +53 -0
- package/app/components/CmsFieldMedia.vue +219 -0
- package/app/components/CmsFieldNumber.vue +11 -0
- package/app/components/CmsFieldRelationMany.vue +54 -0
- package/app/components/CmsFieldRelationOne.vue +47 -0
- package/app/components/CmsFieldRichText.vue +45 -0
- package/app/components/CmsFieldSelect.vue +41 -0
- package/app/components/CmsFieldSlug.vue +46 -0
- package/app/components/CmsFieldText.vue +75 -0
- package/app/components/CmsFormSections.vue +150 -0
- package/app/components/CmsHighlight.vue +22 -0
- package/app/components/CmsImage.vue +39 -0
- package/app/components/CmsLinkPicker.vue +122 -0
- package/app/components/CmsLinkTarget.vue +35 -0
- package/app/components/CmsListFilters.vue +107 -0
- package/app/components/CmsLiveNotice.vue +64 -0
- package/app/components/CmsLoadError.vue +24 -0
- package/app/components/CmsMascot.vue +10 -0
- package/app/components/CmsMediaLibrary.vue +114 -0
- package/app/components/CmsMediaPreview.vue +34 -0
- package/app/components/CmsMediaThumb.vue +22 -0
- package/app/components/CmsMoment.vue +42 -0
- package/app/components/CmsNav.vue +39 -0
- package/app/components/CmsPage.vue +86 -0
- package/app/components/CmsPasswordInput.vue +22 -0
- package/app/components/CmsPresence.vue +29 -0
- package/app/components/CmsPreviewPanel.vue +131 -0
- package/app/components/CmsPublishReview.vue +102 -0
- package/app/components/CmsRevisions.vue +195 -0
- package/app/components/CmsRichTextEditor.vue +290 -0
- package/app/components/CmsSearch.vue +99 -0
- package/app/components/CmsSection.vue +20 -0
- package/app/components/CmsSingletonForm.vue +283 -0
- package/app/components/CmsStatusBadge.vue +21 -0
- package/app/components/CmsTime.vue +16 -0
- package/app/components/CmsTranslationRef.vue +27 -0
- package/app/components/CmsTreeList.vue +453 -0
- package/app/components/CmsUnsavedText.vue +34 -0
- package/app/components/CmsWordmark.vue +10 -0
- package/app/composables/useCmsAdminHead.ts +18 -0
- package/app/composables/useCmsCollab.ts +165 -0
- package/app/composables/useCmsConfirm.ts +39 -0
- package/app/composables/useCmsEntryActions.ts +75 -0
- package/app/composables/useCmsFocus.ts +13 -0
- package/app/composables/useCmsForm.ts +273 -0
- package/app/composables/useCmsI18n.ts +185 -0
- package/app/composables/useCmsLinkTargets.ts +84 -0
- package/app/composables/useCmsLive.ts +199 -0
- package/app/composables/useCmsSearch.ts +62 -0
- package/app/composables/useCmsSeo.ts +99 -0
- package/app/composables/useCmsSignedIn.ts +24 -0
- package/app/composables/useCmsUiLocale.ts +24 -0
- package/app/i18n/cs.ts +781 -0
- package/app/i18n/de.ts +781 -0
- package/app/i18n/en.ts +782 -0
- package/app/i18n/types.ts +22 -0
- package/app/layouts/admin.vue +224 -0
- package/app/layouts/cms-auth.vue +37 -0
- package/app/middleware/cms-api-not-a-page.global.ts +10 -0
- package/app/middleware/cms-auth.global.ts +16 -0
- package/app/pages/admin/[entity]/[id].vue +1109 -0
- package/app/pages/admin/[entity]/index.vue +733 -0
- package/app/pages/admin/account.vue +117 -0
- package/app/pages/admin/forgot.vue +50 -0
- package/app/pages/admin/index.vue +184 -0
- package/app/pages/admin/invite/[token].vue +73 -0
- package/app/pages/admin/login.vue +84 -0
- package/app/pages/admin/media.vue +494 -0
- package/app/pages/admin/privacy.vue +193 -0
- package/app/pages/admin/reset/[token].vue +52 -0
- package/app/pages/admin/setup.vue +87 -0
- package/app/pages/admin/styleguide.vue +261 -0
- package/app/pages/admin/team.vue +258 -0
- package/app/plugins/cmsLivePreview.client.ts +20 -0
- package/app/plugins/cmsSignedOut.client.ts +31 -0
- package/app/utils/cmsBlockType.ts +11 -0
- package/app/utils/cmsBrand.ts +38 -0
- package/app/utils/cmsDiff.ts +83 -0
- package/app/utils/cmsFetch.ts +7 -0
- package/app/utils/cmsFocusInvalid.ts +22 -0
- package/app/utils/cmsIssues.ts +128 -0
- package/app/utils/cmsJson.ts +28 -0
- package/app/utils/cmsMedia.ts +63 -0
- package/app/utils/cmsSections.ts +154 -0
- package/app/utils/cmsTime.ts +41 -0
- package/cli/backup.ts +713 -0
- package/cli/breaking.ts +174 -0
- package/cli/content-migrate.ts +239 -0
- package/cli/db.ts +86 -0
- package/cli/export.ts +336 -0
- package/cli/generate.ts +146 -0
- package/cli/import.ts +643 -0
- package/cli/index.ts +140 -0
- package/cli/links-convert.ts +291 -0
- package/cli/seed-engine.ts +705 -0
- package/cli/seed.ts +339 -0
- package/cli/status.ts +49 -0
- package/cli/tar.ts +304 -0
- package/cli/transfer.ts +677 -0
- package/cli/up.ts +27 -0
- package/cli/upgrade.ts +60 -0
- package/cli/versions.ts +60 -0
- package/forms/components/CmsForm.vue +186 -0
- package/forms/index.ts +203 -0
- package/forms/messages.ts +104 -0
- package/forms/retention.ts +48 -0
- package/forms/server/form.get.ts +12 -0
- package/forms/server/form.post.ts +109 -0
- package/forms/server/formSettings.ts +12 -0
- package/forms/server/forms.ts +127 -0
- package/module.ts +373 -0
- package/nuxt.config.ts +43 -0
- package/package.json +98 -3
- package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
- package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
- package/server/api/cms/[entity]/[id].delete.ts +52 -0
- package/server/api/cms/[entity]/[id].get.ts +21 -0
- package/server/api/cms/[entity]/[id].patch.ts +116 -0
- package/server/api/cms/[entity]/_export.get.ts +69 -0
- package/server/api/cms/[entity]/_move.post.ts +81 -0
- package/server/api/cms/[entity]/_order.post.ts +49 -0
- package/server/api/cms/[entity]/_tree.get.ts +41 -0
- package/server/api/cms/[entity]/index.get.ts +62 -0
- package/server/api/cms/[entity]/index.post.ts +38 -0
- package/server/api/cms/[entity]/options.get.ts +55 -0
- package/server/api/cms/_auth/accept.post.ts +33 -0
- package/server/api/cms/_auth/forgot.post.ts +39 -0
- package/server/api/cms/_auth/invite/[token].get.ts +18 -0
- package/server/api/cms/_auth/login.post.ts +42 -0
- package/server/api/cms/_auth/logout.post.ts +7 -0
- package/server/api/cms/_auth/me.get.ts +5 -0
- package/server/api/cms/_auth/me.patch.ts +32 -0
- package/server/api/cms/_auth/password.post.ts +29 -0
- package/server/api/cms/_auth/reset.post.ts +34 -0
- package/server/api/cms/_auth/setup.post.ts +46 -0
- package/server/api/cms/_auth/status.get.ts +18 -0
- package/server/api/cms/_collab/session.get.ts +30 -0
- package/server/api/cms/_collab/socket.ts +51 -0
- package/server/api/cms/_collab/unsaved.delete.ts +16 -0
- package/server/api/cms/_graph.get.ts +20 -0
- package/server/api/cms/_links/resolve.post.ts +17 -0
- package/server/api/cms/_links.get.ts +81 -0
- package/server/api/cms/_live.ts +30 -0
- package/server/api/cms/_media/[id]/usage.get.ts +14 -0
- package/server/api/cms/_media/[id].delete.ts +29 -0
- package/server/api/cms/_media/[id].get.ts +13 -0
- package/server/api/cms/_media/[id].patch.ts +64 -0
- package/server/api/cms/_media/exists.post.ts +15 -0
- package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
- package/server/api/cms/_media/index.get.ts +44 -0
- package/server/api/cms/_media/index.post.ts +147 -0
- package/server/api/cms/_media/raw/[...key].get.ts +85 -0
- package/server/api/cms/_media/raw/[...key].head.ts +2 -0
- package/server/api/cms/_overview.get.ts +87 -0
- package/server/api/cms/_preview.post.ts +17 -0
- package/server/api/cms/_privacy/delete.post.ts +14 -0
- package/server/api/cms/_privacy/export.post.ts +25 -0
- package/server/api/cms/_privacy/lookup.post.ts +27 -0
- package/server/api/cms/_search.get.ts +17 -0
- package/server/api/cms/_singleton/[name].get.ts +19 -0
- package/server/api/cms/_singleton/[name].put.ts +76 -0
- package/server/api/cms/_users/[id]/resend.post.ts +25 -0
- package/server/api/cms/_users/[id].delete.ts +21 -0
- package/server/api/cms/_users/[id].patch.ts +45 -0
- package/server/api/cms/_users/index.get.ts +28 -0
- package/server/api/cms/_users/index.post.ts +39 -0
- package/server/api/content/[entity]/[slug].get.ts +74 -0
- package/server/api/content/[entity]/index.get.ts +100 -0
- package/server/api/content/_global/[name].get.ts +23 -0
- package/server/api/content/_search.get.ts +28 -0
- package/server/api/content/_sitemap.get.ts +8 -0
- package/server/api/content/_tree/[entity].get.ts +31 -0
- package/server/api/content-types.ts.get.ts +17 -0
- package/server/api/health/live.get.ts +10 -0
- package/server/api/health/ready.get.ts +21 -0
- package/server/api/openapi.get.ts +48 -0
- package/server/api/openapi.json.get.ts +32 -0
- package/server/db/driver.ts +8 -0
- package/server/mail/mailer.ts +43 -0
- package/server/middleware/cms-0-headers.ts +37 -0
- package/server/middleware/cms-0-migrate-gate.ts +14 -0
- package/server/middleware/cms-csrf.ts +28 -0
- package/server/middleware/cms-limits.ts +20 -0
- package/server/middleware/cms-redirects.ts +21 -0
- package/server/middleware/cms-session.ts +41 -0
- package/server/middleware/cms-z-plugin-access.ts +15 -0
- package/server/plugins/cms-0-config-check.ts +35 -0
- package/server/plugins/cms-changes.ts +53 -0
- package/server/plugins/cms-content-etag.ts +21 -0
- package/server/plugins/cms-errors.ts +45 -0
- package/server/plugins/cms-migrate.ts +5 -0
- package/server/plugins/cms-plugins.ts +17 -0
- package/server/plugins/cms-search.ts +12 -0
- package/server/plugins/cms-setup-code.ts +15 -0
- package/server/routes/sitemap.xml.get.ts +25 -0
- package/server/store/redis.ts +95 -0
- package/server/store/types.ts +21 -0
- package/server/tasks/cms/retention.ts +10 -0
- package/server/tasks/cms/scheduled.ts +10 -0
- package/server/tasks/cms/search-reindex.ts +12 -0
- package/server/utils/cmsAudit.ts +29 -0
- package/server/utils/cmsAuth.ts +143 -0
- package/server/utils/cmsBlocks.ts +47 -0
- package/server/utils/cmsChanges.ts +222 -0
- package/server/utils/cmsCollab.ts +286 -0
- package/server/utils/cmsCollabServer.ts +451 -0
- package/server/utils/cmsCollate.ts +27 -0
- package/server/utils/cmsContent.ts +376 -0
- package/server/utils/cmsCrud.ts +361 -0
- package/server/utils/cmsCsv.ts +24 -0
- package/server/utils/cmsDb.ts +23 -0
- package/server/utils/cmsDemo.ts +36 -0
- package/server/utils/cmsEmails.ts +125 -0
- package/server/utils/cmsErrors.ts +40 -0
- package/server/utils/cmsFold.ts +17 -0
- package/server/utils/cmsImageSizes.ts +51 -0
- package/server/utils/cmsJson.ts +6 -0
- package/server/utils/cmsLinkAddress.ts +50 -0
- package/server/utils/cmsLinks.ts +248 -0
- package/server/utils/cmsListQuery.ts +93 -0
- package/server/utils/cmsLive.ts +393 -0
- package/server/utils/cmsLocales.ts +96 -0
- package/server/utils/cmsMail.ts +28 -0
- package/server/utils/cmsMediaGc.ts +188 -0
- package/server/utils/cmsMediaKeys.ts +62 -0
- package/server/utils/cmsMigrate.ts +86 -0
- package/server/utils/cmsPreviewDraft.ts +72 -0
- package/server/utils/cmsPrivacy.ts +176 -0
- package/server/utils/cmsPublished.ts +57 -0
- package/server/utils/cmsRange.ts +38 -0
- package/server/utils/cmsRateLimit.ts +82 -0
- package/server/utils/cmsRedirects.ts +160 -0
- package/server/utils/cmsRetention.ts +61 -0
- package/server/utils/cmsRevisions.ts +103 -0
- package/server/utils/cmsScheduler.ts +45 -0
- package/server/utils/cmsScrub.ts +107 -0
- package/server/utils/cmsSearch.ts +354 -0
- package/server/utils/cmsSession.ts +102 -0
- package/server/utils/cmsSetupCode.ts +97 -0
- package/server/utils/cmsSitemap.ts +176 -0
- package/server/utils/cmsStorage.ts +580 -0
- package/server/utils/cmsStore.ts +109 -0
- package/server/utils/cmsTiming.ts +14 -0
- package/server/utils/cmsTooMany.ts +9 -0
- package/server/utils/cmsTranslationWrite.ts +186 -0
- package/server/utils/cmsTree.ts +251 -0
- package/server/utils/cmsTreeAddress.ts +65 -0
- package/server/utils/cmsUpload.ts +228 -0
- package/server/utils/cmsWebhooks.ts +104 -0
- package/shared/json.ts +11 -0
- package/shared/richtext-editor.ts +56 -0
- package/shared/richtext-normalize.ts +33 -0
- package/shared/richtext-paste.ts +151 -0
- package/shared/storage-delivery.ts +53 -0
- package/src/blocks.ts +624 -0
- package/src/changes.ts +54 -0
- package/src/content-shape.ts +340 -0
- package/src/deprecate.ts +64 -0
- package/src/descriptor.ts +149 -0
- package/src/docs.ts +27 -0
- package/src/emitter.ts +94 -0
- package/src/entity.ts +624 -0
- package/src/errorReport.ts +41 -0
- package/src/fields.ts +465 -0
- package/src/i18n.ts +26 -0
- package/src/index.ts +54 -0
- package/src/linkref.ts +31 -0
- package/src/links.ts +179 -0
- package/src/locales.ts +39 -0
- package/src/media.ts +48 -0
- package/src/migrate.ts +236 -0
- package/src/openapi.ts +377 -0
- package/src/paths.ts +88 -0
- package/src/plugin.ts +505 -0
- package/src/presets.ts +230 -0
- package/src/richtext.ts +88 -0
- package/src/sdk.ts +181 -0
- package/src/search.ts +312 -0
- package/src/seed.ts +225 -0
- package/src/setupCode.ts +30 -0
- package/src/system.ts +219 -0
- package/src/tree.ts +421 -0
- package/src/upgrades.ts +137 -0
- package/starter/.env.example +10 -0
- package/starter/README.md +65 -0
- package/starter/app/app.config.ts +10 -0
- package/starter/app/app.vue +12 -0
- package/starter/app/assets/css/main.css +15 -0
- package/starter/app/components/global/BlockContact.vue +15 -0
- package/starter/app/components/global/BlockCta.vue +15 -0
- package/starter/app/components/global/BlockHero.vue +23 -0
- package/starter/app/components/global/BlockImage.vue +13 -0
- package/starter/app/components/global/BlockText.vue +12 -0
- package/starter/app/composables/useSite.ts +77 -0
- package/starter/app/error.vue +28 -0
- package/starter/app/layouts/default.vue +106 -0
- package/starter/app/lib/cms-content.ts +152 -0
- package/starter/app/pages/[...slug].vue +66 -0
- package/starter/app/pages/hledat.vue +59 -0
- package/starter/app/pages/index.vue +20 -0
- package/starter/cms.config.ts +137 -0
- package/starter/compose.yaml +15 -0
- package/starter/drizzle.config.ts +8 -0
- package/starter/gitignore.txt +8 -0
- package/starter/nuxt.config.ts +13 -0
- package/starter/package.json +38 -0
- package/starter/scripts/typecheck.mjs +38 -0
- package/starter/seed/index.ts +297 -0
- package/starter/seed/media/about.jpg +0 -0
- package/starter/seed/media/hero.jpg +0 -0
- package/starter/seed/media/logo.png +0 -0
- package/starter/seed/media/news-bread.jpg +0 -0
- package/starter/seed/media/news-morning.jpg +0 -0
- package/starter/server/db/migrations/0000_init.sql +269 -0
- package/starter/server/db/migrations/0001_page_tree.sql +5 -0
- package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
- package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
- package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
- package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
- package/starter/server/db/migrations/meta/_journal.json +27 -0
- package/starter/server/db/schema.ts +253 -0
- package/starter/server/plugins/sitemap.ts +11 -0
- package/starter/server/routes/robots.txt.get.ts +12 -0
- package/starter/tsconfig.json +9 -0
package/cli/export.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `tinyzoo export <file>` — the site's content in one archive (format: see transfer.ts).
|
|
3
|
+
*
|
|
4
|
+
* tinyzoo export site.tar.gz
|
|
5
|
+
* tinyzoo export site.tar.gz --only post,author --no-media
|
|
6
|
+
*
|
|
7
|
+
* --only a,b only these entities (and the media they use)
|
|
8
|
+
* --no-media media rows without the files (the target already has them)
|
|
9
|
+
* --with-revisions saved versions too
|
|
10
|
+
* --with-submissions form submissions too (personal data: left out by default, and
|
|
11
|
+
* refused by name in --only without this flag)
|
|
12
|
+
* --force overwrite an existing file
|
|
13
|
+
*
|
|
14
|
+
* The file is created readable by its owner only (0600), from the first byte.
|
|
15
|
+
*
|
|
16
|
+
* Everything is read in ONE `REPEATABLE READ` read-only transaction, so the archive is
|
|
17
|
+
* a consistent snapshot even while editors keep saving. Tables are read in pages
|
|
18
|
+
* (keyset by id) and written straight to disk, media files one at a time: memory does
|
|
19
|
+
* not grow with the size of the site.
|
|
20
|
+
*/
|
|
21
|
+
import { createHash } from 'node:crypto'
|
|
22
|
+
import { existsSync } from 'node:fs'
|
|
23
|
+
import { mkdir, mkdtemp, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
24
|
+
import { tmpdir } from 'node:os'
|
|
25
|
+
import { dirname, join, resolve } from 'node:path'
|
|
26
|
+
import { pathToFileURL } from 'node:url'
|
|
27
|
+
import { asc, getTableColumns, gt, inArray } from 'drizzle-orm'
|
|
28
|
+
import { buildRegistry, type CmsEntity, type CmsRegistry } from '../src/index'
|
|
29
|
+
import { type TarEntry, writeTarGz } from './tar'
|
|
30
|
+
import {
|
|
31
|
+
ARCHIVE_FILES, ARCHIVE_FORMAT, BATCH, JsonlWriter, connectDb, dependencyOrder, describeSchema,
|
|
32
|
+
encodeEntry, fingerprintOf, hostSettings, iso, layoutOf, mediaIdsOfEntry, parseArgs, storageFingerprint, storageFromSettings, tinyzooVersion,
|
|
33
|
+
type Manifest, type ManifestEntity, type TransferStorage,
|
|
34
|
+
} from './transfer'
|
|
35
|
+
|
|
36
|
+
export interface ExportOptions {
|
|
37
|
+
reg: CmsRegistry
|
|
38
|
+
databaseUrl: string
|
|
39
|
+
/** Where the media files are read from; not needed with `media: false`. */
|
|
40
|
+
storage?: TransferStorage | null
|
|
41
|
+
file: string
|
|
42
|
+
only?: string[] | null
|
|
43
|
+
/** Include the media files (default true). */
|
|
44
|
+
media?: boolean
|
|
45
|
+
withRevisions?: boolean
|
|
46
|
+
withSubmissions?: boolean
|
|
47
|
+
/**
|
|
48
|
+
* Identifies the storage the files normally live in (`TransferStorage.identity`),
|
|
49
|
+
* recorded as a fingerprint so an import into the same storage can warn. Defaults to
|
|
50
|
+
* `storage.identity`; pass it with `--no-media`, where there is no storage to read.
|
|
51
|
+
*/
|
|
52
|
+
storageIdentity?: string | null
|
|
53
|
+
force?: boolean
|
|
54
|
+
log?: (message: string) => void
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ExportResult {
|
|
58
|
+
file: string
|
|
59
|
+
bytes: number
|
|
60
|
+
manifest: Manifest
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Entities left out of a full export unless asked for, and why. */
|
|
64
|
+
function excludedByDefault(e: CmsEntity): string | null {
|
|
65
|
+
return e.preset === 'form' ? 'form submissions (personal data) — add --with-submissions to include them' : null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Run `fn` over `items` with at most `limit` in flight. */
|
|
69
|
+
async function mapLimit<T>(items: T[], limit: number, fn: (item: T) => Promise<void>): Promise<void> {
|
|
70
|
+
let next = 0
|
|
71
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
72
|
+
while (next < items.length) await fn(items[next++]!)
|
|
73
|
+
}))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function exportContent(opts: ExportOptions): Promise<ExportResult> {
|
|
77
|
+
const { reg } = opts
|
|
78
|
+
const log = opts.log ?? (() => {})
|
|
79
|
+
const withMedia = opts.media !== false
|
|
80
|
+
const file = resolve(opts.file)
|
|
81
|
+
if (existsSync(file) && !opts.force) throw new Error(`${file} already exists (use --force to overwrite it)`)
|
|
82
|
+
if (withMedia && !opts.storage) throw new Error('Media files need storage (set NUXT_CMS_STORAGE_*), or export with --no-media')
|
|
83
|
+
|
|
84
|
+
// What goes in.
|
|
85
|
+
const only = opts.only?.length ? [...new Set(opts.only)] : null
|
|
86
|
+
const excluded: Manifest['excluded'] = []
|
|
87
|
+
let selected: CmsEntity[]
|
|
88
|
+
if (only) {
|
|
89
|
+
const unknown = only.filter((n) => !reg.byName.has(n))
|
|
90
|
+
if (unknown.length) throw new Error(`Unknown content in --only: ${unknown.join(', ')} (this site has: ${reg.entities.map((e) => e.name).join(', ')})`)
|
|
91
|
+
selected = only.map((n) => reg.byName.get(n)!)
|
|
92
|
+
// Asking for a form by name is not asking for its visitors' messages: say so.
|
|
93
|
+
const forms = selected.filter((e) => excludedByDefault(e))
|
|
94
|
+
if (forms.length && !opts.withSubmissions) {
|
|
95
|
+
throw new Error(`${forms.map((e) => e.name).join(', ')} hold${forms.length === 1 ? 's' : ''} form submissions (personal data): add --with-submissions to export them, or leave ${forms.length === 1 ? 'it' : 'them'} out of --only.`)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
selected = []
|
|
100
|
+
for (const e of reg.entities) {
|
|
101
|
+
const why = opts.withSubmissions ? null : excludedByDefault(e)
|
|
102
|
+
if (why) excluded.push({ entity: e.name, reason: why })
|
|
103
|
+
else selected.push(e)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const { order } = dependencyOrder(reg, selected)
|
|
107
|
+
|
|
108
|
+
const tmp = await mkdtemp(join(tmpdir(), 'tinyzoo-export-'))
|
|
109
|
+
const { db, close } = connectDb(opts.databaseUrl, reg)
|
|
110
|
+
try {
|
|
111
|
+
await mkdir(join(tmp, 'content'), { recursive: true })
|
|
112
|
+
await mkdir(join(tmp, 'globals'), { recursive: true })
|
|
113
|
+
const manifestEntities: ManifestEntity[] = []
|
|
114
|
+
const usedMedia = new Set<string>()
|
|
115
|
+
const mediaFiles: string[] = []
|
|
116
|
+
const identity = opts.storageIdentity !== undefined ? opts.storageIdentity : opts.storage?.identity ?? null
|
|
117
|
+
const media: Manifest['media'] = { rows: 0, included: withMedia, files: 0, missing: 0, storage: identity ? storageFingerprint(identity) : null }
|
|
118
|
+
let revisionRows = 0
|
|
119
|
+
|
|
120
|
+
await db.transaction(async (tx: any) => {
|
|
121
|
+
// ── entries ───────────────────────────────────────────────────────────────
|
|
122
|
+
for (const e of order) {
|
|
123
|
+
const layout = layoutOf(reg, e)
|
|
124
|
+
const info: ManifestEntity = { name: e.name, kind: e.singleton ? 'global' : 'collection', rows: 0, translations: 0, file: null }
|
|
125
|
+
const writer = e.singleton ? null : new JsonlWriter(join(tmp, ARCHIVE_FILES.content(e.name)))
|
|
126
|
+
let last: string | undefined
|
|
127
|
+
let globalEntry: unknown = null
|
|
128
|
+
try {
|
|
129
|
+
for (;;) {
|
|
130
|
+
// A global has one row (the oldest, as the admin reads it); a collection is paged by id.
|
|
131
|
+
const rows: any[] = e.singleton
|
|
132
|
+
? await tx.select().from(e.table).orderBy(asc(e.table.createdAt)).limit(1)
|
|
133
|
+
: await tx.select().from(e.table).where(last ? gt(e.table.id, last) : undefined).orderBy(asc(e.table.id)).limit(BATCH)
|
|
134
|
+
if (!rows.length) break
|
|
135
|
+
const ids = rows.map((r) => r.id as string)
|
|
136
|
+
|
|
137
|
+
const translations = new Map<string, any[]>()
|
|
138
|
+
if (e.localesTable && e.localized.length) {
|
|
139
|
+
const t = e.localesTable
|
|
140
|
+
const trs: any[] = await tx.select().from(t).where(inArray(t.parentId, ids)).orderBy(asc(t.parentId), asc(t.locale))
|
|
141
|
+
for (const tr of trs) translations.set(tr.parentId, [...(translations.get(tr.parentId) ?? []), tr])
|
|
142
|
+
}
|
|
143
|
+
// Many-to-many links, by field: every field is written (an empty list is a statement too).
|
|
144
|
+
const links = new Map<string, Map<string, string[]>>()
|
|
145
|
+
for (const { key, junction: j } of layout.m2m) {
|
|
146
|
+
const jrows: any[] = await tx.select().from(j.table).where(inArray(j.table[j.ownerCol], ids))
|
|
147
|
+
const byOwner = new Map<string, string[]>()
|
|
148
|
+
for (const jr of jrows) byOwner.set(jr[j.ownerCol], [...(byOwner.get(jr[j.ownerCol]) ?? []), jr[j.targetCol]])
|
|
149
|
+
for (const list of byOwner.values()) list.sort()
|
|
150
|
+
links.set(key, byOwner)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const row of rows) {
|
|
154
|
+
const trs = translations.get(row.id) ?? []
|
|
155
|
+
const relations = Object.fromEntries(layout.m2m.map(({ key }) => [key, links.get(key)?.get(row.id) ?? []]))
|
|
156
|
+
const entry = encodeEntry(layout, row, trs, relations)
|
|
157
|
+
if (only) for (const id of mediaIdsOfEntry(layout, entry)) usedMedia.add(id)
|
|
158
|
+
info.rows++
|
|
159
|
+
info.translations += trs.length
|
|
160
|
+
if (writer) await writer.write(entry)
|
|
161
|
+
else globalEntry = entry
|
|
162
|
+
}
|
|
163
|
+
if (e.singleton || rows.length < BATCH) break
|
|
164
|
+
last = ids[ids.length - 1]
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
await writer?.close().catch(() => {})
|
|
169
|
+
}
|
|
170
|
+
if (e.singleton) {
|
|
171
|
+
if (globalEntry) {
|
|
172
|
+
info.file = ARCHIVE_FILES.global(e.name)
|
|
173
|
+
await writeFile(join(tmp, info.file), JSON.stringify({ entity: e.name, entry: globalEntry }, null, 2) + '\n')
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else info.file = ARCHIVE_FILES.content(e.name)
|
|
177
|
+
manifestEntities.push(info)
|
|
178
|
+
log(` ${e.name}: ${info.rows} ${e.singleton ? (info.rows ? 'global' : 'globals (not saved yet)') : info.rows === 1 ? 'entry' : 'entries'}${info.translations ? `, ${info.translations} translations` : ''}`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ── media ─────────────────────────────────────────────────────────────────
|
|
182
|
+
const m = reg.cmsMedia
|
|
183
|
+
const mediaCols = Object.keys(getTableColumns(m)).filter((k) => k !== 'url' && k !== 'uploadedBy')
|
|
184
|
+
const writer = new JsonlWriter(join(tmp, ARCHIVE_FILES.media))
|
|
185
|
+
try {
|
|
186
|
+
// With --only, just the pictures and files the exported entries use.
|
|
187
|
+
const wanted = only ? [...usedMedia].sort() : null
|
|
188
|
+
const pages = async function* (): AsyncGenerator<any[]> {
|
|
189
|
+
if (wanted) {
|
|
190
|
+
for (let at = 0; at < wanted.length; at += BATCH) yield await tx.select().from(m).where(inArray(m.id, wanted.slice(at, at + BATCH))).orderBy(asc(m.id))
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
let lastId: string | undefined
|
|
194
|
+
for (;;) {
|
|
195
|
+
const rows: any[] = await tx.select().from(m).where(lastId ? gt(m.id, lastId) : undefined).orderBy(asc(m.id)).limit(BATCH)
|
|
196
|
+
if (!rows.length) return
|
|
197
|
+
yield rows
|
|
198
|
+
if (rows.length < BATCH) return
|
|
199
|
+
lastId = rows[rows.length - 1].id
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
for await (const rows of pages()) {
|
|
203
|
+
const blobs = new Map<string, { size: number; sha256: string } | null>()
|
|
204
|
+
if (withMedia) {
|
|
205
|
+
await mapLimit(rows, 4, async (row) => {
|
|
206
|
+
const blob = await opts.storage!.get(row.key)
|
|
207
|
+
if (!blob) { blobs.set(row.id, null); return }
|
|
208
|
+
const target = join(tmp, ARCHIVE_FILES.mediaDir, row.key)
|
|
209
|
+
await mkdir(dirname(target), { recursive: true })
|
|
210
|
+
await writeFile(target, blob)
|
|
211
|
+
blobs.set(row.id, { size: blob.length, sha256: createHash('sha256').update(blob).digest('hex') })
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
for (const row of rows) {
|
|
215
|
+
const out: Record<string, unknown> = {}
|
|
216
|
+
for (const k of mediaCols) out[k] = iso(row[k])
|
|
217
|
+
const blob = withMedia ? blobs.get(row.id) ?? null : null
|
|
218
|
+
await writer.write({ ...out, blob })
|
|
219
|
+
media.rows++
|
|
220
|
+
if (blob) { media.files++; mediaFiles.push(row.key) }
|
|
221
|
+
else if (withMedia) { media.missing++; log(` ! the file of media ${row.id} (${row.key}) is not in storage`) }
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
await writer.close().catch(() => {})
|
|
227
|
+
}
|
|
228
|
+
log(` media: ${media.rows} rows${withMedia ? `, ${media.files} files` : ' (files left out)'}`)
|
|
229
|
+
|
|
230
|
+
// ── saved versions ────────────────────────────────────────────────────────
|
|
231
|
+
if (opts.withRevisions) {
|
|
232
|
+
const r = reg.cmsRevisions
|
|
233
|
+
const cols = Object.keys(getTableColumns(r)).filter((k) => k !== 'userId')
|
|
234
|
+
const names = selected.map((e) => e.name)
|
|
235
|
+
const rw = new JsonlWriter(join(tmp, ARCHIVE_FILES.revisions))
|
|
236
|
+
try {
|
|
237
|
+
let lastId: string | undefined
|
|
238
|
+
for (;;) {
|
|
239
|
+
const rows: any[] = await tx.select().from(r)
|
|
240
|
+
.where(lastId ? gt(r.id, lastId) : undefined).orderBy(asc(r.id)).limit(BATCH)
|
|
241
|
+
if (!rows.length) break
|
|
242
|
+
for (const row of rows) {
|
|
243
|
+
if (!names.includes(row.entity)) continue
|
|
244
|
+
await rw.write(Object.fromEntries(cols.map((k) => [k, iso(row[k])])))
|
|
245
|
+
revisionRows++
|
|
246
|
+
}
|
|
247
|
+
if (rows.length < BATCH) break
|
|
248
|
+
lastId = rows[rows.length - 1].id
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
finally { await rw.close().catch(() => {}) }
|
|
252
|
+
log(` revisions: ${revisionRows}`)
|
|
253
|
+
}
|
|
254
|
+
}, { isolationLevel: 'repeatable read', accessMode: 'read only' })
|
|
255
|
+
|
|
256
|
+
// ── manifest and the archive ───────────────────────────────────────────────
|
|
257
|
+
const schema = describeSchema(reg)
|
|
258
|
+
const manifest: Manifest = {
|
|
259
|
+
format: ARCHIVE_FORMAT,
|
|
260
|
+
tinyzoo: tinyzooVersion(),
|
|
261
|
+
exportedAt: new Date().toISOString(),
|
|
262
|
+
fingerprint: fingerprintOf(schema),
|
|
263
|
+
schema,
|
|
264
|
+
locales: reg.locales ? { default: reg.locales.default, all: [...reg.locales.locales] } : null,
|
|
265
|
+
only,
|
|
266
|
+
entities: manifestEntities,
|
|
267
|
+
excluded,
|
|
268
|
+
media,
|
|
269
|
+
revisions: { included: !!opts.withRevisions, rows: revisionRows },
|
|
270
|
+
}
|
|
271
|
+
await writeFile(join(tmp, ARCHIVE_FILES.manifest), JSON.stringify(manifest, null, 2) + '\n')
|
|
272
|
+
|
|
273
|
+
const entries: TarEntry[] = [{ name: ARCHIVE_FILES.manifest, path: join(tmp, ARCHIVE_FILES.manifest) }]
|
|
274
|
+
entries.push({ name: ARCHIVE_FILES.media, path: join(tmp, ARCHIVE_FILES.media) })
|
|
275
|
+
for (const key of mediaFiles) entries.push({ name: ARCHIVE_FILES.mediaDir + key, path: join(tmp, ARCHIVE_FILES.mediaDir, key) })
|
|
276
|
+
if (opts.withRevisions) entries.push({ name: ARCHIVE_FILES.revisions, path: join(tmp, ARCHIVE_FILES.revisions) })
|
|
277
|
+
for (const info of manifestEntities) if (info.file) entries.push({ name: info.file, path: join(tmp, info.file) })
|
|
278
|
+
|
|
279
|
+
await mkdir(dirname(file), { recursive: true })
|
|
280
|
+
const partial = `${file}.partial`
|
|
281
|
+
try {
|
|
282
|
+
await writeTarGz(partial, entries) // created 0600
|
|
283
|
+
await rename(partial, file)
|
|
284
|
+
}
|
|
285
|
+
catch (err) {
|
|
286
|
+
await rm(partial, { force: true })
|
|
287
|
+
throw err
|
|
288
|
+
}
|
|
289
|
+
return { file, bytes: (await stat(file)).size, manifest }
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
await close()
|
|
293
|
+
await rm(tmp, { recursive: true, force: true })
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ── command line ────────────────────────────────────────────────────────────────
|
|
298
|
+
|
|
299
|
+
export const EXPORT_USAGE = `usage: tinyzoo export <file.tar.gz> [--only a,b] [--no-media] [--with-revisions] [--with-submissions] [--force]`
|
|
300
|
+
|
|
301
|
+
/** Load the host's cms.config.ts into a registry. */
|
|
302
|
+
export async function loadRegistry(cwd: string): Promise<CmsRegistry> {
|
|
303
|
+
const configPath = resolve(cwd, 'cms.config.ts')
|
|
304
|
+
if (!existsSync(configPath)) throw new Error(`No cms.config.ts in ${cwd} — run this from the website's folder.`)
|
|
305
|
+
return buildRegistry(await import(pathToFileURL(configPath).href))
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export async function runExport(argv: string[], cwd = process.cwd()): Promise<void> {
|
|
309
|
+
const args = parseArgs(argv, ['only', 'database-url', 'uploads'], ['no-media', 'with-revisions', 'with-submissions', 'force'])
|
|
310
|
+
const file = args.positional[0]
|
|
311
|
+
if (!file || args.positional.length > 1) throw new Error(EXPORT_USAGE)
|
|
312
|
+
// The same settings the running app uses: environment, .env, then nuxt.config.
|
|
313
|
+
const settings = await hostSettings(cwd)
|
|
314
|
+
const databaseUrl = args.values['database-url'] ?? (settings.databaseUrl || null)
|
|
315
|
+
if (!databaseUrl) throw new Error('No database: set DATABASE_URL in the environment or the website\'s .env')
|
|
316
|
+
const reg = await loadRegistry(cwd)
|
|
317
|
+
const noMedia = args.flags.has('no-media')
|
|
318
|
+
const override = { dir: args.values.uploads, cwd }
|
|
319
|
+
const storage = noMedia ? null : storageFromSettings(settings.storage, override)
|
|
320
|
+
// With --no-media there is nothing to read, but the archive still says where the files live.
|
|
321
|
+
let storageIdentity: string | null = storage?.identity ?? null
|
|
322
|
+
if (noMedia) { try { storageIdentity = storageFromSettings(settings.storage, override).identity } catch { storageIdentity = null } }
|
|
323
|
+
console.log(`Exporting to ${resolve(cwd, file)}${storage ? ` (media from ${storage.describe})` : ''}`)
|
|
324
|
+
const started = Date.now()
|
|
325
|
+
const { manifest, bytes, file: written } = await exportContent({
|
|
326
|
+
reg, databaseUrl, storage, storageIdentity, file: resolve(cwd, file),
|
|
327
|
+
only: args.values.only ? args.values.only.split(',').map((s) => s.trim()).filter(Boolean) : null,
|
|
328
|
+
media: !noMedia,
|
|
329
|
+
withRevisions: args.flags.has('with-revisions'),
|
|
330
|
+
withSubmissions: args.flags.has('with-submissions'),
|
|
331
|
+
force: args.flags.has('force'),
|
|
332
|
+
log: (m) => console.log(m),
|
|
333
|
+
})
|
|
334
|
+
for (const x of manifest.excluded) console.log(` – left out: ${x.entity}, ${x.reason}`)
|
|
335
|
+
console.log(`✓ ${written} (${(bytes / 1024 / 1024).toFixed(1)} MB, ${((Date.now() - started) / 1000).toFixed(1)} s, fingerprint ${manifest.fingerprint.slice(0, 12)})`)
|
|
336
|
+
}
|
package/cli/generate.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `tinyzoo generate`, after the schema has been emitted: let drizzle-kit write the
|
|
3
|
+
* migration, enable pg_trgm in it when needed, and stop on statements that lose data or
|
|
4
|
+
* may fail on existing rows (see breaking.ts) unless the person confirms them.
|
|
5
|
+
*/
|
|
6
|
+
import { docsUrl } from '../src/docs'
|
|
7
|
+
import { spawn } from 'node:child_process'
|
|
8
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
import { createInterface } from 'node:readline/promises'
|
|
11
|
+
import { isRisky, removeMigrations, scanBreaking, type BreakingChange } from './breaking'
|
|
12
|
+
|
|
13
|
+
export interface DrizzleKitResult {
|
|
14
|
+
ok: boolean
|
|
15
|
+
/** drizzle-kit needed to ask a question (a rename?) and there was no terminal. */
|
|
16
|
+
needsTerminal: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface GenerateOptions {
|
|
20
|
+
/** The host's migrations folder. */
|
|
21
|
+
migrationsDir: string
|
|
22
|
+
/** Keep a migration that loses data (`--allow-drop`). */
|
|
23
|
+
allowDrop: boolean
|
|
24
|
+
/** Keep a migration with statements that may fail on existing rows (`--allow-risky`). */
|
|
25
|
+
allowRisky?: boolean
|
|
26
|
+
/** A person can be asked a question. */
|
|
27
|
+
interactive: boolean
|
|
28
|
+
runDrizzleKit: () => Promise<DrizzleKitResult>
|
|
29
|
+
confirm: (question: string) => Promise<boolean>
|
|
30
|
+
log?: (line: string) => void
|
|
31
|
+
error?: (line: string) => void
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const journalTags = (dir: string): string[] => {
|
|
35
|
+
const file = join(dir, 'meta/_journal.json')
|
|
36
|
+
return existsSync(file) ? (JSON.parse(readFileSync(file, 'utf8')).entries as { tag: string }[]).map((e) => e.tag) : []
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const NEEDS_TERMINAL_MESSAGE = `✗ drizzle-kit needs to ask whether a table or column was renamed or is new, and there is no terminal to answer in.
|
|
40
|
+
Run \`tinyzoo generate\` in a terminal and answer the question, or write the rename into the migration by hand:
|
|
41
|
+
see "Renaming a field" (${docsUrl('renamingField')}) and "Renaming an entity" (${docsUrl('renamingEntity')}).`
|
|
42
|
+
|
|
43
|
+
/** Plain-language report of what a migration would do, for the terminal. */
|
|
44
|
+
export function describeBreaking(file: string, changes: BreakingChange[]): string {
|
|
45
|
+
const risky = changes.filter(isRisky)
|
|
46
|
+
const destructive = changes.filter((c) => !isRisky(c))
|
|
47
|
+
const out = [`${file} would:`, ...changes.map((c) => ` • ${c.message}`)]
|
|
48
|
+
if (destructive.length) {
|
|
49
|
+
out.push(
|
|
50
|
+
'',
|
|
51
|
+
'Losing data cannot be undone, and the version of the site that is still running during a deploy breaks on a dropped or renamed column or table.',
|
|
52
|
+
'Safer: spread it over two releases — first add the new column or table and write to both, drop the old one in the next release.',
|
|
53
|
+
`See "The two-step rule": ${docsUrl('twoStepRule')} (and "Renaming a field", ${docsUrl('renamingField')}, if this is a rename).`,
|
|
54
|
+
)
|
|
55
|
+
// A type change needs the values converted: the recipe has the USING cast.
|
|
56
|
+
if (destructive.some((c) => c.kind === 'change-type')) {
|
|
57
|
+
out.push(`A column that changes type usually needs its values converted: add \`USING "column"::newtype\` to the statement by hand (${docsUrl('changingType')}).`)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (risky.length) {
|
|
61
|
+
out.push(
|
|
62
|
+
'',
|
|
63
|
+
'The statements about required columns, unique values and checks fail on rows that already exist. The new version then cannot finish its migration at start, exits, and is restarted in a loop — the site is down until it is fixed.',
|
|
64
|
+
'Safer: give the field a default, or add it optional first, fill it, then make it required in a later release. See "Adding a required field": ' + docsUrl('requiredField'),
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
return out.join('\n')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Returns the process exit code. */
|
|
71
|
+
export async function generateMigration(opts: GenerateOptions): Promise<number> {
|
|
72
|
+
const log = opts.log ?? console.log
|
|
73
|
+
const error = opts.error ?? console.error
|
|
74
|
+
const journalFile = join(opts.migrationsDir, 'meta/_journal.json')
|
|
75
|
+
const journalBefore = existsSync(journalFile) ? readFileSync(journalFile, 'utf8') : undefined
|
|
76
|
+
const before = new Set(journalTags(opts.migrationsDir))
|
|
77
|
+
|
|
78
|
+
const result = await opts.runDrizzleKit()
|
|
79
|
+
if (result.needsTerminal) { error(NEEDS_TERMINAL_MESSAGE); return 1 }
|
|
80
|
+
if (!result.ok) return 1
|
|
81
|
+
|
|
82
|
+
const added = journalTags(opts.migrationsDir).filter((t) => !before.has(t))
|
|
83
|
+
const changes: { file: string; found: BreakingChange[] }[] = []
|
|
84
|
+
for (const tag of added) {
|
|
85
|
+
const file = join(opts.migrationsDir, `${tag}.sql`)
|
|
86
|
+
let text = readFileSync(file, 'utf8')
|
|
87
|
+
// Trigram indexes (the search index) need pg_trgm, which drizzle-kit does not enable.
|
|
88
|
+
// `tinyzoo up` and the app's start enable it themselves (an upgrade step); this keeps
|
|
89
|
+
// a site that migrates with plain drizzle-kit working too.
|
|
90
|
+
if (text.includes('gin_trgm_ops') && !text.includes('CREATE EXTENSION IF NOT EXISTS pg_trgm')) {
|
|
91
|
+
text = 'CREATE EXTENSION IF NOT EXISTS pg_trgm;--> statement-breakpoint\n' + text
|
|
92
|
+
writeFileSync(file, text)
|
|
93
|
+
log(`✓ ${tag}.sql: enables pg_trgm (for the search index)`)
|
|
94
|
+
}
|
|
95
|
+
const found = scanBreaking(text)
|
|
96
|
+
if (found.length) changes.push({ file: `${tag}.sql`, found })
|
|
97
|
+
}
|
|
98
|
+
if (!changes.length) return 0
|
|
99
|
+
|
|
100
|
+
for (const c of changes) error(`\n⚠ ${describeBreaking(c.file, c.found)}`)
|
|
101
|
+
const all = changes.flatMap((c) => c.found)
|
|
102
|
+
const needsDrop = all.some((c) => !isRisky(c))
|
|
103
|
+
const needsRisky = all.some(isRisky)
|
|
104
|
+
if ((!needsDrop || opts.allowDrop) && (!needsRisky || opts.allowRisky)) {
|
|
105
|
+
log(`\n${[needsDrop && '--allow-drop', needsRisky && '--allow-risky'].filter(Boolean).join(' and ')}: keeping the migration.`)
|
|
106
|
+
return 0
|
|
107
|
+
}
|
|
108
|
+
if (opts.interactive && await opts.confirm('\nKeep the migration anyway?')) return 0
|
|
109
|
+
|
|
110
|
+
removeMigrations(opts.migrationsDir, added, journalBefore)
|
|
111
|
+
error(`\n✗ Removed ${added.map((t) => `${t}.sql`).join(', ')} (with its snapshot and journal entry); nothing was applied.`
|
|
112
|
+
+ (opts.interactive ? '' : ' There is no terminal to ask in.')
|
|
113
|
+
+ '\n Change the schema in two steps'
|
|
114
|
+
+ [needsDrop && ', or run `tinyzoo generate --allow-drop` if losing this is what you want', needsRisky && ', or run `tinyzoo generate --allow-risky` if you know the existing rows satisfy it'].filter(Boolean).join('')
|
|
115
|
+
+ '.')
|
|
116
|
+
return 1
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** drizzle-kit with the terminal when there is one; otherwise output is read to spot its rename question. */
|
|
120
|
+
export function drizzleKitRunner(cwd: string, interactive: boolean): () => Promise<DrizzleKitResult> {
|
|
121
|
+
return () => new Promise((resolve, reject) => {
|
|
122
|
+
const child = spawn('pnpm', ['exec', 'drizzle-kit', 'generate'], { cwd, stdio: interactive ? 'inherit' : ['ignore', 'pipe', 'pipe'] })
|
|
123
|
+
let output = ''
|
|
124
|
+
// Without a terminal drizzle-kit refuses to ask its rename question and says so (and
|
|
125
|
+
// still exits 0): that, not the exit code, is how it is told apart from success. Its
|
|
126
|
+
// output is held back until then, so the stack trace of that refusal is not shown.
|
|
127
|
+
child.stdout?.on('data', (chunk: Buffer) => { output += chunk })
|
|
128
|
+
child.stderr?.on('data', (chunk: Buffer) => { output += chunk })
|
|
129
|
+
child.on('error', reject)
|
|
130
|
+
child.on('exit', (code) => {
|
|
131
|
+
const needsTerminal = /Interactive prompts require a TTY/i.test(output)
|
|
132
|
+
if (!needsTerminal) process.stdout.write(output)
|
|
133
|
+
resolve({ ok: code === 0 && !needsTerminal, needsTerminal })
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function askYesNo(question: string): Promise<boolean> {
|
|
139
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
140
|
+
try {
|
|
141
|
+
return /^y(es)?$/i.test((await rl.question(`${question} [y/N] `)).trim())
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
rl.close()
|
|
145
|
+
}
|
|
146
|
+
}
|