the-forest/server/db_scripts/initialize_db.js

114 lines
3.5 KiB
JavaScript

// 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");
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,
number integer autoincrement,
title varchar(255),
description text,
html text,
time timestamp default current_timestamp,
author integer
)
`);
/* 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
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
time = when this edit was saved
author = the user id of whoever authored this revision
type = 0 for noun, 1 for verb
*/
/* 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
verbs = json array of objecsts which are verbs on this one
prepositions
*/
const createUsers = db.prepare(`
create table if not exists users (
id integer primary key,
name varchar(64) unique,
password varchar(128)
)
`);
/* a user has:
id = primary key
name = name string
password = argon2 hash of their password
character = object in the game world representing your character
*/
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());
console.log('iterating over old pages');
rows = db.prepare(`select * from old_pages`).all();
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});
});
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());
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());
}
db.transaction(migrateUsers)();