1
0
Fork 0

Add SQLite support

This commit is contained in:
Gregory Eremin 2017-11-07 00:12:06 +01:00
parent 0a108739d4
commit badcad8d70
10 changed files with 67 additions and 33 deletions

View File

@ -12,5 +12,8 @@ build:
frontend/build/... frontend/build/...
go build -tags=binassets -o backend/build/cmdui backend/main.go go build -tags=binassets -o backend/build/cmdui backend/main.go
create_db:
sqlite3 backend/data/cmdui.db < backend/schema_sqlite.sql
cloc: cloc:
cloc --exclude-dir=vendor,build,node_modules . cloc --exclude-dir=vendor,build,node_modules .

1
backend/.gitignore vendored
View File

@ -1,4 +1,5 @@
vendor vendor
build build
data
api/assets/bindata_assetfs.go api/assets/bindata_assetfs.go
config.toml config.toml

14
backend/Gopkg.lock generated
View File

@ -43,6 +43,12 @@
revision = "8c199fb6259ffc1af525cc3ad52ee60ba8359669" revision = "8c199fb6259ffc1af525cc3ad52ee60ba8359669"
version = "v1.1" version = "v1.1"
[[projects]]
name = "github.com/mattn/go-sqlite3"
packages = ["."]
revision = "5160b48509cf5c877bc22c11c373f8c7738cdb38"
version = "v1.3.0"
[[projects]] [[projects]]
name = "github.com/satori/go.uuid" name = "github.com/satori/go.uuid"
packages = ["."] packages = ["."]
@ -55,6 +61,12 @@
packages = ["ssh/terminal"] packages = ["ssh/terminal"]
revision = "2509b142fb2b797aa7587dad548f113b2c0f20ce" revision = "2509b142fb2b797aa7587dad548f113b2c0f20ce"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = ["context"]
revision = "01c190206fbdffa42f334f4b2bf2220f50e64920"
[[projects]] [[projects]]
branch = "master" branch = "master"
name = "golang.org/x/sys" name = "golang.org/x/sys"
@ -64,6 +76,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "6c201fcf68612a8ad33e6e620125ca3a88e8e4c34d7acc2b5ed0f452c3457a3f" inputs-digest = "4db9692ffa1d302e9cb342da138f0606a659403be6d1131f8689b874610a1eff"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -1,12 +1,16 @@
log_dir = "/var/log/commands" log_dir = "data/logs"
[commands] [commands]
base_path = "/usr/local/bin/runner" base_path = "/usr/local/bin/runner"
config_command = "/usr/local/bin/runner_commands --export=json" config_command = "/usr/local/bin/runner_commands --export=json"
[database] [database]
driver = "mysql" driver = "sqlite3"
spec = "root:@(localhost:3306)/commands?charset=utf8mb4&parseTime=true" spec = "data/cmdui.db"
# MySQL example
# driver = "mysql"
# spec = "root:@(localhost:3306)/commands?charset=utf8mb4&parseTime=true"
[server] [server]
host = "http://127.0.0.1" host = "http://127.0.0.1"

View File

@ -6,6 +6,7 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"github.com/localhots/cmdui/backend/config" "github.com/localhots/cmdui/backend/config"

View File

@ -127,15 +127,8 @@ func (r *Job) Create() error {
r.State = string(JobStateCreated) r.State = string(JobStateCreated)
_, err := db.NamedExec(` _, err := db.NamedExec(`
INSERT INTO jobs INSERT INTO jobs (id, command, args, flags, user_id, state, created_at)
SET VALUES (:id, :command, :args, :flags, :user_id, :state, :created_at)
id = :id,
command = :command,
args = :args,
flags = :flags,
user_id = :user_id,
state = :state,
created_at = :created_at
`, r) `, r)
if err != nil { if err != nil {
return errors.Annotate(err, "Failed to create a job") return errors.Annotate(err, "Failed to create a job")

View File

@ -42,12 +42,8 @@ func FindSession(id string) (*Session, error) {
func (s Session) Create() error { func (s Session) Create() error {
_, err := db.NamedExec(` _, err := db.NamedExec(`
INSERT INTO sessions INSERT INTO sessions (id, user_id, created_at, expires_at)
SET VALUES (:id, :user_id, :created_at, :expires_at)
id = :id,
user_id = :user_id,
created_at = :created_at,
expires_at = :expires_at
`, s) `, s)
if err != nil { if err != nil {
return errors.Annotate(err, "Failed to create a session") return errors.Annotate(err, "Failed to create a session")

View File

@ -71,13 +71,8 @@ func findUser(query string, args ...interface{}) (*User, error) {
func (u User) Create() error { func (u User) Create() error {
_, err := db.NamedExec(` _, err := db.NamedExec(`
INSERT INTO users INSERT INTO users (id, github_id, github_login, github_name, github_picture)
SET VALUES (:id, :github_id, :github_login, :github_name, :github_picture)
id = :id,
github_id = :github_id,
github_login = :github_login,
github_name = :github_name,
github_picture = :github_picture
`, u) `, u)
if err != nil { if err != nil {
return errors.Annotate(err, "Failed to create a user") return errors.Annotate(err, "Failed to create a user")

View File

@ -1,6 +1,6 @@
CREATE TABLE `jobs` ( CREATE TABLE `jobs` (
`id` char(36) NOT NULL DEFAULT '', `id` char(36) NOT NULL,
`command` varchar(255) NOT NULL DEFAULT '', `command` varchar(255) NOT NULL,
`args` text NOT NULL, `args` text NOT NULL,
`flags` text NOT NULL, `flags` text NOT NULL,
`user_id` char(36) DEFAULT NULL, `user_id` char(36) DEFAULT NULL,
@ -12,15 +12,15 @@ CREATE TABLE `jobs` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `sessions` ( CREATE TABLE `sessions` (
`id` char(36) NOT NULL DEFAULT '', `id` char(36) NOT NULL,
`user_id` char(36) NOT NULL DEFAULT '', `user_id` char(36) NOT NULL,
`created_at` datetime NOT NULL, `created_at` datetime NOT NULL,
`expires_at` datetime NOT NULL, `expires_at` datetime NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `users` ( CREATE TABLE `users` (
`id` char(36) NOT NULL DEFAULT '', `id` char(36) NOT NULL,
`github_id` int(11) unsigned NOT NULL, `github_id` int(11) unsigned NOT NULL,
`github_login` varchar(255) NOT NULL DEFAULT '', `github_login` varchar(255) NOT NULL DEFAULT '',
`github_name` varchar(255) NOT NULL DEFAULT '', `github_name` varchar(255) NOT NULL DEFAULT '',

29
backend/schema_sqlite.sql Normal file
View File

@ -0,0 +1,29 @@
CREATE TABLE `jobs` (
`id` CHAR(36) NOT NULL PRIMARY KEY,
`command` VARCHAR(255) NOT NULL,
`args` TEXT NOT NULL,
`flags` TEXT NOT NULL,
`user_id` CHAR(36) DEFAULT NULL,
`state` VARCHAR(20) NOT NULL,
`created_at` DATETIME DEFAULT NULL,
`started_at` DATETIME DEFAULT NULL,
`finished_at` DATETIME DEFAULT NULL
);
-- PRAGMA table_info(jobs);
CREATE TABLE `sessions` (
`id` CHAR(36) NOT NULL PRIMARY KEY,
`user_id` CHAR(36) NOT NULL,
`created_at` DATETIME NOT NULL,
`expires_at` DATETIME NOT NULL
);
-- PRAGMA table_info(sessions);
CREATE TABLE `users` (
`id` CHAR(36) NOT NULL PRIMARY KEY,
`github_id` UNSIGNED BIGINT NOT NULL,
`github_login` VARCHAR(255) NOT NULL DEFAULT '',
`github_name` VARCHAR(255) NOT NULL DEFAULT '',
`github_picture` VARCHAR(255) NOT NULL DEFAULT ''
);
-- PRAGMA table_info(users);