Implement creating new database file

This commit is contained in:
RunasSudo 2025-06-07 13:15:52 +10:00
parent 188b61d3a2
commit 54ef0de03b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
7 changed files with 136 additions and 9 deletions

View File

@ -14,6 +14,8 @@
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
-- Current version: 3 (see db.ts)
---------
-- Tables
@ -93,7 +95,7 @@ CREATE TABLE austax_cgt_cost_adjustments (
description VARCHAR,
cost_adjustment INTEGER,
PRIMARY KEY (id)
)
);
--------
-- Views

View File

@ -28,7 +28,8 @@
"icons/icon.png"
],
"resources": {
"../libdrcr/plugins/": "plugins/"
"../libdrcr/plugins/": "plugins/",
"../schema.sql": "schema.sql"
}
}
}

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -22,8 +22,8 @@
<div class="py-8">
<main>
<div class="mx-auto max-w-7xl px-6 lg:px-8">
<NoFileView v-if="db.filename === null" />
<RouterView v-if="db.filename !== null" />
<NoFileView v-if="!(db.filename !== null || route.name === 'new-file')" />
<RouterView v-if="db.filename !== null || route.name === 'new-file'" />
</div>
</main>
</div>
@ -31,8 +31,11 @@
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
import HeaderBar from './components/HeaderBar.vue';
import { db } from './db.js';
import NoFileView from './pages/NoFileView.vue';
import { db } from './db.js';
const route = useRoute();
</script>

View File

@ -17,14 +17,16 @@
*/
import { invoke } from '@tauri-apps/api/core';
import { resolveResource } from '@tauri-apps/api/path';
import { getCurrentWindow } from '@tauri-apps/api/window';
import { readTextFile } from '@tauri-apps/plugin-fs';
import Database from '@tauri-apps/plugin-sql';
import { reactive } from 'vue';
import { Balance } from './amounts.ts';
import { ExtendedDatabase } from './dbutil.ts';
export const DB_VERSION = 3; // Should match schema.sql
export const DT_FORMAT = 'YYYY-MM-DD HH:mm:ss.SSS000';
export const db = reactive({
@ -60,6 +62,8 @@ export const db = reactive({
this.metadata.reporting_commodity = metadataObject.reporting_commodity;
this.metadata.dps = parseInt(metadataObject.amount_dps);
}
// TODO: Validate database version
},
load: async function(): Promise<ExtendedDatabase> {
@ -67,6 +71,39 @@ export const db = reactive({
},
});
export async function createNewDatabase(filename: string, eofy_date: string, reporting_commodity: string, dps: number) {
// Open new SQLite database
const session = new ExtendedDatabase(await Database.load('sqlite:' + filename));
// Read SQL schema
const schemaPath = await resolveResource('schema.sql');
const schemaSql = await readTextFile(schemaPath);
// Execute SQL
const transaction = await session.begin();
await transaction.execute(schemaSql);
// Init metadata
await transaction.execute(
`INSERT INTO metadata (key, value) VALUES (?, ?)`,
['version', DB_VERSION.toString()] // Manually call .toString() to format as int, otherwise sqlx formats as float
);
await transaction.execute(
`INSERT INTO metadata (key, value) VALUES (?, ?)`,
['eofy_date', eofy_date]
);
await transaction.execute(
`INSERT INTO metadata (key, value) VALUES (?, ?)`,
['reporting_commodity', reporting_commodity]
);
await transaction.execute(
`INSERT INTO metadata (key, value) VALUES (?, ?)`,
['amount_dps', dps.toString()] // Manually call .toString() to format as int, otherwise sqlx formats as float
);
await transaction.commit();
}
export function joinedToTransactions(joinedTransactionPostings: JoinedTransactionPosting[]): Transaction[] {
// Group postings into transactions
const transactions: Transaction[] = [];

View File

@ -40,6 +40,7 @@ async function initApp() {
{ path: '/journal', name: 'journal', component: () => import('./pages/JournalView.vue') },
{ path: '/journal/edit/:id', name: 'journal-edit-transaction', component: () => import('./pages/EditTransactionView.vue') },
{ path: '/journal/new', name: 'journal-new-transaction', component: () => import('./pages/NewTransactionView.vue') },
{ path: '/new-file', name: 'new-file', component: () => import('./pages/NewFileView.vue') },
{ path: '/statement-lines', name: 'statement-lines', component: () => import('./pages/StatementLinesView.vue') },
{ path: '/statement-lines/import', name: 'import-statement', component: () => import('./pages/ImportStatementView.vue') },
{ path: '/transactions/:account', name: 'transactions', component: () => import('./pages/TransactionsView.vue') },

80
src/pages/NewFileView.vue Normal file
View File

@ -0,0 +1,80 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<h1 class="page-heading mb-4">
New file
</h1>
<div class="grid grid-cols-[max-content_1fr] space-y-2 mb-4 items-baseline">
<label for="eofy_date" class="block text-gray-900 pr-4">End of financial year</label>
<div>
<input type="date" class="bordered-field" id="eofy_date" v-model="eofy_date">
</div>
<label for="reporting_commodity" class="block text-gray-900 pr-4">Reporting currency</label>
<div>
<input type="text" class="bordered-field text-gray-500" id="reporting_commodity" v-model="reporting_commodity" disabled>
</div>
<label for="amount_dps" class="block text-gray-900 pr-4">Decimal places</label>
<div>
<input type="number" class="bordered-field text-gray-500" id="amount_dps" v-model="amount_dps" disabled>
</div>
</div>
<div class="flex justify-end mt-4 space-x-2">
<button class="btn-primary" @click="createNewFile">OK</button>
</div>
</template>
<script setup type="ts">
import dayjs from 'dayjs';
import { save } from '@tauri-apps/plugin-dialog';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { createNewDatabase, db } from '../db.ts';
// Get initial EOFY date
let eofy_dayjs = dayjs().set('month', 5).set('date', 30);
if (eofy_dayjs.isBefore(dayjs())) {
eofy_dayjs.set('year', eofy_dayjs.year() + 1);
}
const eofy_date = ref(eofy_dayjs.format('YYYY-MM-DD'));
const reporting_commodity = ref('$');
const amount_dps = ref(2);
const router = useRouter();
async function createNewFile() {
const file = await save({
filters: [
{ name: 'DrCr database (SQLite)', extensions: ['db'] }
],
});
if (file !== null) {
// Create new database
await createNewDatabase(file, dayjs(eofy_date.value).format('YYYY-MM-DD'), reporting_commodity.value, amount_dps.value);
// Load the database
await db.init(file);
router.push({ name: 'index' });
}
}
</script>

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -19,7 +19,7 @@
<template>
<p class="text-gray-900 mb-4">Welcome to DrCr. No file is currently open.</p>
<ul class="list-disc ml-6">
<!--<li><a href="#" class="text-gray-900 hover:text-blue-700 hover:underline">New file</a></li>-->
<li><RouterLink :to="{name: 'new-file'}" class="text-gray-900 hover:text-blue-700 hover:underline">New file</RouterLink></li>
<li><a href="#" @click="openFile" class="text-gray-900 hover:text-blue-700 hover:underline">Open file</a></li>
</ul>
</template>
@ -33,6 +33,9 @@
const file = await open({
multiple: false,
directory: false,
filters: [
{ name: 'DrCr database (SQLite)', extensions: ['db'] }
],
});
if (file !== null) {