Compare commits

..

No commits in common. "76bd2a5e1b60fc1aa825ed05660e4fa0651ef8fd" and "2fdcc1633dab0bf01172b8a0fffc5dbd0431c7df" have entirely different histories.

11 changed files with 104 additions and 129 deletions

View File

@ -1,11 +1,9 @@
import type { Handle } from '@sveltejs/kit' import type { Handle } from '@sveltejs/kit'
import { getPool } from '$lib/server/database/pool'; import { getConnection } from '$lib/server/database/get_connection'
import git from '$lib/server/git';
export const handle: Handle = async ({ event, resolve }) => { export const handle: Handle = async ({ event, resolve }) => {
const pg = getPool; const pg = getConnection();
event.locals.db = pg; event.locals.db = pg;
event.locals.git = git;
const result = await resolve(event); const result = await resolve(event);
return result; return result;

View File

@ -24,21 +24,9 @@ export class Postgres {
await this.client!.query('commit'); await this.client!.query('commit');
} }
async savepoint(name: string) {
await this.client!.query(`savepoint ${name}`);
}
async rollback() { async rollback() {
await this.client!.query('rollback'); await this.client!.query('rollback');
} }
async rollbackTo(name: string) {
await this.client!.query(`rollback to ${name}`);
}
async releaseSavepoint(name: string) {
await this.client!.query(`release savepoint ${name}`);
}
} }
export default async ( export default async (

View File

@ -12,13 +12,12 @@ const connectionString = `postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}
let pool = new Pool({ connectionString }); let pool = new Pool({ connectionString });
export const getPool = pool; export const getConnection = () => pool;
export const reConnect = async () => { export const reConnect = async () => {
pool = new Pool({ connectionString }); pool = new Pool({ connectionString });
} }
import { init as init_data} from '$lib/server/data_loader'; import init_db from '$lib/server/database/init';
import PG from '$lib/server/database/postgres'; import PG from '$lib/server/database';
import git from '$lib/server/git'; await init_db(await PG(pool));
await init_data(await PG(pool), git);

View File

@ -1,21 +1,90 @@
import type { Postgres } from '$lib/server/database/postgres'; // initialize
import type { SimpleGit } from 'simple-git'; import type { Postgres } from '$lib/server/database';
import fs from 'fs'; import fs from 'fs';
import compile from '$lib/server/article_compiler'; import compile from '$lib/server/article';
import type { TableSchema } from '$lib/server/database/table'; import article_git from '$lib/server/aritcle-git';
import server_table from '$lib/server/database/table';
import { format } from '$lib/scripts/formatted_date'; import { format } from '$lib/scripts/formatted_date';
const load = async (db: Postgres, git: SimpleGit) => { export default async function init(db: Postgres) {
await cloneRepo();
await createTable(db, [
{
name: 'article',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'id', type: 'text', constraint: 'not null' },
{ name: 'released_at', type: 'timestamp', constraint: 'not null' },
{ name: 'updated_at', type: 'timestamp', constraint: 'not null' },
{ name: 'author', type: 'text', constraint: 'not null' },
{ name: 'email', type: 'text', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' },
{ name: 'category', type: 'text', constraint: 'not null' },
{ name: 'tags', type: 'text[]', constraint: 'not null' },
{ name: 'image', type: 'text', constraint: '' },
{ name: 'publish', type: 'text', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'article_comment',
columns: [
{ name: 'id', type: 'serial', constraint: 'primary key' },
{ name: 'article', type: 'integer', constraint: 'not null' },
{ name: 'posted_at', type: 'timestamp', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'thread',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'id', type: 'text', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' },
{ name: 'category', type: 'text', constraint: 'not null' },
{ name: 'created_at', type: 'timestamp', constraint: 'not null' },
{ name: 'updated_at', type: 'timestamp', constraint: 'not null' },
{ name: 'tags', type: 'text[]', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'thread_post',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'thread_id', type: 'integer', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' },
{ name: 'posted_at', type: 'timestamp', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'thread_comment',
columns: [
{ name: 'id', type: 'serial', constraint: 'primary key' },
{ name: 'thread', type: 'integer', constraint: 'not null' },
{ name: 'posted_at', type: 'timestamp', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'tag',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'name', type: 'text', constraint: 'not null' },
{ name: 'ref_count', type: 'integer', constraint: 'not null' },
],
}
]);
const articleFiles = await crawlArticles(db); const articleFiles = await crawlArticles(db);
await db.query('update tag set ref_count = 0'); await db.query('update tag set ref_count = 0');
await db.begin();
for (const { path, id } of articleFiles) { for (const { path, id } of articleFiles) {
await db.savepoint(id);
console.log(`Processing ${id}...`); console.log(`Processing ${id}...`);
await db.begin();
try { try {
const gitlog = await git.log({ const gitlog = await article_git.log({
file: path.slice(11), file: path.slice(11),
strictDate: true, strictDate: true,
}); });
@ -66,38 +135,36 @@ const load = async (db: Postgres, git: SimpleGit) => {
} }
} catch (err) { } catch (err) {
console.log(err); console.log(err);
await db.rollbackTo(id); await db.rollback();
} finally { } finally {
console.log(''); console.log('');
await db.releaseSavepoint(id);
}
}
await db.commit(); await db.commit();
} }
}
export async function init(db: Postgres, git: SimpleGit) { await db.release();
await createTable(db, server_table);
await cloneRepo(git);
await load(db, git);
}
export async function reload(db: Postgres, git: SimpleGit) {
await cloneRepo(git);
await load(db, git);
} }
type ArticleFileItem = { type ArticleFileItem = {
path: string, path: string,
id: string, id: string,
} }
export type TableSchema = {
name: string,
columns: {
name: string,
type: string,
constraint: string,
}[],
}
const cloneRepo = async (git: SimpleGit) => { const cloneRepo = async () => {
if (fs.existsSync('./articles/')) { if (fs.existsSync('./articles/')) {
console.log('Pulling articles from git..'); console.log('Pulling articles from git..');
await git.pull(); await article_git.pull();
} else { } else {
console.log('Cloning articles from git..'); console.log('Cloning articles from git..');
await git.clone('git@gitea.hareworks.net:Hare/blog-articles.git', 'articles'); await article_git.clone('git@gitea.hareworks.net:Hare/blog-articles.git', 'articles');
} }
} }

View File

@ -1,77 +0,0 @@
export type TableSchema = {
name: string,
columns: {
name: string,
type: string,
constraint: string,
}[],
}
export default [
{
name: 'article',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'id', type: 'text', constraint: 'not null' },
{ name: 'released_at', type: 'timestamp', constraint: 'not null' },
{ name: 'updated_at', type: 'timestamp', constraint: 'not null' },
{ name: 'author', type: 'text', constraint: 'not null' },
{ name: 'email', type: 'text', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' },
{ name: 'category', type: 'text', constraint: 'not null' },
{ name: 'tags', type: 'text[]', constraint: 'not null' },
{ name: 'image', type: 'text', constraint: '' },
{ name: 'publish', type: 'text', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'article_comment',
columns: [
{ name: 'id', type: 'serial', constraint: 'primary key' },
{ name: 'article', type: 'integer', constraint: 'not null' },
{ name: 'posted_at', type: 'timestamp', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'thread',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'id', type: 'text', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' },
{ name: 'category', type: 'text', constraint: 'not null' },
{ name: 'created_at', type: 'timestamp', constraint: 'not null' },
{ name: 'updated_at', type: 'timestamp', constraint: 'not null' },
{ name: 'tags', type: 'text[]', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'thread_post',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'thread_id', type: 'integer', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' },
{ name: 'posted_at', type: 'timestamp', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'thread_comment',
columns: [
{ name: 'id', type: 'serial', constraint: 'primary key' },
{ name: 'thread', type: 'integer', constraint: 'not null' },
{ name: 'posted_at', type: 'timestamp', constraint: 'not null' },
{ name: 'content', type: 'text', constraint: 'not null' },
],
},
{
name: 'tag',
columns: [
{ name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'name', type: 'text', constraint: 'not null' },
{ name: 'ref_count', type: 'integer', constraint: 'not null' },
],
}
] as TableSchema[];

View File

@ -1,6 +1,6 @@
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import PG from '$lib/server/database/postgres'; import PG from '$lib/server/database';
import { format } from '$lib/scripts/formatted_date'; import { format } from '$lib/scripts/formatted_date';
let data: { let data: {

View File

@ -6,9 +6,9 @@ import {
TOKEN TOKEN
} from '$env/static/private' } from '$env/static/private'
import PG from '$lib/server/database/postgres'; import PG from '$lib/server/database';
import { building } from '$app/environment'; import { building } from '$app/environment';
import { reload as reload_data } from '$lib/server/data_loader'; import init_db from '$lib/server/database/init';
export const POST: RequestHandler = async ({ url, locals }) => { export const POST: RequestHandler = async ({ url, locals }) => {
const token = url.searchParams.get('token'); const token = url.searchParams.get('token');
@ -17,6 +17,6 @@ export const POST: RequestHandler = async ({ url, locals }) => {
return error(401, 'Unauthorized'); return error(401, 'Unauthorized');
} }
if (!building) await reload_data(await PG(locals.db), locals.git); if (!building) await init_db(await PG(locals.db));
return new Response(String(token)); return new Response(String(token));
}; };

View File

@ -1,6 +1,6 @@
import type { Article } from '$lib/article'; import type { Article } from '$lib/article';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import PG from '$lib/server/database/postgres'; import PG from '$lib/server/database';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params, locals }) => { export const load: PageServerLoad = async ({ params, locals }) => {

View File

@ -1,6 +1,6 @@
import type { Article } from '$lib/article'; import type { Article } from '$lib/article';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import PG from '$lib/server/database/postgres'; import PG from '$lib/server/database';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params, locals }) => { export const load: PageServerLoad = async ({ params, locals }) => {