Skip to content

Commit

Permalink
Ensure migration queries are parsed correctly (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
colodenn authored Jan 8, 2025
1 parent e5d2fc5 commit 08de542
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"dependencies": {
"@iarna/toml": "2.2.5",
"@inquirer/prompts": "7.2.1",
"@ronin/compiler": "0.13.3",
"@ronin/compiler": "0.13.9",
"@ronin/engine": "0.0.27",
"chalk-template": "1.1.0",
"get-port": "7.1.0",
Expand All @@ -47,8 +47,8 @@
"prettier": "3.4.2"
},
"devDependencies": {
"ronin": "5.4.0",
"@biomejs/biome": "1.9.4",
"@ronin/schema": "0.1.3",
"@types/bun": "1.1.14",
"@types/ini": "4.1.1",
"bun-bagel": "1.1.0",
Expand Down
6 changes: 4 additions & 2 deletions src/utils/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const fieldsToAdjust = (
name: local.name,
unique: local.unique,
required: local.required,
increment: local.increment,
...(local.type === 'number' && { increment: local.increment }),
};

diff.push(setFieldQuery(tempTableSlug, local.slug, adjustedFieldValue));
Expand Down Expand Up @@ -281,6 +281,8 @@ export const fieldsAreDifferent = (local: ModelField, remote: ModelField): boole
local.unique !== remote.unique ||
local.required !== remote.required ||
local.defaultValue !== remote.defaultValue ||
local.increment !== remote.increment
(local.type === 'number' &&
remote.type === 'number' &&
local.increment !== remote.increment)
);
};
10 changes: 7 additions & 3 deletions src/utils/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Protocol {
const { getBatchProxy } = roninUtils;

const queryObjects = await getBatchProxy(
() => queries.map((query) => this.queryToObject(query, ronin).query),
() => queries.map((query) => this.queryToObject(query, ronin).structure),
{ asyncContext: new (await import('node:async_hooks')).AsyncLocalStorage() },
(queries: Array<Query>) => queries,
);
Expand All @@ -67,7 +67,7 @@ export class Protocol {
private queryToObject = (
query: string,
ronin: typeof Package,
): { query: Query; options: unknown } => {
): { structure: Query; options: unknown } => {
const { add, alter, create, drop, get, set } = ronin;

const func = new Function(
Expand Down Expand Up @@ -130,6 +130,7 @@ export default () => [
if (!fs.existsSync(filePath)) {
throw new Error(`Migration protocol file ${filePath} does not exist`);
}

const queries = await import(filePath);

const roninUtils = await import(path.join(localRoninPath, 'dist/utils'));
Expand All @@ -143,7 +144,10 @@ export default () => [
async (r: Array<Query>) => r,
);

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

return this;
};

Expand Down
14 changes: 7 additions & 7 deletions tests/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Model } from '@ronin/compiler';
import { blob, boolean, date, link, model, number, string } from '@ronin/schema';
import { add } from 'ronin';
import { blob, boolean, date, link, model, number, string } from 'ronin/schema';

const TestAccount = model({
slug: 'account',
Expand Down Expand Up @@ -50,9 +50,9 @@ const TestProfile2 = model({
usernameNew: string({ required: true, unique: true }),
avatar: string(),
account: link({
model: TestAccount2,
target: 'accountNew',
required: true,
actions: { onDelete: 'cascade' },
actions: { onDelete: 'CASCADE' },
}),
},
}) as unknown as Model;
Expand All @@ -63,9 +63,9 @@ const TestProfile = model({
username: string({ required: true, unique: true }),
avatar: string(),
account: link({
model: TestAccount,
target: 'account',
required: true,
actions: { onDelete: 'cascade' },
actions: { onDelete: 'CASCADE' },
}),
},
}) as unknown as Model;
Expand All @@ -74,7 +74,7 @@ const TestBlog = model({
slug: 'blog',
fields: {
name: string(),
author: link({ model: TestProfile, required: true }),
author: link({ target: 'profile', required: true }),
published: boolean({ defaultValue: false }),
hero: blob(),
},
Expand All @@ -91,7 +91,7 @@ const TestBlog2 = model({
slug: 'blog',
fields: {
name: string(),
authorNew: link({ model: TestProfile2, required: true }),
authorNew: link({ target: 'profileNew', required: true }),
published: boolean({ defaultValue: false }),
hero: blob(),
},
Expand Down
10 changes: 5 additions & 5 deletions tests/utils/migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('migration', () => {

expect(modelDiff).toHaveLength(4);
expect(modelDiff).toStrictEqual([
"create.model({slug:'RONIN_TEMP_account',fields:[{slug:'name', unique:false, required:false, type:'string'}]})",
"create.model({slug:'RONIN_TEMP_account',fields:[{slug:'name', type:'string'}]})",
'add.RONIN_TEMP_account.to(() => get.account())',
'drop.model("account")',
'alter.model("RONIN_TEMP_account").to({slug: "account"})',
Expand All @@ -57,7 +57,7 @@ describe('migration', () => {
const modelDiff = await diffModels([Account, Profile], [Account]);
expect(modelDiff).toHaveLength(1);
expect(modelDiff).toStrictEqual([
"create.model({slug:'profile',fields:[{slug:'username', unique:false, required:false, type:'string'}]})",
"create.model({slug:'profile',fields:[{slug:'username', type:'string'}]})",
]);
});

Expand All @@ -75,7 +75,7 @@ describe('migration', () => {

expect(modelDiff).toHaveLength(4);
expect(modelDiff).toStrictEqual([
"create.model({slug:'RONIN_TEMP_account',fields:[{slug:'name', unique:true, required:true, type:'string'}]})",
"create.model({slug:'RONIN_TEMP_account',fields:[{slug:'name', required:true, unique:true, type:'string'}]})",
'add.RONIN_TEMP_account.to(() => get.account())',
'drop.model("account")',
'alter.model("RONIN_TEMP_account").to({slug: "account"})',
Expand All @@ -98,7 +98,7 @@ describe('migration', () => {

expect(modelDiff).toHaveLength(4);
expect(modelDiff).toStrictEqual([
"create.model({slug:'RONIN_TEMP_account',fields:[{slug:'name', unique:false, required:false, type:'string'}]})",
"create.model({slug:'RONIN_TEMP_account',fields:[{slug:'name', type:'string'}]})",
'add.RONIN_TEMP_account.to(() => get.account())',
'drop.model("account")',
'alter.model("RONIN_TEMP_account").to({slug: "account"})',
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('migration', () => {
expect(modelDiff).toHaveLength(2);

expect(modelDiff).toStrictEqual([
"create.model({slug:'comment',fields:[{slug:'name', unique:false, required:false, type:'string'}]})",
"create.model({slug:'comment',fields:[{slug:'name', type:'string'}]})",
'alter.model("comment").create.trigger({"action":"INSERT","when":"BEFORE","effects":[{"add":{"comment":{"to":{"name":"Test"}}}}]})',
]);
});
Expand Down
2 changes: 0 additions & 2 deletions tests/utils/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ describe('misc', () => {
fields: [
{
slug: 'name',
unique: false,
required: false,
type: 'string',
},
],
Expand Down

0 comments on commit 08de542

Please sign in to comment.