Skip to content

Commit

Permalink
Addes support for reading config file in the migration sql generation
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Oct 9, 2024
1 parent e7f16db commit 1598d84
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/packages/pongo/src/commandLine/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const sampleConfig = (collectionNames: string[] = ['users']) => {
const types = collectionNames
.map(
(name) =>
`type ${formatTypeName(name)} = { name: string; description: string; date: Date }`,
`export type ${formatTypeName(name)} = { name: string; description: string; date: Date }`,
)
.join('\n');

Expand Down
26 changes: 19 additions & 7 deletions src/packages/pongo/src/commandLine/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface MigrateRunOptions {
interface MigrateSqlOptions {
print?: boolean;
write?: string;
config?: string;
collection: string[];
}

Expand All @@ -29,7 +30,7 @@ migrateCommand
.command('run')
.description('Run database migrations')
.option(
'-cs, --connectionString <string>',
'-cs, --connection-string <string>',
'Connection string for the database',
)
.option(
Expand All @@ -51,7 +52,8 @@ migrateCommand

if (!connectionString) {
console.error(
'Error: Connection string is required. Provide it either as a "--connectionString" parameter or through the DB_CONNECTION_STRING environment variable.',
'Error: Connection string is required. Provide it either as a "--connection-string" parameter or through the DB_CONNECTION_STRING environment variable.' +
'\nFor instance: --connection-string postgresql://postgres:postgres@localhost:5432/postgres',
);
process.exit(1);
}
Expand Down Expand Up @@ -94,24 +96,34 @@ migrateCommand
},
[] as string[],
)
.option('-f, --config <path>', 'Path to configuration file with Pongo config')
.option('--print', 'Print the SQL to the console (default)', true)
//.option('--write <filename>', 'Write the SQL to a specified file')
.action((options: MigrateSqlOptions) => {
.action(async (options: MigrateSqlOptions) => {
const { collection } = options;

if (!collection) {
let collectionNames: string[];

if (options.config) {
const config = await loadConfigFile(options.config);

collectionNames = config.collections.map((c) => c.name);
} else if (collection) {
collectionNames = collection;
} else {
console.error(
'Error: You need to provide at least one collection name is required. Provide it either as a "col" parameter.',
'Error: You need to provide at least one collection name. Provide it either through "--config" file or as a "--collection" parameter.',
);
process.exit(1);
}

const coreMigrations = migrationTableSchemaComponent.migrations({
connector: 'PostgreSQL:pg',
});
const migrations = [
...coreMigrations,
...collection.flatMap((collectionsName) =>
pongoCollectionSchemaComponent(collectionsName).migrations({
...collectionNames.flatMap((collectionName) =>
pongoCollectionSchemaComponent(collectionName).migrations({
connector: 'PostgreSQL:pg', // TODO: Provide connector here
}),
),
Expand Down

0 comments on commit 1598d84

Please sign in to comment.