feat: Add new tables for article comments, threads, and thread comments

This commit is contained in:
Keisuke Hirata 2024-08-24 17:00:39 +09:00
parent c4a37dcdc5
commit 6b59fe77c4
3 changed files with 45 additions and 3 deletions

View File

@ -1,3 +1,4 @@
FROM postgres:16 FROM postgres:16
COPY ./docker/initdb.d/ /docker-entrypoint-initdb.d/ COPY ./docker/initdb.d/ /docker-entrypoint-initdb.d/
# COPY ./docker/postgres.conf /var/lib/postgresql/data/postgresql.conf

0
docker/postgres.conf Normal file
View File

View File

@ -10,7 +10,8 @@ export default async function init() {
{ {
name: 'article', name: 'article',
columns: [ columns: [
{ name: 'id', type: 'serial', constraint: 'primary key' }, { name: 'seq', type: 'serial', constraint: 'primary key' },
{ name: 'id', type: 'text', constraint: 'not null' },
{ name: 'title', type: 'text', constraint: 'not null' }, { name: 'title', type: 'text', constraint: 'not null' },
{ name: 'released_at', type: 'timestamp', constraint: 'not null' }, { name: 'released_at', type: 'timestamp', constraint: 'not null' },
{ name: 'updated_at', type: 'timestamp', constraint: 'not null' }, { name: 'updated_at', type: 'timestamp', constraint: 'not null' },
@ -18,13 +19,53 @@ export default async function init() {
{ name: 'content', 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: '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' },
],
}
]; ];
const db = await PG(); const db = await PG();
try { try {
await db.begin(); await db.begin();
for (const schema of schemas) { for (const schema of schemas) {
const res = await db.query(`select * from information_schema.tables where table_name = '${schema.name}'`) const res = await db.query(`select * from information_schema.tables where table_name = '${schema.name}'`)
if (res.rowCount === null) { if (res.rowCount == 0) {
console.log(`Creating table ${schema.name}`); console.log(`Creating table ${schema.name}`);
const columnStr = schema.columns.map(c => `${c.name} ${c.type} ${c.constraint}`).join(', '); const columnStr = schema.columns.map(c => `${c.name} ${c.type} ${c.constraint}`).join(', ');
await db.query(`create table ${schema.name} (${columnStr})`); await db.query(`create table ${schema.name} (${columnStr})`);