Commit d5fd9a28 authored by ale's avatar ale

Allow adding logs without a DeviceInfo field

parent d324639f
Pipeline #3358 passed with stages
in 5 minutes and 39 seconds
-- Make the userlog.device_id field nullable. Unfortunately SQLite
-- does not support modifying constraints on a column, so we have to
-- dump and reload the table contents using a temporary table.
PRAGMA foreign_keys=off;
ALTER TABLE userlog RENAME TO _userlog_old;
CREATE TABLE userlog (
username TEXT NOT NULL,
service TEXT NOT NULL,
log_type TEXT NOT NULL,
login_method TEXT,
message TEXT,
device_id VARCHAR(64),
device_remote_zone TEXT,
device_user_agent TEXT,
device_browser TEXT,
device_os TEXT,
device_mobile BOOL,
timestamp DATETIME
);
INSERT INTO userlog (username, service, log_type, login_method, message, device_id, device_remote_zone, device_user_agent, device_browser, device_os, device_mobile, timestamp)
SELECT username, service, log_type, login_method, message, device_id, device_remote_zone, device_user_agent, device_browser, device_os, device_mobile, timestamp
FROM _userlog_old;
DROP TABLE _userlog_old;
PRAGMA foreign_keys=on;
......@@ -4,6 +4,7 @@
// migrations/1_initialize_schema.up.sql
// migrations/2_lastlogin.down.sql
// migrations/2_lastlogin.up.sql
// migrations/3_userlog_device_nullable.up.sql
// DO NOT EDIT!
package migrations
......@@ -65,7 +66,7 @@ func _1_initialize_schemaDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "1_initialize_schema.down.sql", size: 144, mode: os.FileMode(420), modTime: time.Unix(1549127471, 0)}
info := bindataFileInfo{name: "1_initialize_schema.down.sql", size: 144, mode: os.FileMode(420), modTime: time.Unix(1535013291, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
......@@ -121,7 +122,7 @@ func _1_initialize_schemaUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "1_initialize_schema.up.sql", size: 1258, mode: os.FileMode(420), modTime: time.Unix(1549127471, 0)}
info := bindataFileInfo{name: "1_initialize_schema.up.sql", size: 1258, mode: os.FileMode(420), modTime: time.Unix(1535013291, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
......@@ -141,7 +142,7 @@ func _2_lastloginDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "2_lastlogin.down.sql", size: 100, mode: os.FileMode(420), modTime: time.Unix(1549532976, 0)}
info := bindataFileInfo{name: "2_lastlogin.down.sql", size: 100, mode: os.FileMode(420), modTime: time.Unix(1551599549, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
......@@ -166,7 +167,55 @@ func _2_lastloginUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "2_lastlogin.up.sql", size: 253, mode: os.FileMode(420), modTime: time.Unix(1549532906, 0)}
info := bindataFileInfo{name: "2_lastlogin.up.sql", size: 253, mode: os.FileMode(420), modTime: time.Unix(1551599549, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var __3_userlog_device_nullableUpSql = []byte(`
-- Make the userlog.device_id field nullable. Unfortunately SQLite
-- does not support modifying constraints on a column, so we have to
-- dump and reload the table contents using a temporary table.
PRAGMA foreign_keys=off;
ALTER TABLE userlog RENAME TO _userlog_old;
CREATE TABLE userlog (
username TEXT NOT NULL,
service TEXT NOT NULL,
log_type TEXT NOT NULL,
login_method TEXT,
message TEXT,
device_id VARCHAR(64),
device_remote_zone TEXT,
device_user_agent TEXT,
device_browser TEXT,
device_os TEXT,
device_mobile BOOL,
timestamp DATETIME
);
INSERT INTO userlog (username, service, log_type, login_method, message, device_id, device_remote_zone, device_user_agent, device_browser, device_os, device_mobile, timestamp)
SELECT username, service, log_type, login_method, message, device_id, device_remote_zone, device_user_agent, device_browser, device_os, device_mobile, timestamp
FROM _userlog_old;
DROP TABLE _userlog_old;
PRAGMA foreign_keys=on;
`)
func _3_userlog_device_nullableUpSqlBytes() ([]byte, error) {
return __3_userlog_device_nullableUpSql, nil
}
func _3_userlog_device_nullableUpSql() (*asset, error) {
bytes, err := _3_userlog_device_nullableUpSqlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "3_userlog_device_nullable.up.sql", size: 1046, mode: os.FileMode(420), modTime: time.Unix(1560152262, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
......@@ -227,6 +276,7 @@ var _bindata = map[string]func() (*asset, error){
"1_initialize_schema.up.sql": _1_initialize_schemaUpSql,
"2_lastlogin.down.sql": _2_lastloginDownSql,
"2_lastlogin.up.sql": _2_lastloginUpSql,
"3_userlog_device_nullable.up.sql": _3_userlog_device_nullableUpSql,
}
// AssetDir returns the file names below a certain
......@@ -273,6 +323,7 @@ var _bintree = &bintree{nil, map[string]*bintree{
"1_initialize_schema.up.sql": &bintree{_1_initialize_schemaUpSql, map[string]*bintree{}},
"2_lastlogin.down.sql": &bintree{_2_lastloginDownSql, map[string]*bintree{}},
"2_lastlogin.up.sql": &bintree{_2_lastloginUpSql, map[string]*bintree{}},
"3_userlog_device_nullable.up.sql": &bintree{_3_userlog_device_nullableUpSql, map[string]*bintree{}},
}}
// RestoreAsset restores an asset under the given directory
......
......@@ -165,6 +165,36 @@ func TestUserlog_AddLog(t *testing.T) {
}
}
func TestUserlog_AddLog_NoDeviceInfo(t *testing.T) {
defer os.Remove("test.db")
db, err := openDB("test.db")
if err != nil {
t.Fatal(err)
}
defer db.Close()
ulog, err := newUserlogDB(db, 0, 0)
if err != nil {
t.Fatal(err)
}
defer ulog.Close()
// Create a LogEntry with a nil DeviceInfo.
u := choose(randomUsernames)
e := &usermetadb.LogEntry{
Username: u,
Service: choose(randomServices),
Type: usermetadb.LogTypeLogin,
LoginMethod: usermetadb.LoginMethodPassword,
Message: "logged in",
Timestamp: randomTime().UTC(),
}
if err := ulog.AddLog(e); err != nil {
t.Fatalf("AddLog(%+v): %v", e, err)
}
}
func TestUserlog_GetUserDevices(t *testing.T) {
defer os.Remove("test.db")
db, err := openDB("test.db")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment