Skip to content

Commit

Permalink
feat: migration helper scripts + docs (wip)
Browse files Browse the repository at this point in the history
Resolves #211
  • Loading branch information
jbmoelker committed Jan 11, 2025
1 parent bac41ed commit 6149e86
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Migrations

**Head Start uses DatoCMS migrations to develop CMS changes in isolation and ensure they can be reproduced.**


Run

```shell
npm run cms:environment:create
```
4 changes: 4 additions & 0 deletions scripts/cms-apply-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { execCommand } from './lib/exec-command';
import { datocmsEnvironment } from '../datocms-environment';

execCommand(`npx datocms migrations:run --destination=${datocmsEnvironment} --fast-fork`);
21 changes: 21 additions & 0 deletions scripts/cms-create-environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { buildClient } from '@datocms/cma-client-node';
import dotenv from 'dotenv-safe';
import { execCommand } from './lib/exec-command';
import { datocmsEnvironment } from '../datocms-environment';

dotenv.config({
allowEmptyValues: Boolean(process.env.CI),
});

const getPrimaryEnvironment = async () => {
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const environments = await client.environments.list();
return environments.find(env => env.meta.primary)?.id;
};

async function run() {
const sourceEnv = await getPrimaryEnvironment();
execCommand(`npx datocms environments:fork ${sourceEnv} ${datocmsEnvironment} --fast`);
}

run();
4 changes: 4 additions & 0 deletions scripts/cms-create-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { execCommand } from './lib/exec-command';
import { datocmsEnvironment } from '../datocms-environment';

execCommand(`npx datocms migrations:new --autogenerate=${datocmsEnvironment} ${datocmsEnvironment}`);
4 changes: 4 additions & 0 deletions scripts/cms-destroy-environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { execCommand } from './lib/exec-command';
import { datocmsEnvironment } from '../datocms-environment';

execCommand(`npx datocms environments:destroy ${datocmsEnvironment}`);
Empty file.
12 changes: 12 additions & 0 deletions scripts/lib/exec-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { exec } from 'node:child_process';
import { promisify } from 'node:util';

export const execCommand = async (command: string) => {
const execAsync = promisify(exec);
try {
const { stdout } = await execAsync(command);
console.log(stdout);
} catch (error) {
console.error(error);
}
};

0 comments on commit 6149e86

Please sign in to comment.