Move migrations to db.ts

This commit is contained in:
Brandon Presley 2022-08-30 23:43:39 +12:00
parent 756a2089e9
commit 450d488089
2 changed files with 38 additions and 31 deletions

View File

@ -33,28 +33,7 @@ export default function Routes() {
const dark = useColorScheme() === 'dark';
useEffect(() => {
const init = async () => {
const _db = await getDb();
try {
await _db.executeSql(createPlans);
await _db.executeSql(createSets);
await _db.executeSql(createSettings);
await _db.executeSql(createWorkouts);
await _db.executeSql(addSound).catch(() => null);
await _db.executeSql(addHidden).catch(() => null);
await _db.executeSql(addNotify).catch(() => null);
await _db.executeSql(addImage).catch(() => null);
const [result] = await _db.executeSql(`SELECT * FROM settings LIMIT 1`);
if (result.rows.length === 0)
return _db.executeSql(`
INSERT INTO settings(minutes,seconds,alarm,vibrate,predict,sets)
VALUES(3,30,false,true,true,3);
`);
} finally {
setDb(_db);
}
};
init();
getDb().then(setDb);
}, []);
if (!db) return null;

46
db.ts
View File

@ -1,9 +1,8 @@
import {enablePromise, openDatabase} from 'react-native-sqlite-storage';
enablePromise(true);
export const getDb = () => openDatabase({name: 'massive.db'});
export const createSets = `
const createSets = `
CREATE TABLE IF NOT EXISTS sets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
@ -14,7 +13,7 @@ export const createSets = `
);
`;
export const createPlans = `
const createPlans = `
CREATE TABLE IF NOT EXISTS plans (
id INTEGER PRIMARY KEY AUTOINCREMENT,
days TEXT NOT NULL,
@ -22,7 +21,7 @@ export const createPlans = `
);
`;
export const createSettings = `
const createSettings = `
CREATE TABLE IF NOT EXISTS settings (
minutes INTEGER NOT NULL DEFAULT 3,
seconds INTEGER NOT NULL DEFAULT 30,
@ -33,25 +32,54 @@ export const createSettings = `
);
`;
export const addSound = `
const addSound = `
ALTER TABLE settings ADD COLUMN sound TEXT NULL;
`;
export const createWorkouts = `
const createWorkouts = `
CREATE TABLE IF NOT EXISTS workouts(
name TEXT PRIMARY KEY,
sets INTEGER DEFAULT 3
);
`;
export const addHidden = `
const addHidden = `
ALTER TABLE sets ADD COLUMN hidden DEFAULT false;
`;
export const addNotify = `
const addNotify = `
ALTER TABLE settings ADD COLUMN notify DEFAULT false;
`;
export const addImage = `
const addImage = `
ALTER TABLE sets ADD COLUMN image TEXT NULL;
`;
const addImages = `
ALTER TABLE settings ADD COLUMN images BOOLEAN DEFAULT false;
`;
const selectSettings = `
SELECT * FROM settings LIMIT 1
`;
const insertSettings = `
INSERT INTO settings(minutes,seconds,alarm,vibrate,predict,sets)
VALUES(3,30,false,true,true,3);
`;
export const getDb = async () => {
const db = await openDatabase({name: 'massive.db'});
await db.executeSql(createPlans);
await db.executeSql(createSets);
await db.executeSql(createSettings);
await db.executeSql(createWorkouts);
await db.executeSql(addSound).catch(() => null);
await db.executeSql(addHidden).catch(() => null);
await db.executeSql(addNotify).catch(() => null);
await db.executeSql(addImage).catch(() => null);
await db.executeSql(addImages).catch(() => null);
const [result] = await db.executeSql(selectSettings);
if (result.rows.length === 0) await db.executeSql(insertSettings);
return db;
};