lockserver/db/migrations/1_init.sql

60 lines
1.3 KiB
SQL

-- +goose Up
-- +goose StatementBegin
PRAGMA foreign_keys = ON;
CREATE TABLE locks (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
zwave_device_id INTEGER NOT NULL UNIQUE
);
CREATE TABLE lock_code_slots (
id INTEGER PRIMARY KEY,
lock INTEGER NOT NULL REFERENCES locks(id),
code TEXT NOT NULL,
slot INTEGER NOT NULL,
name TEXT NOT NULL,
enabled BOOLEAN NOT NULL DEFAULT 0,
UNIQUE (lock, slot)
);
CREATE TABLE lock_log (
lock INTEGER NOT NULL REFERENCES locks(id),
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
state TEXT NOT NULL,
code INTEGER REFERENCES lock_code_slots(id)
);
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE user_codes (
id INTEGER PRIMARY KEY,
user INTEGER REFERENCES users(id),
name TEXT,
code TEXT UNIQUE NOT NULL,
start DATETIME,
end DATETIME
);
CREATE TABLE user_code_slots (
user_code REFERENCES user_codes(id),
lock REFERENCES locks(id),
slot REFERENCES lock_code_slots(id),
UNIQUE (user_code, lock)
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE user_code_slots;
DROP TABLE user_codes;
DROP TABLE users;
DROP TABLE lock_log;
DROP TABLE lock_code_slots;
DROP TABLE locks;
-- +goose StatementEnd