Skip to content

Commit

Permalink
Ensure consistent directory naming (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Dec 19, 2024
1 parent 9094ff3 commit 93aacdc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
31 changes: 24 additions & 7 deletions src/commands/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import type { parseArgs } from 'node:util';
import { readConfig, saveConfig } from '@/src/utils/config';
import { initializeDatabase } from '@/src/utils/database';
import { diffModels } from '@/src/utils/migration';
import { type BaseFlags, getModelDefinitions, logTableDiff } from '@/src/utils/misc';
import {
type BaseFlags,
MODELS_IN_CODE_DIR,
getModelDefinitions,
logTableDiff,
} from '@/src/utils/misc';
import { getModels } from '@/src/utils/model';
import { Protocol } from '@/src/utils/protocol';
import { getSpaces } from '@/src/utils/space';
Expand Down Expand Up @@ -134,7 +139,7 @@ const create = async (
status = 'syncing';
spinner.text = 'Writing migration protocol file';

const migrationsDir = path.join(process.cwd(), 'models/.protocols');
const migrationsDir = path.join(process.cwd(), MODELS_IN_CODE_DIR, '.protocols');
const nextNum = (() => {
if (!fs.existsSync(migrationsDir)) return 1;
const files = fs.readdirSync(migrationsDir);
Expand All @@ -158,11 +163,16 @@ const create = async (

if (flags.apply) {
const statements = protocol.getSQLStatements(existingModels);
const migrationsPath = path.join(process.cwd(), 'models/migrations');
const migrationsPath = path.join(process.cwd(), MODELS_IN_CODE_DIR, 'migrations');
fs.mkdirSync(migrationsPath, { recursive: true });

fs.copyFileSync(
path.join(process.cwd(), 'models/.protocols', `migration-${paddedNum}.ts`),
path.join(
process.cwd(),
MODELS_IN_CODE_DIR,
'.protocols',
`migration-${paddedNum}.ts`,
),
path.join(migrationsPath, `migration-${paddedNum}.ts`),
);

Expand Down Expand Up @@ -197,13 +207,20 @@ const apply = async (
const protocol = await new Protocol().load(migrationFilePath);
const statements = protocol.getSQLStatements(existingModels);

const files = fs.readdirSync(path.join(process.cwd(), 'models/.protocols'));
const files = fs.readdirSync(
path.join(process.cwd(), MODELS_IN_CODE_DIR, '.protocols'),
);
const latestProtocolFile = files.sort().pop() || 'migration';

const migrationsPath = path.join(process.cwd(), 'models/migrations');
const migrationsPath = path.join(process.cwd(), MODELS_IN_CODE_DIR, 'migrations');
fs.copyFileSync(
migrationFilePath ||
path.join(process.cwd(), 'models/.protocols', path.basename(latestProtocolFile)),
path.join(
process.cwd(),
MODELS_IN_CODE_DIR,
'.protocols',
path.basename(latestProtocolFile),
),
path.join(migrationsPath, path.basename(latestProtocolFile)),
);

Expand Down
6 changes: 3 additions & 3 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export const BASE_FLAGS = {
export type BaseFlags = Record<keyof typeof BASE_FLAGS, boolean | undefined>;

/** Directory containing RONIN model definitions */
export const MODELS_IN_CODE_DIR = 'models';
export const MODELS_IN_CODE_DIR = 'schema';

/** Path to the RONIN schema definitions file */
export const MODEL_IN_CODE_PATH = path.resolve(
process.cwd(),
MODELS_IN_CODE_DIR,
'models.ts',
'index.ts',
);

/** Suffix used for temporary RONIN schemas */
Expand Down Expand Up @@ -170,7 +170,7 @@ export const logTableDiff = (tableB: Model, tableA: Model, tableName: string): v
*/
export const getModelDefinitions = async (): Promise<Array<Model>> => {
if (!fs.existsSync(MODEL_IN_CODE_PATH)) {
console.error('Could not find a model definition file models/models.ts');
console.error('Could not find a model definition file schema/index.ts');
process.exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getBatchProxy } from 'ronin/utils';
export class Protocol {
private _queries: Array<Query> = [];
private _roninQueries: Array<string>;
private _protocolDir = `${process.cwd()}/models/.protocols/`;
private _protocolDir = `${process.cwd()}/schema/.protocols/`;

/**
* Creates a new Protocol instance.
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('protocol', () => {
data: string | NodeJS.ArrayBufferView,
) => {
writeFileSyncCalled = true;
expect(path).toBe(`${process.cwd()}/models/.protocols/${fileName}.ts`);
expect(path).toBe(`${process.cwd()}/schema/.protocols/${fileName}.ts`);
expect(data).toContain(queries[0]);
};

Expand Down Expand Up @@ -63,7 +63,7 @@ describe('protocol', () => {

fs.writeFileSync = (path: PathOrFileDescriptor, data: string | ArrayBufferView) => {
writeFileSyncCalled = true;
expect(path).toBe(`${process.cwd()}/models/.protocols/${fileName}.sql`);
expect(path).toBe(`${process.cwd()}/schema/.protocols/${fileName}.sql`);
expect(data).toBe('CREATE SCHEMA my_schema;');
};

Expand Down

0 comments on commit 93aacdc

Please sign in to comment.