the-forest/server/db_scripts/initialize_db.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-10-05 12:01:19 -04:00
// this is a horrible script i use and edit for db revisions and migrations. i hate it.
// i should have a db migration plan but this is not, and probably never will be, big
// enough of a project to really warrant it.
const sqlite = require("better-sqlite3");
const showdown = require("showdown");
2024-10-05 12:01:19 -04:00
const db = new sqlite('the_big_db.db', { verbose: console.log });
const converter = new showdown.Converter();
const createPages = db.prepare(`
create table if not exists pages (
id integer primary key,
2024-10-05 12:01:19 -04:00
number integer autoincrement,
title varchar(255),
2024-10-05 12:01:19 -04:00
description text,
2024-10-05 12:01:19 -04:00
html text,
time timestamp default current_timestamp,
author integer
)
`);
2024-10-10 22:54:22 -04:00
/* an object has:
id = primary key
number = object identifier number
title / NAME = a short display name for an object, or the title of a page
description / TEXT? = the markdown description of an object, or the lua script of the source code of a verb
2024-10-21 13:50:25 -04:00
html = the markdown for that object, rendered into html for display as a page
lua = the lua source code of the object, rendered into html with syntax highlighting
2024-10-10 22:54:22 -04:00
time = when this edit was saved
author = the user id of whoever authored this revision
2024-10-21 13:50:25 -04:00
type = 0 for noun, 1 for verb
2024-10-10 22:54:22 -04:00
*/
/* an attribute store has:
number = the object number it's attached to
contents = a `text` object holding a json store of attributes defined on object #number, containing:
LOCATION? = the object which contains this object? should this be in the DB or in the json store?
PROTOTYPE = the number of the prototype for this object, to which we should look for attributes not found here
ATTRIBUTES? = json object of attached, script-controlled attributes.
CONTENTS = json array of objectss contained inside this one
2024-10-21 13:50:25 -04:00
verbs = json array of objecsts which are verbs on this one
prepositions
2024-10-10 22:54:22 -04:00
*/
const createUsers = db.prepare(`
create table if not exists users (
2024-10-05 12:01:19 -04:00
id integer primary key,
name varchar(64) unique,
password varchar(128)
)
`);
2024-10-10 22:54:22 -04:00
/* a user has:
id = primary key
name = name string
password = argon2 hash of their password
2024-10-10 22:54:22 -04:00
character = object in the game world representing your character
*/
2024-10-05 12:01:19 -04:00
function migratePages() {
console.log("moving old table to a temporary");
console.log(db.prepare(`alter table pages rename to old_pages`).run())
console.log("creating new page table")
console.log(createPages.run());
2024-10-05 12:01:19 -04:00
console.log('iterating over old pages');
rows = db.prepare(`select * from old_pages`).all();
2024-10-05 12:01:19 -04:00
const insertPage = db.prepare(`insert into pages (number, title, description, html) values (:id, :title, :description, :html)`);
rows.forEach((pageData) => {
const {id, title, description, html} = pageData;
console.log(`rendering page number ${id}`)
const renderedPage = converter.makeHtml(description);
insertPage.run({...pageData, html: renderedPage});
});
2024-10-05 12:01:19 -04:00
console.log("getting rid of old table");
console.log(db.prepare("drop table old_pages").run());
}
db.transaction(migratePages)();
function migrateUsers() {
console.log("moving old users");
console.log(db.prepare("alter table users rename to old_users").run());
console.log("creating new users table");
console.log(createUsers.run());
2024-10-05 12:01:19 -04:00
const insertUser = db.prepare("insert into users (name, password) values (:name, :password)")
db.prepare("select * from old_users").all().forEach((user) => {
console.log(insertUser.run(user));
});
console.log("clearing old table");
console.log(db.prepare("drop table old_users").run());
}
2024-10-05 12:01:19 -04:00
db.transaction(migrateUsers)();