import{r as l,p as r,q as o,w as i,z as e,R as a}from"./vendor.8ed37afc.js";const c=e("div",{class:"markdown-body"},[e("blockquote",null,[e("p",null,[a("Directus allows adding custom migration files that run whenever the "),e("code",null,"directus database migrate:*"),a(" commands are executed. All migrations must reside in the "),e("code",null,"extensions/migrations"),a(" folder.")])]),e("h2",{id:"file-name"},[a("File Name "),e("a",{class:"header-anchor",href:"#file-name"},"#")]),e("p",null,"The file name follows the following structure:"),e("pre",null,[e("code",null,`[identifier]-[name].js
`)]),e("p",null,"for example:"),e("pre",null,[e("code",null,`20201202A-my-custom-migration.js
`)]),e("h2",{id:"structure"},[a("Structure "),e("a",{class:"header-anchor",href:"#structure"},"#")]),e("p",null,[a("Migrations have to export an "),e("code",null,"up"),a(" and a "),e("code",null,"down"),a(" function. These functions get a "),e("a",{href:"http://knexjs.org",target:"_blank",rel:"noopener noreferrer"},"Knex"),a(" instance that can be used to do virtually whatever.")]),e("pre",null,[e("code",{class:"language-js"},[e("span",{class:"hljs-built_in"},"module"),a(`.exports = {
	`),e("span",{class:"hljs-keyword"},"async"),a(),e("span",{class:"hljs-function"},[e("span",{class:"hljs-title"},"up"),a("("),e("span",{class:"hljs-params"},"knex"),a(")")]),a(` {
		`),e("span",{class:"hljs-keyword"},"await"),a(" knex.schema.createTable("),e("span",{class:"hljs-string"},"'test'"),a(", "),e("span",{class:"hljs-function"},[a("("),e("span",{class:"hljs-params"},"table"),a(") =>")]),a(` {
			table.increments();
			table.string(`),e("span",{class:"hljs-string"},"'rijk'"),a(`);
		});
	},

	`),e("span",{class:"hljs-keyword"},"async"),a(),e("span",{class:"hljs-function"},[e("span",{class:"hljs-title"},"down"),a("("),e("span",{class:"hljs-params"},"knex"),a(")")]),a(` {
		`),e("span",{class:"hljs-keyword"},"await"),a(" knex.schema.dropTable("),e("span",{class:"hljs-string"},"'test'"),a(`);
	},
};
`)])]),e("div",{class:"danger hint"},[e("div",{class:"hint-title"},"Danger"),e("p",null,"Seeing that these migrations are a bit of a free-for-all, you can really harm your database. Please make sure you know what you\u2019re doing and backup your database before adding these migrations.")]),e("h2",{id:"migrations-and-directus-schema"},[a("Migrations and Directus schema "),e("a",{class:"header-anchor",href:"#migrations-and-directus-schema"},"#")]),e("p",null,[a("Migrations can be used for managing contents of Directus collections (e.g. initial hydration). In order to do it, you must ensure that schema is up to date before running migrations. One way of achieving it is opting out of default "),e("code",null,"directus bootstrap"),a(" process and running:")]),e("pre",null,[e("code",{class:"language-bash"},[a(`npx directus database install
`),e("span",{class:"hljs-comment"},"# notice that schema is applied before running migrations"),a(`
npx directus schema apply ./path/to/snapshot.yaml
npx directus database migrate:latest
`)])]),e("p",null,[a("You may want to add additional steps to reflect other responsibilities of "),e("code",null,"directus bootstrap"),a(".")])],-1),m={setup(u,{expose:n}){const s={title:"Custom Migrations",modularExtension:!1};return n({frontmatter:s}),(d,p)=>{const t=l("DocsWrapper");return r(),o(t,{frontmatter:s},{default:i(()=>[c]),_:1})}}};export{m as default};
