Skip to content

Commit

Permalink
Use local ronin package (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
colodenn authored Jan 7, 2025
1 parent 0236340 commit 3035606
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/commands/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ const apply = async (

try {
const { slug } = await getOrSelectSpaceId(sessionToken, spinner);
const existingModels = await getModels(db, appToken, slug, flags.prod);
const existingModels = await getModels(
db,
appToken ?? sessionToken,
slug,
flags.prod,
);
const protocol = await new Protocol().load(migrationFilePath);
const statements = protocol.getSQLStatements(existingModels);

Expand Down
14 changes: 9 additions & 5 deletions src/utils/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import fs from 'node:fs';
import path from 'node:path';
import { formatCode } from '@/src/utils/format';
import { type Model, type Query, type Statement, Transaction } from '@ronin/compiler';
import { add, alter, create, drop, get, set } from 'ronin';
import { getBatchProxy } from 'ronin/utils';

const localRoninPath = path.join(process.cwd(), 'node_modules', 'ronin');
const ronin = await import(localRoninPath);
const roninUtils = await import(path.join(localRoninPath, 'dist/utils'));

const { add, alter, create, drop, get, set } = ronin;
const { getBatchProxy } = roninUtils;

/**
* Protocol represents a set of database migration queries that can be executed in sequence.
Expand Down Expand Up @@ -45,7 +50,7 @@ export class Protocol {
const queryObjects = await getBatchProxy(
() => queries.map((query) => this.queryToObject(query).query),
{ asyncContext: new (await import('node:async_hooks')).AsyncLocalStorage() },
(queries) => queries,
(queries: Array<Query>) => queries,
);
return queryObjects;
};
Expand Down Expand Up @@ -118,14 +123,13 @@ export default () => [
if (!fs.existsSync(filePath)) {
throw new Error(`Migration protocol file ${filePath} does not exist`);
}

const queries = await import(filePath);
const queryObjects = getBatchProxy(
() => {
return queries.default();
},
{ asyncContext: new (await import('node:async_hooks')).AsyncLocalStorage() },
async (r) => r,
async (r: Array<Query>) => r,
);

this._queries = (await queryObjects).map((query: { query: Query }) => query.query);
Expand Down

0 comments on commit 3035606

Please sign in to comment.