Skip to content

Commit 0686733

Browse files
committed
fix(setup): should wait for creating tables
1 parent f7b0fdf commit 0686733

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/core.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ function SqlPouch(opts: OpenDatabaseOptions, cb: (err: any) => void) {
117117

118118
async function setup(callback: (err: any) => void) {
119119
await db.transaction(async (tx) => {
120-
checkEncoding(tx)
121-
fetchVersion(tx)
120+
await Promise.all([checkEncoding(tx), fetchVersion(tx)])
122121
})
123122
callback(null)
124123
}
@@ -133,22 +132,24 @@ function SqlPouch(opts: OpenDatabaseOptions, cb: (err: any) => void) {
133132
const sql = 'SELECT sql FROM sqlite_master WHERE tbl_name = ' + META_STORE
134133
const result = await tx.execute(sql, [])
135134
if (!result.rows?.length) {
136-
onGetVersion(tx, 0)
135+
await onGetVersion(tx, 0)
137136
} else if (!/db_version/.test(result.rows[0]!.sql as string)) {
138-
tx.execute('ALTER TABLE ' + META_STORE + ' ADD COLUMN db_version INTEGER')
139-
onGetVersion(tx, 1)
137+
await tx.execute(
138+
'ALTER TABLE ' + META_STORE + ' ADD COLUMN db_version INTEGER'
139+
)
140+
await onGetVersion(tx, 1)
140141
} else {
141142
const resDBVer = await tx.execute('SELECT db_version FROM ' + META_STORE)
142143
const dbVersion = resDBVer.rows[0]!.db_version as number
143-
onGetVersion(tx, dbVersion)
144+
await onGetVersion(tx, dbVersion)
144145
}
145146
}
146147

147-
function onGetVersion(tx: Transaction, dbVersion: number) {
148+
async function onGetVersion(tx: Transaction, dbVersion: number) {
148149
if (dbVersion === 0) {
149-
createInitialSchema(tx)
150+
await createInitialSchema(tx)
150151
} else {
151-
runMigrations(tx, dbVersion)
152+
await runMigrations(tx, dbVersion)
152153
}
153154
}
154155

0 commit comments

Comments
 (0)