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

experimentalAutoMigrate (draft) #259

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,33 @@ export class Database {
}
}

/** Automatic migrate: Create the given models in the current database, and then sync all fields in the table.
* Does not delete fields.
*
* await db.experimentalAutoMigrate();
*/
async experimentalAutoMigrate() {
const dialect = this.getDialect() as BuiltInDatabaseDialect;

console.log("[experimentalAutoMigrate] dialect:", dialect);

for (const model of this._models) {
try {
console.log('[experimentalAutoMigrate] migrating table', model.table);
await model.createTableOnlyTable();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this could be:

await model.createTable({ withFields: false });

} catch (e) {
console.log('[experimentalAutoMigrate]', e);
}


if (dialect === "mongo") {
throw ("Auto-migration only works on SQL.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the codebase uses Error usually:

throw new Error("Auto-migration only works on SQL.");

}

await model.autoMigrate();
}
}

/** Associate all the required information for a model to connect to a database.
*
* await db.link([Flight, Airport]);
Expand Down