fix(storage): make sure the storage initialization crashes when db connection can't be established
This commit is contained in:
parent
f6761fe434
commit
f8a5cc728d
3 changed files with 15 additions and 7 deletions
6
.changeset/tasty-bulldogs-guess.md
Normal file
6
.changeset/tasty-bulldogs-guess.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
'@emigrate/postgres': patch
|
||||
'@emigrate/mysql': patch
|
||||
---
|
||||
|
||||
Make sure the storage initialization crashes when a database connection can't be established
|
||||
|
|
@ -171,6 +171,8 @@ export const createMysqlStorage = ({ table = defaultTable, connection }: MysqlSt
|
|||
async initializeStorage() {
|
||||
const pool = getPool(connection);
|
||||
|
||||
await pool.query('SELECT 1');
|
||||
|
||||
try {
|
||||
await initializeTable(pool, table);
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -32,12 +32,12 @@ export type PostgresLoaderOptions = {
|
|||
connection: ConnectionOptions | string;
|
||||
};
|
||||
|
||||
const getPool = (connection: ConnectionOptions | string) => {
|
||||
if (typeof connection === 'string') {
|
||||
return postgres(connection);
|
||||
}
|
||||
const getPool = async (connection: ConnectionOptions | string): Promise<Sql> => {
|
||||
const sql = typeof connection === 'string' ? postgres(connection) : postgres(connection);
|
||||
|
||||
return postgres(connection);
|
||||
await sql`SELECT 1`;
|
||||
|
||||
return sql;
|
||||
};
|
||||
|
||||
const lockMigration = async (sql: Sql, table: string, migration: MigrationMetadata) => {
|
||||
|
|
@ -122,7 +122,7 @@ export const createPostgresStorage = ({
|
|||
}: PostgresStorageOptions): EmigrateStorage => {
|
||||
return {
|
||||
async initializeStorage() {
|
||||
const sql = getPool(connection);
|
||||
const sql = await getPool(connection);
|
||||
|
||||
try {
|
||||
await initializeTable(sql, table);
|
||||
|
|
@ -211,7 +211,7 @@ export const createPostgresLoader = ({ connection }: PostgresLoaderOptions): Loa
|
|||
loadableExtensions: ['.sql'],
|
||||
async loadMigration(migration) {
|
||||
return async () => {
|
||||
const sql = getPool(connection);
|
||||
const sql = await getPool(connection);
|
||||
|
||||
try {
|
||||
// @ts-expect-error The "simple" option is not documented, but it exists
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue