<commit_before>#
#
#              The Nim Forum
#        (c) Copyright 2018 <NAME>, <NAME>
#        Look at license.txt for more info.
#        All rights reserved.
#
# Script to initialise the nimforum.

import strutils, db_sqlite, os, times, json

import auth, frontend/user

proc backup(path: string) =
  if existsFile(path):
    let backupPath = path & "." & $getTime().toUnix()
    echo(path, " already exists. Moving to ", backupPath)
    moveFile(path, backupPath)

proc initialiseDb(admin: tuple[username, password, email: string]) =
  let path = getCurrentDir() / "nimforum.db"
  backup(path)

  var db = open(connection="nimforum.db", user="", password="",
                database="nimforum")

  const
    userNameType = "varchar(20)"
    passwordType = "varchar(50)"
    emailType = "varchar(254)" # https://stackoverflow.com/a/574698/492186

  # -- Category

  db.exec(sql"""
    create table category(
      id integer primary key,
      name varchar(100) not null,
      description varchar(500) not null,
      color varchar(10) not null
    );

    insert into category (id, name, description, color)
    values (0, 'Default', '', '');
  """)

  # -- Thread

  db.exec(sql"""
  create table thread(
    id integer primary key,
    name varchar(100) not null,
    views integer not null,
    modified timestamp not null default (DATETIME('now')),<commit_msg>Fixes category creation in setup script.<commit_after>#
#
#              The Nim Forum
#        (c) Copyright 2018 <NAME>, <NAME>
#        Look at license.txt for more info.
#        All rights reserved.
#
# Script to initialise the nimforum.

import strutils, db_sqlite, os, times, json

import auth, frontend/user

proc backup(path: string) =
  if existsFile(path):
    let backupPath = path & "." & $getTime().toUnix()
    echo(path, " already exists. Moving to ", backupPath)
    moveFile(path, backupPath)

proc initialiseDb(admin: tuple[username, password, email: string]) =
  let path = getCurrentDir() / "nimforum.db"
  backup(path)

  var db = open(connection="nimforum.db", user="", password="",
                database="nimforum")

  const
    userNameType = "varchar(20)"
    passwordType = "varchar(50)"
    emailType = "varchar(254)" # https://stackoverflow.com/a/574698/492186

  # -- Category

  db.exec(sql"""
    create table category(
      id integer primary key,
      name varchar(100) not null,
      description varchar(500) not null,
      color varchar(10) not null
    );
  """)

  db.exec(sql"""
    insert into category (id, name, description, color)
    values (0, 'Default', '', '');
  """)

  # -- Thread

  db.exec(sql"""
  create table thread(
    id integer primary key,
    name varchar(100) not null,
    views integer not null,
    modified timestamp not null default (DATETIME('now')),
{'fraction_non_alphanumeric': 0.13147552697391926, 'fraction_numerical': 0.025008931761343337, 'mean_word_length': 3.0, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7276165', 'n_tokens_mistral': 937, 'n_tokens_neox': 862, 'n_words': 261}