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

Added schema components to pongo collecton and database #76

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/packages/dumbo/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './connections';
export * from './execute';
export * from './locks';
export * from './query';
export * from './schema';
export * from './sql';
13 changes: 8 additions & 5 deletions src/packages/dumbo/src/core/schema/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import {
rawSql,
singleOrNull,
sql,
type SQL,
type SQLExecutor,
} from '..';
import { type DatabaseLock, type DatabaseLockOptions, type Dumbo } from '../..';
import { type Dumbo } from '../..';
import type { DatabaseLock, DatabaseLockOptions } from '../locks';

export type MigrationStyle = 'None' | 'CreateOrUpdate';

export type Migration = {
name: string;
sqls: string[];
sqls: SQL[];
};

export type MigrationRecord = {
Expand All @@ -25,7 +27,7 @@ export const MIGRATIONS_LOCK_ID = 999956789;

export type MigratorOptions = {
schema: {
migrationTableSQL: string;
migrationTableSQL: SQL;
};
lock: {
databaseLock: DatabaseLock;
Expand Down Expand Up @@ -100,8 +102,9 @@ const getMigrationHash = async (content: string): Promise<string> => {
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
};

export const combineMigrations = (...migration: Pick<Migration, 'sqls'>[]) =>
migration.flatMap((m) => m.sqls).join('\n');
export const combineMigrations = (
...migration: Pick<Migration, 'sqls'>[]
): SQL => rawSql(migration.flatMap((m) => m.sqls).join('\n'));

const setupMigrationTable = async (
execute: SQLExecutor,
Expand Down
38 changes: 25 additions & 13 deletions src/packages/dumbo/src/core/schema/schemaComponent.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import type { Dumbo } from '../..';
import type { Dumbo, SQL } from '../..';
import {
combineMigrations,
runSQLMigrations,
type Migration,
type MigratorOptions,
} from './migrations';

export type SchemaComponent<ComponentType extends string = string> = {
type: ComponentType;
export type SchemaComponent = {
type: string;
migration: Readonly<Migration>;
sql(): string;
sql: SQL;
print(): void;
migrate(pool: Dumbo, options: MigratorOptions): Promise<void>;
migrate(pool: Dumbo, options?: MigratorOptions): Promise<void>;
};

export type SchemaComponentGroup<ComponentTypeGroup extends string = string> = {
type: ComponentTypeGroup;
export type SchemaComponentGroup = {
type: string;
components: ReadonlyArray<SchemaComponent>;
migrations: ReadonlyArray<Migration>;
sql(): string;
sql: SQL;
print(): void;
migrate(pool: Dumbo, options: MigratorOptions): Promise<void>;
migrate(pool: Dumbo, options?: MigratorOptions): Promise<void>;
};

export const schemaComponent = <ComponentType extends string = string>(
type: ComponentType,
migration: Migration,
): SchemaComponent<ComponentType> => {
): SchemaComponent => {
return {
type,
migration,
sql: () => combineMigrations(migration),
get sql() {
return combineMigrations(migration);
},
print: () => console.log(JSON.stringify(migration)),
migrate: (pool: Dumbo, options: MigratorOptions) =>
runSQLMigrations(pool, [migration], options),
Expand All @@ -42,16 +44,26 @@ export const schemaComponentGroup = <
>(
type: ComponentTypeGroup,
components: SchemaComponent[],
): SchemaComponentGroup<ComponentTypeGroup> => {
): SchemaComponentGroup => {
const migrations = components.map((c) => c.migration);

return {
type,
components,
migrations,
sql: () => combineMigrations(...migrations),
get sql() {
return combineMigrations(...migrations);
},
print: () => console.log(JSON.stringify(migrations)),
migrate: (pool: Dumbo, options: MigratorOptions) =>
runSQLMigrations(pool, migrations, options),
};
};

export type WithSchemaComponent = {
schema: SchemaComponent;
};

export type WithSchemaComponentGroup = {
schema: SchemaComponentGroup;
};
3 changes: 2 additions & 1 deletion src/packages/dumbo/src/postgres/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '../../core/locks';
export * from './connections';
export * from './schema';

export * from './locks';
8 changes: 5 additions & 3 deletions src/packages/dumbo/src/postgres/core/locks/advisoryLocks.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
defaultDatabaseLockOptions,
type AcquireDatabaseLockMode,
type AcquireDatabaseLockOptions,
type DatabaseLock,
type DatabaseLockOptions,
defaultDatabaseLockOptions,
type ReleaseDatabaseLockOptions,
} from '..';
import { single, sql, type SQLExecutor } from '../../../core';
single,
sql,
type SQLExecutor,
} from '../../../core';

export const tryAcquireAdvisoryLock = async (
execute: SQLExecutor,
Expand Down
85 changes: 45 additions & 40 deletions src/packages/dumbo/src/postgres/core/schema/migrations.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import assert from 'assert';
import { after, before, beforeEach, describe, it } from 'node:test';
import { tableExists } from '..';
import { type Dumbo, dumbo } from '../../..';
import { count, rawSql, sql } from '../../../core';
import {
count,
type Migration,
MIGRATIONS_LOCK_ID,
rawSql,
sql,
} from '../../../core';
import { acquireAdvisoryLock, releaseAdvisoryLock } from '../locks';
import { runPostgreSQLMigrations } from './migrations';
import { type Migration, MIGRATIONS_LOCK_ID } from '../../../core/schema';

void describe('Migration Integration Tests', () => {
let pool: Dumbo;
Expand Down Expand Up @@ -37,25 +42,25 @@ void describe('Migration Integration Tests', () => {
const firstMigration: Migration = {
name: 'initial_setup',
sqls: [
`
rawSql(`
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);`,
);`),
],
};

const secondMigration: Migration = {
name: 'add_roles_table',
sqls: [
`
rawSql(`
CREATE TABLE roles (
id SERIAL PRIMARY KEY,
role_name VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);`,
);`),
],
};

Expand All @@ -74,10 +79,10 @@ void describe('Migration Integration Tests', () => {
const migration: Migration = {
name: 'timeout_migration',
sqls: [
`CREATE TABLE timeout_table (
rawSql(`CREATE TABLE timeout_table (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
);`),
],
};

Expand Down Expand Up @@ -115,11 +120,11 @@ void describe('Migration Integration Tests', () => {
const migration: Migration = {
name: 'concurrent_migration',
sqls: [
`
rawSql(`
CREATE TABLE concurrent_table (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
);`),
],
};

Expand Down Expand Up @@ -151,11 +156,11 @@ void describe('Migration Integration Tests', () => {
const migration: Migration = {
name: 'hash_check_migration',
sqls: [
`
rawSql(`
CREATE TABLE hash_table (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
);`),
],
};

Expand Down Expand Up @@ -183,11 +188,11 @@ void describe('Migration Integration Tests', () => {
const migration: Migration = {
name: 'hash_check_migration',
sqls: [
`
rawSql(`
CREATE TABLE hash_table (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
);`),
],
};

Expand All @@ -196,12 +201,12 @@ void describe('Migration Integration Tests', () => {
const modifiedMigration: Migration = {
...migration,
sqls: [
`
CREATE TABLE hash_table (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL,
extra_column INT
);`,
rawSql(`
CREATE TABLE hash_table (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL,
extra_column INT
);`),
],
};

Expand All @@ -222,26 +227,26 @@ void describe('Migration Integration Tests', () => {
const migration: Migration = {
name: 'large_migration',
sqls: [
`
CREATE TABLE large_table_1 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
`
CREATE TABLE large_table_2 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
`
CREATE TABLE large_table_3 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
`
CREATE TABLE large_table_4 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`,
rawSql(`
CREATE TABLE large_table_1 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`),
rawSql(`
CREATE TABLE large_table_2 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`),
rawSql(`
CREATE TABLE large_table_3 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`),
rawSql(`
CREATE TABLE large_table_4 (
id SERIAL PRIMARY KEY,
data TEXT NOT NULL
);`),
],
};

Expand Down
37 changes: 25 additions & 12 deletions src/packages/dumbo/src/postgres/core/schema/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { AdvisoryLock, type DatabaseLockOptions } from '../..';
import type { Dumbo } from '../../..';
import { type Dumbo, type MigratorOptions } from '../../..';
import {
type Migration,
runSQLMigrations,
MIGRATIONS_LOCK_ID,
} from '../../../core/schema';
rawSql,
runSQLMigrations,
type DatabaseLockOptions,
type Migration,
type SQL,
} from '../../../core';
import { AdvisoryLock } from '../locks';

export type PostgreSQLMigratorOptions = {
lock?: {
Expand All @@ -13,30 +16,40 @@ export type PostgreSQLMigratorOptions = {
};
};

export const migrationTableSQL = `
export const migrationTableSQL: SQL = rawSql(`
CREATE TABLE IF NOT EXISTS migrations (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
application VARCHAR(255) NOT NULL DEFAULT 'default',
sql_hash VARCHAR(64) NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
`;
`);

export const postgreSQLMigrationOptions: MigratorOptions = {
schema: {
migrationTableSQL,
},
lock: {
databaseLock: AdvisoryLock,
options: {
lockId: MIGRATIONS_LOCK_ID,
},
},
};

export const runPostgreSQLMigrations = (
pool: Dumbo,
migrations: Migration[],
options?: PostgreSQLMigratorOptions,
): Promise<void> =>
runSQLMigrations(pool, migrations, {
schema: {
migrationTableSQL,
},
...postgreSQLMigrationOptions,
lock: {
databaseLock: AdvisoryLock,
...postgreSQLMigrationOptions.lock,
options: {
...postgreSQLMigrationOptions.lock.options,
...(options ?? {}),
lockId: MIGRATIONS_LOCK_ID,
},
},
});
1 change: 0 additions & 1 deletion src/packages/dumbo/src/postgres/pg/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from '../core/locks';
export * from './connections';
export * from './execute';
Loading