Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow toggling force deploy migrations #228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/opengb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Run Tests
env:
VERBOSE: true
run: cd tests/basic/ && opengb test || docker ps
run: cd tests/basic/ && opengb test --strict-schemas --force-deploy-migrations || docker ps

# Inspect containers running after tests finish
- name: docker ps
Expand Down
8 changes: 7 additions & 1 deletion src/build/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ export interface BuildOpts {
format: Format;
runtime: Runtime;
dbDriver: DbDriver;
autoMigrate: boolean;
/** If true, parse TypeScript to generate JSON schemas to be validated at runtime. */
strictSchemas: boolean;
/** If true, don't run `deno check` on the generated code. */
skipDenoCheck: boolean;
/** If exists, run database migrations. */
migrate?: {
/** If true, run migrations automatically without dev mode. */
forceDeploy: boolean;
};
signal?: AbortSignal;
}

Expand Down
70 changes: 35 additions & 35 deletions src/build/plan/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,50 +213,50 @@ export async function planProjectBuild(

await waitForBuildPromises(buildState);

if (opts.autoMigrate) {
if (opts.migrate) {
// Deploy external migrations in parallel
for (const module of project.modules.values()) {
if (!module.db || !module.registry.isExternal) continue;

const migrations = await glob.glob(resolve(module.path, "db", "migrations", "*", "*.sql"));
buildStep(buildState, {
name: "Migrate Database",
module,
description: "deploy",
condition: {
files: migrations,
},
async build({ signal }) {
await migrateDeploy(project, [module], signal);
},
});
if (module.db && (opts.migrate.forceDeploy || module.registry.isExternal)) {
const migrations = await glob.glob(resolve(module.path, "db", "migrations", "*", "*.sql"));
buildStep(buildState, {
name: "Migrate Database",
module,
description: "deploy",
condition: {
files: migrations,
},
async build({ signal }) {
await migrateDeploy(project, [module], signal);
},
});
}
}

await waitForBuildPromises(buildState);

// Deploy dev migrations one at a time
for (const module of project.modules.values()) {
if (!module.db || module.registry.isExternal) continue;

const migrations = await glob.glob(resolve(module.path, "db", "migrations", "*", "*.sql"));
buildStep(buildState, {
name: "Migrate Database",
module,
description: "develop",
condition: {
files: [resolve(module.path, "db", "schema.prisma"), ...migrations],
},
async build({ signal }) {
await migrateDev(project, [module], {
createOnly: false,
// HACK: Disable lock since running this command in watch does not clear the lock correctly
disableSchemaLock: true,
signal,
});
},
});
if (module.db && !opts.migrate.forceDeploy && !module.registry.isExternal) {
const migrations = await glob.glob(resolve(module.path, "db", "migrations", "*", "*.sql"));
buildStep(buildState, {
name: "Migrate Database",
module,
description: "develop",
condition: {
files: [resolve(module.path, "db", "schema.prisma"), ...migrations],
},
async build({ signal }) {
await migrateDev(project, [module], {
createOnly: false,
// HACK: Disable lock since running this command in watch does not clear the lock correctly
disableSchemaLock: true,
signal,
});
},
});

await waitForBuildPromises(buildState);
await waitForBuildPromises(buildState);
}
}
}
}
7 changes: 6 additions & 1 deletion src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const buildCommand = new Command<GlobalOpts>()
"Automatically migrate the database",
{ default: false },
)
.option("--force-deploy-migrations", "Auto deploy migrations without using development prompt", { default: false })
.option(
"--no-strict-schemas",
"Disable strict schema validation",
Expand Down Expand Up @@ -111,9 +112,13 @@ export const buildCommand = new Command<GlobalOpts>()
format: opts.outputFormat!,
runtime: opts.runtime,
dbDriver: opts.dbDriver!,
autoMigrate: opts.autoMigrate,
strictSchemas: opts.strictSchemas,
skipDenoCheck: false,
migrate: opts.autoMigrate
? {
forceDeploy: opts.forceDeployMigrations,
}
: undefined,
signal,
});
},
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export const devCommand = new Command<GlobalOpts>()
runtime: Runtime.Deno,
format: Format.Native,
dbDriver: DbDriver.NodePostgres,
autoMigrate: true,
strictSchemas: opts.strictSchemas,

// This gets ran on `deno run`
skipDenoCheck: true,

migrate: {
forceDeploy: false,
},
signal,
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/cli/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const testCommand = new Command<GlobalOpts>()
.option("--no-build", "Don't build source files")
.option("--no-check", "Don't check source files before running")
.option("--strict-schemas", "Strictly validate schemas", { default: false })
.option("--force-deploy-migrations", "Auto deploy migrations without using development prompt", { default: false })
.option("--watch", "Automatically rerun tests on changes")
.action(
async (opts, ...modulesFilter: string[]) => {
Expand All @@ -30,12 +31,12 @@ export const testCommand = new Command<GlobalOpts>()
runtime: Runtime.Deno,
format: Format.Native,
dbDriver: DbDriver.NodePostgres,
autoMigrate: true,
strictSchemas: opts.strictSchemas,

// This gets ran on `deno test`
skipDenoCheck: true,

migrate: {
forceDeploy: opts.forceDeployMigrations,
},
signal,
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/template/template_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ Deno.test({
format: Format.Native,
runtime: Runtime.Deno,
dbDriver: DbDriver.NodePostgres,
autoMigrate: false,
skipDenoCheck: false,
strictSchemas: true,
skipDenoCheck: false,
});
},
});
Loading