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

refactor: move migrations to scripts/migrations package #36

Merged
merged 1 commit into from
Nov 21, 2024
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"lint": "turbo run lint",
"lint:fix": "turbo run lint:fix",
"prepare": "husky",
"script:db:migrate": "pnpm run --filter @grants-stack-indexer/scripts script:db:migrate",
"script:db:reset": "pnpm run --filter @grants-stack-indexer/scripts script:db:reset",
"script:db:migrate": "pnpm run --filter @grants-stack-indexer/migrations script:db:migrate",
"script:db:reset": "pnpm run --filter @grants-stack-indexer/migrations script:db:reset",
"start": "turbo run start",
"test": "turbo run test",
"test:cov": "turbo run test:cov",
Expand Down
13 changes: 0 additions & 13 deletions packages/repository/src/db/helpers.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/repository/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./connection.js";
export * from "./provider.js";
3 changes: 0 additions & 3 deletions packages/repository/src/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,3 @@ export {
} from "./internal.js";

export { createKyselyPostgresDb as createKyselyDatabase } from "./internal.js";

export { migrateToLatest, resetDatabase } from "./db/index.js";
export type { MigrationConfig } from "./db/index.js";
1 change: 0 additions & 1 deletion packages/repository/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from "./types/index.js";
export * from "./interfaces/index.js";
export * from "./db/connection.js";
export * from "./repositories/kysely/index.js";
export * from "./db/helpers.js";
export * from "./exceptions/index.js";
36 changes: 20 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
- "apps/*"
- "packages/*"
- "scripts/*"
File renamed without changes.
6 changes: 5 additions & 1 deletion apps/scripts/README.md → scripts/migrations/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# grants-stack-indexer: scripts
# grants-stack-indexer: migrations scripts

This package contains scripts for managing the database schema and migrations.

Expand Down Expand Up @@ -110,3 +110,7 @@ export async function down(db: Kysely<any>): Promise<void> {
- Verify schema consistency

TODO: add E2E tests for the scripts

## References

- [Kysely](https://kysely.dev/)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@grants-stack-indexer/scripts",
"name": "@grants-stack-indexer/migrations",
"version": "0.0.1",
"private": true,
"description": "",
Expand Down Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@grants-stack-indexer/repository": "workspace:*",
"dotenv": "16.4.5",
"kysely": "0.27.4",
"zod": "3.23.8"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from "path";
import { configDotenv } from "dotenv";

import { createKyselyDatabase, migrateToLatest } from "@grants-stack-indexer/repository";
import { createKyselyDatabase } from "@grants-stack-indexer/repository";

import { getDatabaseConfigFromEnv } from "./schemas/index.js";
import { migrateToLatest } from "./utils/index.js";

configDotenv();

Expand Down Expand Up @@ -41,6 +43,10 @@ export const main = async (): Promise<void> => {
const migrationResults = await migrateToLatest({
db,
schema: DATABASE_SCHEMA,
migrationsFolder: path.join(
path.dirname(new URL(import.meta.url).pathname),
"./migrations",
),
});

if (migrationResults && migrationResults?.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Kysely, sql } from "kysely";

import { getSchemaName } from "../db/helpers.js";
import { getSchemaName } from "../utils/index.js";

/**
* The up function is called when you update your database schema to the next version and down when you go back to previous version.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from "path";
import { configDotenv } from "dotenv";

import { createKyselyDatabase, resetDatabase } from "@grants-stack-indexer/repository";
import { createKyselyDatabase } from "@grants-stack-indexer/repository";

import { getDatabaseConfigFromEnv } from "./schemas/index.js";
import { resetDatabase } from "./utils/index.js";

configDotenv();

Expand Down Expand Up @@ -44,6 +46,10 @@ const main = async (): Promise<void> => {
const resetResults = await resetDatabase({
db,
schema: DATABASE_SCHEMA,
migrationsFolder: path.join(
path.dirname(new URL(import.meta.url).pathname),
"./migrations",
),
});

if (resetResults && resetResults?.length > 0) {
Expand Down
1 change: 1 addition & 0 deletions scripts/migrations/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./kysely.js";
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { promises as fs } from "fs";
import * as path from "path";
import { FileMigrationProvider, Kysely, MigrationResult, Migrator, NO_MIGRATIONS } from "kysely";
import {
FileMigrationProvider,
Kysely,
MigrationResult,
Migrator,
NO_MIGRATIONS,
SchemaModule,
} from "kysely";

import { Database } from "./connection.js";

export interface MigrationConfig {
db: Kysely<Database>;
export interface MigrationConfig<T> {
db: Kysely<T>;
schema: string;
migrationsFolder: string;
}

/**
* Since WithSchemaPlugin doesn't work with `sql.table`, we need to get the schema name manually.
* ref: https://github.com/kysely-org/kysely/issues/761
*/
export const getSchemaName = (schema: SchemaModule): string => {
let name = "public";
schema.createTable("test").$call((b) => {
name = b.toOperationNode().table.table.schema?.name ?? "public";
});
return name;
};

/**
* Applies all migrations to the database up to the latest version.
*
Expand All @@ -17,8 +35,8 @@ export interface MigrationConfig {
* @param config.schema - The schema to use for the migrations. Should be the same as the schema used in the Kysely database instance.
* @returns The migration results.
*/
export async function migrateToLatest(
config: MigrationConfig,
export async function migrateToLatest<T>(
config: MigrationConfig<T>,
): Promise<MigrationResult[] | undefined> {
await config.db.schema.createSchema(config.schema).ifNotExists().execute();

Expand All @@ -27,10 +45,7 @@ export async function migrateToLatest(
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: path.join(
path.dirname(new URL(import.meta.url).pathname),
"../migrations",
),
migrationFolder: config.migrationsFolder,
}),
migrationTableSchema: config.schema,
});
Expand Down Expand Up @@ -62,18 +77,15 @@ export async function migrateToLatest(
* @param config.schema - The schema to use for the migrations. Should be the same as the schema used in the Kysely database instance.
* @returns The migration results.
*/
export async function resetDatabase(
config: MigrationConfig,
export async function resetDatabase<T>(
config: MigrationConfig<T>,
): Promise<MigrationResult[] | undefined> {
const migrator = new Migrator({
db: config.db,
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: path.join(
path.dirname(new URL(import.meta.url).pathname),
"../migrations",
),
migrationFolder: config.migrationsFolder,
}),
});

Expand Down
File renamed without changes.
File renamed without changes.