Compare commits
No commits in common. "76bd2a5e1b60fc1aa825ed05660e4fa0651ef8fd" and "2fdcc1633dab0bf01172b8a0fffc5dbd0431c7df" have entirely different histories.
76bd2a5e1b
...
2fdcc1633d
|
@ -1,11 +1,9 @@
|
|||
import type { Handle } from '@sveltejs/kit'
|
||||
import { getPool } from '$lib/server/database/pool';
|
||||
import git from '$lib/server/git';
|
||||
import { getConnection } from '$lib/server/database/get_connection'
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
const pg = getPool;
|
||||
const pg = getConnection();
|
||||
event.locals.db = pg;
|
||||
event.locals.git = git;
|
||||
|
||||
const result = await resolve(event);
|
||||
return result;
|
||||
|
|
|
@ -24,21 +24,9 @@ export class Postgres {
|
|||
await this.client!.query('commit');
|
||||
}
|
||||
|
||||
async savepoint(name: string) {
|
||||
await this.client!.query(`savepoint ${name}`);
|
||||
}
|
||||
|
||||
async 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 (
|
|
@ -12,13 +12,12 @@ const connectionString = `postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}
|
|||
|
||||
let pool = new Pool({ connectionString });
|
||||
|
||||
export const getPool = pool;
|
||||
export const getConnection = () => pool;
|
||||
|
||||
export const reConnect = async () => {
|
||||
pool = new Pool({ connectionString });
|
||||
}
|
||||
|
||||
import { init as init_data} from '$lib/server/data_loader';
|
||||
import PG from '$lib/server/database/postgres';
|
||||
import git from '$lib/server/git';
|
||||
await init_data(await PG(pool), git);
|
||||
import init_db from '$lib/server/database/init';
|
||||
import PG from '$lib/server/database';
|
||||
await init_db(await PG(pool));
|
|
@ -1,21 +1,90 @@
|
|||
import type { Postgres } from '$lib/server/database/postgres';
|
||||
import type { SimpleGit } from 'simple-git';
|
||||
// initialize
|
||||
import type { Postgres } from '$lib/server/database';
|
||||
import fs from 'fs';
|
||||
import compile from '$lib/server/article_compiler';
|
||||
import type { TableSchema } from '$lib/server/database/table';
|
||||
import server_table from '$lib/server/database/table';
|
||||
import compile from '$lib/server/article';
|
||||
import article_git from '$lib/server/aritcle-git';
|
||||
|
||||
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);
|
||||
await db.query('update tag set ref_count = 0');
|
||||
|
||||
await db.begin();
|
||||
for (const { path, id } of articleFiles) {
|
||||
await db.savepoint(id);
|
||||
console.log(`Processing ${id}...`);
|
||||
await db.begin();
|
||||
try {
|
||||
const gitlog = await git.log({
|
||||
const gitlog = await article_git.log({
|
||||
file: path.slice(11),
|
||||
strictDate: true,
|
||||
});
|
||||
|
@ -66,38 +135,36 @@ const load = async (db: Postgres, git: SimpleGit) => {
|
|||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
await db.rollbackTo(id);
|
||||
await db.rollback();
|
||||
} finally {
|
||||
console.log('');
|
||||
await db.releaseSavepoint(id);
|
||||
await db.commit();
|
||||
}
|
||||
}
|
||||
await db.commit();
|
||||
}
|
||||
|
||||
export async function init(db: Postgres, git: SimpleGit) {
|
||||
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);
|
||||
await db.release();
|
||||
}
|
||||
|
||||
type ArticleFileItem = {
|
||||
path: 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/')) {
|
||||
console.log('Pulling articles from git..');
|
||||
await git.pull();
|
||||
await article_git.pull();
|
||||
} else {
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
|
@ -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[];
|
|
@ -1,6 +1,6 @@
|
|||
import { error } from '@sveltejs/kit';
|
||||
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';
|
||||
|
||||
let data: {
|
||||
|
|
|
@ -6,9 +6,9 @@ import {
|
|||
TOKEN
|
||||
} from '$env/static/private'
|
||||
|
||||
import PG from '$lib/server/database/postgres';
|
||||
import PG from '$lib/server/database';
|
||||
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 }) => {
|
||||
const token = url.searchParams.get('token');
|
||||
|
@ -17,6 +17,6 @@ export const POST: RequestHandler = async ({ url, locals }) => {
|
|||
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));
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import type { Article } from '$lib/article';
|
||||
import type { PageServerLoad } from './$types';
|
||||
import PG from '$lib/server/database/postgres';
|
||||
import PG from '$lib/server/database';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ params, locals }) => {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import type { Article } from '$lib/article';
|
||||
import type { PageServerLoad } from './$types';
|
||||
import PG from '$lib/server/database/postgres';
|
||||
import PG from '$lib/server/database';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ params, locals }) => {
|
||||
|
|
Loading…
Reference in New Issue
Block a user