|
@@ -22,11 +22,11 @@ async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
|
|
|
if (!state.isConnected || state.isInternetReachable === false) return;
|
|
|
|
|
|
if (state.isInternetReachable === null) {
|
|
|
- await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
+ await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
const retryState = await NetInfo.fetch();
|
|
|
if (!retryState.isConnected || retryState.isInternetReachable === false) return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
console.log('DB copy start - ' + dbName);
|
|
|
await dbAsset.downloadAsync();
|
|
|
|
|
@@ -39,6 +39,19 @@ async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
|
|
|
return dbUri;
|
|
|
}
|
|
|
|
|
|
+async function isSQLiteFileValid(dbPath: string): Promise<boolean> {
|
|
|
+ try {
|
|
|
+ const db = await SQLite.openDatabaseAsync(dbPath, {
|
|
|
+ useNewConnection: true
|
|
|
+ });
|
|
|
+ const result = await db.getAllAsync(`SELECT name FROM sqlite_master WHERE type='table'`);
|
|
|
+ return Array.isArray(result) && result.length > 0;
|
|
|
+ } catch (error) {
|
|
|
+ console.warn('Invalid SQLite file:', dbPath, error);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export async function openDatabases() {
|
|
|
try {
|
|
|
const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
|
|
@@ -46,59 +59,61 @@ export async function openDatabases() {
|
|
|
await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
|
|
|
}
|
|
|
|
|
|
- const nmRegionsDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, {
|
|
|
- size: true
|
|
|
- });
|
|
|
- if (!nmRegionsDB.exists) {
|
|
|
- await copyDatabaseFile(
|
|
|
- nmRegionsDBname,
|
|
|
- Asset.fromModule(require('../../assets/db/' + nmRegionsDBname))
|
|
|
- );
|
|
|
- }
|
|
|
- if (nmRegionsDB.exists && nmRegionsDB.size == 0) {
|
|
|
- await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname);
|
|
|
+ const nmRegionsPath = sqliteFullPath + DS + nmRegionsDBname;
|
|
|
+ const nmRegionsDB = await FileSystem.getInfoAsync(nmRegionsPath);
|
|
|
+ if (
|
|
|
+ !nmRegionsDB.exists ||
|
|
|
+ nmRegionsDB.size === 0 ||
|
|
|
+ !(await isSQLiteFileValid(nmRegionsDBname))
|
|
|
+ ) {
|
|
|
+ if (nmRegionsDB.exists) {
|
|
|
+ await FileSystem.deleteAsync(nmRegionsPath, { idempotent: true });
|
|
|
+ }
|
|
|
+
|
|
|
await copyDatabaseFile(
|
|
|
nmRegionsDBname,
|
|
|
Asset.fromModule(require('../../assets/db/' + nmRegionsDBname))
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- const darePlacesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + darePlacesDBname, {
|
|
|
- size: true
|
|
|
- });
|
|
|
- if (!darePlacesDB.exists) {
|
|
|
- await copyDatabaseFile(
|
|
|
- darePlacesDBname,
|
|
|
- Asset.fromModule(require('../../assets/db/' + darePlacesDBname))
|
|
|
- );
|
|
|
- }
|
|
|
+ const darePlacesPath = sqliteFullPath + DS + darePlacesDBname;
|
|
|
+ const darePlacesDB = await FileSystem.getInfoAsync(darePlacesPath);
|
|
|
+ if (
|
|
|
+ !darePlacesDB.exists ||
|
|
|
+ darePlacesDB.size === 0 ||
|
|
|
+ !(await isSQLiteFileValid(darePlacesDBname))
|
|
|
+ ) {
|
|
|
+ if (darePlacesDB.exists) {
|
|
|
+ await FileSystem.deleteAsync(darePlacesPath, { idempotent: true });
|
|
|
+ }
|
|
|
|
|
|
- if (darePlacesDB.exists && darePlacesDB.size == 0) {
|
|
|
- await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname);
|
|
|
await copyDatabaseFile(
|
|
|
darePlacesDBname,
|
|
|
Asset.fromModule(require('../../assets/db/' + darePlacesDBname))
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- const countriesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + countriesDBname, {
|
|
|
- size: true
|
|
|
- });
|
|
|
- if (!countriesDB.exists) {
|
|
|
- await copyDatabaseFile(
|
|
|
- countriesDBname,
|
|
|
- Asset.fromModule(require('../../assets/db/' + countriesDBname))
|
|
|
- );
|
|
|
- }
|
|
|
- if (countriesDB.exists && countriesDB.size == 0) {
|
|
|
- await FileSystem.deleteAsync(sqliteFullPath + DS + countriesDBname);
|
|
|
+ const countriesPath = sqliteFullPath + DS + countriesDBname;
|
|
|
+ const countriesDB = await FileSystem.getInfoAsync(countriesPath);
|
|
|
+ if (
|
|
|
+ !countriesDB.exists ||
|
|
|
+ countriesDB.size === 0 ||
|
|
|
+ !(await isSQLiteFileValid(countriesDBname))
|
|
|
+ ) {
|
|
|
+ if (countriesDB.exists) {
|
|
|
+ await FileSystem.deleteAsync(countriesPath, { idempotent: true });
|
|
|
+ }
|
|
|
+
|
|
|
await copyDatabaseFile(
|
|
|
countriesDBname,
|
|
|
Asset.fromModule(require('../../assets/db/' + countriesDBname))
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- const openDatabase = (dbName: string) => SQLite.openDatabaseSync(dbName);
|
|
|
+ const openDatabase = (dbName: string) =>
|
|
|
+ SQLite.openDatabaseSync(dbName, {
|
|
|
+ useNewConnection: true
|
|
|
+ });
|
|
|
db1 = openDatabase(nmRegionsDBname);
|
|
|
db2 = openDatabase(darePlacesDBname);
|
|
|
db3 = openDatabase(countriesDBname);
|
|
@@ -146,7 +161,10 @@ export const getCountriesDatabase = async () => {
|
|
|
return db3;
|
|
|
};
|
|
|
|
|
|
-const openDatabase = (dbName: string) => SQLite.openDatabaseSync(dbName);
|
|
|
+const openDatabase = (dbName: string) =>
|
|
|
+ SQLite.openDatabaseSync(dbName, {
|
|
|
+ useNewConnection: true
|
|
|
+ });
|
|
|
|
|
|
async function refreshNmDatabase() {
|
|
|
try {
|