Skip to content

Commit

Permalink
Updated migration events based on @radex comments
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashlng1080 committed Dec 2, 2020
1 parent 764d982 commit 3a8a784
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/adapters/sqlite/index.js
Expand Up @@ -49,7 +49,7 @@ export default class SQLiteAdapter implements DatabaseAdapter, SQLDatabaseAdapte

migrations: ?SchemaMigrations

migrationEvents: ?MigrationEvents
_migrationEvents: ?MigrationEvents

_tag: ConnectionTag = connectionTag()

Expand All @@ -66,7 +66,7 @@ export default class SQLiteAdapter implements DatabaseAdapter, SQLDatabaseAdapte
const { dbName, schema, migrations, migrationEvents } = options
this.schema = schema
this.migrations = migrations
this.migrationEvents = migrationEvents
this._migrationEvents = migrationEvents
this._dbName = this._getName(dbName)
this._dispatcherType = getDispatcherType(options)
this._dispatcher = makeDispatcher(this._dispatcherType, this._tag, this._dbName)
Expand Down Expand Up @@ -149,7 +149,9 @@ export default class SQLiteAdapter implements DatabaseAdapter, SQLDatabaseAdapte
`[WatermelonDB][SQLite] Migrating from version ${databaseVersion} to ${this.schema.version}...`,
)

this.migrationEvents?.onStarted?.()
if(this._migrationEvents && this._migrationEvents.onStarted){
this._migrationEvents.onStarted()
}

try {
await toPromise(callback =>
Expand All @@ -162,10 +164,14 @@ export default class SQLiteAdapter implements DatabaseAdapter, SQLDatabaseAdapte
),
)
logger.log('[WatermelonDB][SQLite] Migration successful')
this.migrationEvents?.onSuccess?.()
if(this._migrationEvents && this._migrationEvents.onSuccess){
this._migrationEvents.onSuccess()
}
} catch (error) {
logger.error('[WatermelonDB][SQLite] Migration failed', error)
this.migrationEvents?.onFailure?.(error)
if(this._migrationEvents && this._migrationEvents.onFailure){
this._migrationEvents.onFailure()
}
throw error
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/sqlite/type.js
Expand Up @@ -14,9 +14,9 @@ export type SQLiteArg = string | boolean | number | null
export type SQLiteQuery = [SQL, SQLiteArg[]]

export type MigrationEvents = {
onSuccess?: () => void,
onStarted?: () => void,
onFailure?: (error: string) => void,
onSuccess: () => void,
onStarted: () => void,
onFailure: (error: string) => void,
}

export type SQLiteAdapterOptions = $Exact<{
Expand Down

0 comments on commit 3a8a784

Please sign in to comment.