Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .frontmatter/database/mediaDb.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "src": { "pages": { "api": { "auth": {} } } } }
{ "src": { "utils": {}, "pages": { "api": { "auth": {} } } } }
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ This is the official website for Datum Inc., built with Astro.

## Getting Started

### Prerequisites

- Node.js (version specified in package.json)

### Installation

1. Install dependencies:
Expand Down Expand Up @@ -70,8 +66,17 @@ npm run dev

For detailed information about the project structure, see [PROJECT_STRUCTURE.md](./PROJECT_STRUCTURE.md).

### Commands
### Image

Import component

```
import Figure from '@components/Figure.astro';
```

Example in real use:

### Commands
All commands are run from the root of the project, from a terminal:

| Command | Action |
Expand Down Expand Up @@ -138,7 +143,7 @@ All commands are run from the root of the project, from a terminal:
minikube start
```

2. Create secret.yaml file separated with this source. Value:
4. Create secret.yaml file separated with this source. Value:

```yaml
apiVersion: v1
Expand Down Expand Up @@ -166,13 +171,13 @@ kubectl create namespace datum-net
Apply secret:

```bash
kubectl apply -f config/dev/secret.yaml
kubectl apply -f ../secret.yaml
```

Apply the kustomize config file:

```bash
kubectl apply -k config/base
kubectl apply -k config/base -n datum-net
```

Apply the kustomize postgres config file:
Expand All @@ -184,7 +189,7 @@ kubectl apply -k config/dev/postgres-config.yaml
4. Install postgresql helm (example from bitnami source):

```bash
helm install postgresql -f config/dev/postgres-values.yaml -n datum-net oci://registry-1.docker.io/bitnamicharts/postgresql
kubectl port-forward pod_name -n datum-net 4321:4321
```

---
Expand Down
2 changes: 2 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import compressor from 'astro-compressor';

import glossary from './src/plugins/glossary.js';
import sitemap from './src/plugins/sitemap.js';
import databaseMigration from './src/plugins/databaseMigration.ts';
import announcement from './src/plugins/announcement.ts';
import { remarkModifiedTime } from './src/plugins/remarkModifiedTime.mjs';

Expand Down Expand Up @@ -206,6 +207,7 @@ export default defineConfig({
gzip: true,
brotli: true,
}),
databaseMigration(),
],
vite: {
plugins: [tailwindcss()],
Expand Down
1 change: 0 additions & 1 deletion config/base/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ spec:
containers:
- name: datum-net
image: ghcr.io/datum-cloud/datum-net:latest

imagePullPolicy: Always
ports:
- containerPort: 4321
Expand Down
3 changes: 3 additions & 0 deletions config/dev/postgres-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ global:
configurationCM: 'postgresql-config'
auth:
database: 'datum_net'
username: 'postgres'
password: 'datum'
postgresPassword: 'datum' # Admin password
resources:
requests:
cpu: '1'
Expand Down
8 changes: 5 additions & 3 deletions src/pages/dev/info.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ if (mode == 'production') {
let psqlInfo;
try {
const sql = await dbConnect();
psqlInfo = await sql`SELECT version()`;
} catch {
psqlInfo = null;
const listTables = await sql`SELECT tablename FROM pg_tables WHERE schemaname='public'`;
const version = await sql`SELECT version()`;
psqlInfo = { version: version[0].version, tables: listTables.map((t) => t.tablename) };
} catch (error) {
psqlInfo = { error };
}

type EnvValue = string | number | boolean | undefined;
Expand Down
60 changes: 60 additions & 0 deletions src/plugins/databaseMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { dbConnect } from '../libs/postgres';
import type { AstroIntegration } from 'astro';

/**
* Database migration integration for Astro
*/
async function migrateDatabase() {
try {
const POSTGRES_USER = process.env.POSTGRES_USER;
const sql = await dbConnect();
// check if table exists
const table = await sql`SELECT to_regclass('public.issues') as table_exists;`;

if (table[0].table_exists) {
console.log('DB Init: Database already initialized');
} else {
if (POSTGRES_USER) {
// Initialize tables
await sql`
CREATE TABLE IF NOT EXISTS issues (
id VARCHAR(32) PRIMARY KEY,
title VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
url VARCHAR(255) NOT NULL,
updated_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS votes (
id VARCHAR(32) UNIQUE NOT NULL,
vote INTEGER DEFAULT 0,
updated_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS user_votes (
user_id VARCHAR(64) NOT NULL,
issue_id VARCHAR(32) NOT NULL,
CONSTRAINT user_issue UNIQUE (user_id,issue_id)
);

ALTER TABLE issues OWNER TO ${POSTGRES_USER};
ALTER TABLE votes OWNER TO ${POSTGRES_USER};
ALTER TABLE user_votes OWNER TO ${POSTGRES_USER};`;
}
}
console.log('DB Init: Database initialization completed');
} catch {
console.log('DB Init: Database initialization failed');
}
}

export default function databaseMigration(): AstroIntegration {
return {
name: 'database-migration',
hooks: {
'astro:build:done': async () => {
await migrateDatabase();
},
},
};
}
80 changes: 0 additions & 80 deletions src/utils/github.ts

This file was deleted.

Loading