app/composables/useMigrations.ts

142 lines
No EOL
4.5 KiB
TypeScript

export function useMigrations() {
const CURRENT_SCHEMA_VERSION = "1.1"
const SCHEMA_KEY = "urgent-tools-schema-version"
// Get stored schema version
function getStoredVersion(): string {
if (process.client) {
return localStorage.getItem(SCHEMA_KEY) || "1.0"
}
return "1.0"
}
// Set schema version
function setVersion(version: string) {
if (process.client) {
localStorage.setItem(SCHEMA_KEY, version)
}
}
// Migration functions
const migrations = {
"1.0": () => {
// Initial schema - no migration needed
},
"1.1": () => {
// Add new member needs fields
const membersData = localStorage.getItem("urgent-tools-members")
if (membersData) {
try {
const parsed = JSON.parse(membersData)
if (Array.isArray(parsed.members)) {
// Add default values for new fields
parsed.members = parsed.members.map((member: any) => ({
...member,
minMonthlyNeeds: member.minMonthlyNeeds || 0,
targetMonthlyPay: member.targetMonthlyPay || 0,
externalMonthlyIncome: member.externalMonthlyIncome || 0,
monthlyPayPlanned: member.monthlyPayPlanned || 0,
}))
localStorage.setItem("urgent-tools-members", JSON.stringify(parsed))
}
} catch (error) {
console.warn("Failed to migrate members data:", error)
}
}
// Add new policy fields
const policiesData = localStorage.getItem("urgent-tools-policies")
if (policiesData) {
try {
const parsed = JSON.parse(policiesData)
parsed.operatingMode = parsed.operatingMode || 'minimum'
parsed.payPolicy = parsed.payPolicy || {
relationship: 'equal-pay',
roleBands: []
}
localStorage.setItem("urgent-tools-policies", JSON.stringify(parsed))
} catch (error) {
console.warn("Failed to migrate policies data:", error)
}
}
// DISABLED: No automatic cash balance initialization
// The app should start completely empty - no demo data
//
// Previously this would auto-initialize cash balances, but this
// created unwanted demo data. Users must explicitly set up their data.
//
// const cashData = localStorage.getItem("urgent-tools-cash")
// if (!cashData) {
// localStorage.setItem("urgent-tools-cash", JSON.stringify({
// currentCash: 50000,
// currentSavings: 15000,
// cashEvents: [],
// paymentQueue: []
// }))
// }
}
}
// Run all necessary migrations
function migrate() {
if (!process.client) return
const currentVersion = getStoredVersion()
if (currentVersion === CURRENT_SCHEMA_VERSION) {
return // Already up to date
}
console.log(`Migrating from schema version ${currentVersion} to ${CURRENT_SCHEMA_VERSION}`)
// Run migrations in order
const versions = Object.keys(migrations).sort()
const currentIndex = versions.indexOf(currentVersion)
if (currentIndex === -1) {
console.warn(`Unknown schema version: ${currentVersion}, running all migrations`)
// Run all migrations
versions.forEach(version => {
try {
migrations[version as keyof typeof migrations]()
console.log(`Applied migration: ${version}`)
} catch (error) {
console.error(`Migration ${version} failed:`, error)
}
})
} else {
// Run migrations from current version + 1 to latest
for (let i = currentIndex + 1; i < versions.length; i++) {
const version = versions[i]
try {
migrations[version as keyof typeof migrations]()
console.log(`Applied migration: ${version}`)
} catch (error) {
console.error(`Migration ${version} failed:`, error)
}
}
}
// Update stored version
setVersion(CURRENT_SCHEMA_VERSION)
console.log(`Migration complete. Schema version: ${CURRENT_SCHEMA_VERSION}`)
}
// Check if migration is needed
function needsMigration(): boolean {
// Don't run migrations if data was intentionally cleared
if (process.client && localStorage.getItem('urgent-tools-cleared-flag') === 'true') {
return false
}
return getStoredVersion() !== CURRENT_SCHEMA_VERSION
}
return {
migrate,
needsMigration,
currentVersion: CURRENT_SCHEMA_VERSION,
storedVersion: getStoredVersion()
}
}