fix(sqlite): add indexes and unique constraint

This commit is contained in:
hexxa 2022-09-16 09:58:31 +08:00 committed by Hexxa
parent df5c820c68
commit d97451653e
4 changed files with 37 additions and 16 deletions

View file

@ -12,17 +12,6 @@ import (
"github.com/ihexxa/quickshare/src/db"
)
const (
InitNs = "Init"
InitTimeKey = "initTime"
SchemaVerKey = "SchemaVersion"
SchemaV1 = "v1"
)
var (
maxHashingTime = 10
)
func (st *SQLiteStore) getFileInfo(ctx context.Context, itemPath string) (*db.FileInfo, error) {
var infoStr string
fInfo := &db.FileInfo{}

View file

@ -154,7 +154,7 @@ func (st *SQLiteStore) ListSharingsByLocation(ctx context.Context, location stri
ctx,
`select path, share_id
from t_file_info
where location=? and share_id<>''`,
where share_id<>'' and location=?`,
location,
)
if err != nil {

View file

@ -137,8 +137,8 @@ func (st *SQLiteStore) getUploadInfo(ctx context.Context, userId uint64, filePat
ctx,
`select size, uploaded
from t_file_uploading
where user=? and real_path=?`,
userId, filePath,
where real_path=? and user=?`,
filePath, userId,
).Scan(&size, &uploaded)
if err != nil {
return "", 0, 0, err

View file

@ -76,7 +76,7 @@ func (st *SQLiteStore) InitUserTable(ctx context.Context, rootName, rootPwd stri
ctx,
`create table if not exists t_user (
id bigint not null,
name varchar not null,
name varchar not null unique,
pwd varchar not null,
role integer not null,
used_space bigint not null,
@ -89,6 +89,14 @@ func (st *SQLiteStore) InitUserTable(ctx context.Context, rootName, rootPwd stri
return err
}
_, err = st.db.ExecContext(
ctx,
`create index i_user_name on t_user (name)`,
)
if err != nil {
return err
}
admin := &db.User{
ID: 0,
Name: rootName,
@ -143,17 +151,41 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
return err
}
_, err = st.db.ExecContext(
ctx,
`create index t_file_share on t_file_info (share_id, location)`,
)
if err != nil {
return err
}
_, err = st.db.ExecContext(
ctx,
`create table if not exists t_file_uploading (
real_path varchar not null,
tmp_path varchar not null,
tmp_path varchar not null unique,
user bigint not null,
size bigint not null,
uploaded bigint not null,
primary key(real_path)
)`,
)
if err != nil {
return err
}
_, err = st.db.ExecContext(
ctx,
`create index t_file_uploading_path on t_file_uploading (real_path, user)`,
)
if err != nil {
return err
}
_, err = st.db.ExecContext(
ctx,
`create index t_file_uploading_user on t_file_uploading (user)`,
)
return err
}