mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 19:05:32 -04:00
Proper DB migration
This commit is contained in:
parent
1065b430e3
commit
f7a360c2d8
19
migration/ids.2.cr
Normal file
19
migration/ids.2.cr
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
class CreateIds < MG::Base
|
||||||
|
def up : String
|
||||||
|
<<-SQL
|
||||||
|
CREATE TABLE IF NOT EXISTS ids (
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
id TEXT NOT NULL,
|
||||||
|
is_title INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS path_idx ON ids (path);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS id_idx ON ids (id);
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
def down : String
|
||||||
|
<<-SQL
|
||||||
|
DROP TABLE ids;
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
end
|
19
migration/tags.4.cr
Normal file
19
migration/tags.4.cr
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
class CreateTags < MG::Base
|
||||||
|
def up : String
|
||||||
|
<<-SQL
|
||||||
|
CREATE TABLE IF NOT EXISTS tags (
|
||||||
|
id TEXT NOT NULL,
|
||||||
|
tag TEXT NOT NULL,
|
||||||
|
unique (id, tag)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS tags_id_idx ON tags (id);
|
||||||
|
CREATE INDEX IF NOT EXISTS tags_tag_idx ON tags (tag);
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
def down : String
|
||||||
|
<<-SQL
|
||||||
|
DROP TABLE tags;
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
end
|
20
migration/thumbnails.3.cr
Normal file
20
migration/thumbnails.3.cr
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
class CreateThumbnails < MG::Base
|
||||||
|
def up : String
|
||||||
|
<<-SQL
|
||||||
|
CREATE TABLE IF NOT EXISTS thumbnails (
|
||||||
|
id TEXT NOT NULL,
|
||||||
|
data BLOB NOT NULL,
|
||||||
|
filename TEXT NOT NULL,
|
||||||
|
mime TEXT NOT NULL,
|
||||||
|
size INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS tn_index ON thumbnails (id);
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
def down : String
|
||||||
|
<<-SQL
|
||||||
|
DROP TABLE thumbnails;
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
end
|
20
migration/users.1.cr
Normal file
20
migration/users.1.cr
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
class CreateUsers < MG::Base
|
||||||
|
def up : String
|
||||||
|
<<-SQL
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
username TEXT NOT NULL,
|
||||||
|
password TEXT NOT NULL,
|
||||||
|
token TEXT,
|
||||||
|
admin INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS username_idx ON users (username);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS token_idx ON users (token);
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
def down : String
|
||||||
|
<<-SQL
|
||||||
|
DROP TABLE users;
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
end
|
@ -52,6 +52,10 @@ shards:
|
|||||||
git: https://github.com/hkalexling/koa.git
|
git: https://github.com/hkalexling/koa.git
|
||||||
version: 0.5.0
|
version: 0.5.0
|
||||||
|
|
||||||
|
mg:
|
||||||
|
git: https://github.com/hkalexling/mg.git
|
||||||
|
version: 0.1.0+git.commit.8d378bf58da442be2e5a27670233d43687d14121
|
||||||
|
|
||||||
myhtml:
|
myhtml:
|
||||||
git: https://github.com/kostya/myhtml.git
|
git: https://github.com/kostya/myhtml.git
|
||||||
version: 1.5.1
|
version: 1.5.1
|
||||||
|
@ -41,3 +41,5 @@ dependencies:
|
|||||||
github: hkalexling/koa
|
github: hkalexling/koa
|
||||||
tallboy:
|
tallboy:
|
||||||
github: epoch/tallboy
|
github: epoch/tallboy
|
||||||
|
mg:
|
||||||
|
github: hkalexling/mg
|
||||||
|
@ -11,20 +11,7 @@ class Logger
|
|||||||
use_default
|
use_default
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
level = Config.current.log_level
|
@@severity = Logger.get_severity
|
||||||
{% begin %}
|
|
||||||
case level.downcase
|
|
||||||
when "off"
|
|
||||||
@@severity = :none
|
|
||||||
{% for lvl, i in LEVELS %}
|
|
||||||
when {{lvl}}
|
|
||||||
@@severity = Log::Severity.new SEVERITY_IDS[{{i}}]
|
|
||||||
{% end %}
|
|
||||||
else
|
|
||||||
raise "Unknown log level #{level}"
|
|
||||||
end
|
|
||||||
{% end %}
|
|
||||||
|
|
||||||
@log = Log.for("")
|
@log = Log.for("")
|
||||||
@backend = Log::IOBackend.new
|
@backend = Log::IOBackend.new
|
||||||
|
|
||||||
@ -49,6 +36,28 @@ class Logger
|
|||||||
Log.setup @@severity, @backend
|
Log.setup @@severity, @backend
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.get_severity(level = "") : Log::Severity
|
||||||
|
if level.empty?
|
||||||
|
level = Config.current.log_level
|
||||||
|
end
|
||||||
|
{% begin %}
|
||||||
|
case level.downcase
|
||||||
|
when "off"
|
||||||
|
return Log::Severity::None
|
||||||
|
{% for lvl, i in LEVELS %}
|
||||||
|
when {{lvl}}
|
||||||
|
return Log::Severity.new SEVERITY_IDS[{{i}}]
|
||||||
|
{% end %}
|
||||||
|
else
|
||||||
|
raise "Unknown log level #{level}"
|
||||||
|
end
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset
|
||||||
|
@@default = Logger.new
|
||||||
|
end
|
||||||
|
|
||||||
# Ignores @@severity and always log msg
|
# Ignores @@severity and always log msg
|
||||||
def log(msg)
|
def log(msg)
|
||||||
@backend.write Log::Entry.new "", Log::Severity::None, msg,
|
@backend.write Log::Entry.new "", Log::Severity::None, msg,
|
||||||
|
@ -3,6 +3,8 @@ require "crypto/bcrypt"
|
|||||||
require "uuid"
|
require "uuid"
|
||||||
require "base64"
|
require "base64"
|
||||||
require "./util/*"
|
require "./util/*"
|
||||||
|
require "mg"
|
||||||
|
require "../migration/*"
|
||||||
|
|
||||||
def hash_password(pw)
|
def hash_password(pw)
|
||||||
Crypto::Bcrypt::Password.create(pw).to_s
|
Crypto::Bcrypt::Password.create(pw).to_s
|
||||||
@ -35,44 +37,19 @@ class Storage
|
|||||||
MainFiber.run do
|
MainFiber.run do
|
||||||
DB.open "sqlite3://#{@path}" do |db|
|
DB.open "sqlite3://#{@path}" do |db|
|
||||||
begin
|
begin
|
||||||
# v0.18.0
|
severity = Logger.get_severity
|
||||||
db.exec "create table tags (id text, tag text, unique (id, tag))"
|
Log.setup "mg", severity
|
||||||
db.exec "create index tags_id_idx on tags (id)"
|
MG::Migration.new(db).migrate
|
||||||
db.exec "create index tags_tag_idx on tags (tag)"
|
|
||||||
|
|
||||||
# v0.15.0
|
|
||||||
db.exec "create table thumbnails " \
|
|
||||||
"(id text, data blob, filename text, " \
|
|
||||||
"mime text, size integer)"
|
|
||||||
db.exec "create unique index tn_index on thumbnails (id)"
|
|
||||||
|
|
||||||
# v0.1.1
|
|
||||||
db.exec "create table ids" \
|
|
||||||
"(path text, id text, is_title integer)"
|
|
||||||
db.exec "create unique index path_idx on ids (path)"
|
|
||||||
db.exec "create unique index id_idx on ids (id)"
|
|
||||||
|
|
||||||
# v0.1.0
|
|
||||||
db.exec "create table users" \
|
|
||||||
"(username text, password text, token text, admin integer)"
|
|
||||||
rescue e
|
rescue e
|
||||||
unless e.message.not_nil!.ends_with? "already exists"
|
Logger.fatal "DB migration failed. #{e}"
|
||||||
Logger.fatal "Error when checking tables in DB: #{e}"
|
raise e
|
||||||
raise e
|
ensure
|
||||||
end
|
Logger.reset
|
||||||
|
|
||||||
# If the DB is initialized through CLI but no user is added, we need
|
|
||||||
# to create the admin user when first starting the app
|
|
||||||
user_count = db.query_one "select count(*) from users", as: Int32
|
|
||||||
init_admin if init_user && user_count == 0
|
|
||||||
else
|
|
||||||
Logger.debug "Creating DB file at #{@path}"
|
|
||||||
db.exec "create unique index username_idx on users (username)"
|
|
||||||
db.exec "create unique index token_idx on users (token)"
|
|
||||||
|
|
||||||
init_admin if init_user
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
user_count = db.query_one "select count(*) from users", as: Int32
|
||||||
|
init_admin if init_user && user_count == 0
|
||||||
|
|
||||||
# Verifies that the default username in config is valid
|
# Verifies that the default username in config is valid
|
||||||
if Config.current.disable_login
|
if Config.current.disable_login
|
||||||
username = Config.current.default_username
|
username = Config.current.default_username
|
||||||
|
Loading…
x
Reference in New Issue
Block a user