From a4a5e539edb4833305c78d52f91b43e6db9f90fe Mon Sep 17 00:00:00 2001 From: boojack Date: Sat, 19 Nov 2022 09:57:54 +0800 Subject: [PATCH] chore: update dev version (#489) --- server/version/version.go | 2 +- store/db/db.go | 24 ------- store/db/migration/dev/LATEST__SCHEMA.sql | 44 +++++++++---- .../prod/0.8/00__migration_history.sql | 5 ++ .../migration/prod/0.8/01__memo_relation.sql | 11 ++++ store/db/migration_history.go | 17 +---- store/db/table.go | 65 ------------------- 7 files changed, 51 insertions(+), 117 deletions(-) create mode 100644 store/db/migration/prod/0.8/00__migration_history.sql create mode 100644 store/db/migration/prod/0.8/01__memo_relation.sql delete mode 100644 store/db/table.go diff --git a/server/version/version.go b/server/version/version.go index 4aa2858f..5e5c99f6 100644 --- a/server/version/version.go +++ b/server/version/version.go @@ -10,7 +10,7 @@ import ( var Version = "0.7.3" // DevVersion is the service current development version. -var DevVersion = "0.7.3" +var DevVersion = "0.8.0" func GetCurrentVersion(mode string) string { if mode == "dev" { diff --git a/store/db/db.go b/store/db/db.go index 19085add..58031886 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -66,10 +66,6 @@ func (db *DB) Open(ctx context.Context) (err error) { return fmt.Errorf("failed to apply latest schema: %w", err) } } else { - if err := db.createMigrationHistoryTable(ctx); err != nil { - return fmt.Errorf("failed to create migration_history table: %w", err) - } - currentVersion := version.GetCurrentVersion(db.profile.Mode) migrationHistory, err := db.FindMigrationHistory(ctx, &MigrationHistoryFind{}) if err != nil { @@ -236,23 +232,3 @@ func getMinorVersionList() []string { return minorVersionList } - -// createMigrationHistoryTable creates the migration_history table if it doesn't exist. -func (db *DB) createMigrationHistoryTable(ctx context.Context) error { - tx, err := db.Db.Begin() - if err != nil { - return err - } - defer tx.Rollback() - - if err := createTable(ctx, tx, ` - CREATE TABLE IF NOT EXISTS migration_history ( - version TEXT NOT NULL PRIMARY KEY, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) - ); - `); err != nil { - return err - } - - return tx.Commit() -} diff --git a/store/db/migration/dev/LATEST__SCHEMA.sql b/store/db/migration/dev/LATEST__SCHEMA.sql index f388f2ad..645a7530 100644 --- a/store/db/migration/dev/LATEST__SCHEMA.sql +++ b/store/db/migration/dev/LATEST__SCHEMA.sql @@ -1,3 +1,17 @@ +-- migration_history +CREATE TABLE migration_history ( + version TEXT NOT NULL PRIMARY KEY, + created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) +); + +-- system_setting +CREATE TABLE system_setting ( + name TEXT NOT NULL, + value TEXT NOT NULL, + description TEXT NOT NULL DEFAULT '', + UNIQUE(name) +); + -- user CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -11,6 +25,14 @@ CREATE TABLE user ( open_id TEXT NOT NULL UNIQUE ); +-- user_setting +CREATE TABLE user_setting ( + user_id INTEGER NOT NULL, + key TEXT NOT NULL, + value TEXT NOT NULL, + UNIQUE(user_id, key) +); + -- memo CREATE TABLE memo ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -55,14 +77,6 @@ CREATE TABLE resource ( size INTEGER NOT NULL DEFAULT 0 ); --- user_setting -CREATE TABLE user_setting ( - user_id INTEGER NOT NULL, - key TEXT NOT NULL, - value TEXT NOT NULL, - UNIQUE(user_id, key) -); - -- memo_resource CREATE TABLE memo_resource ( memo_id INTEGER NOT NULL, @@ -72,10 +86,14 @@ CREATE TABLE memo_resource ( UNIQUE(memo_id, resource_id) ); --- system_setting -CREATE TABLE system_setting ( - name TEXT NOT NULL, - value TEXT NOT NULL, +-- memo_relation +CREATE TABLE memo_relation ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), + updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), + memo_id INTEGER NOT NULL, + related_memo_id INTEGER NOT NULL, + type TEXT NOT NULL, description TEXT NOT NULL DEFAULT '', - UNIQUE(name) + UNIQUE(memo_id, related_memo_id, type) ); diff --git a/store/db/migration/prod/0.8/00__migration_history.sql b/store/db/migration/prod/0.8/00__migration_history.sql new file mode 100644 index 00000000..77ff52e7 --- /dev/null +++ b/store/db/migration/prod/0.8/00__migration_history.sql @@ -0,0 +1,5 @@ +-- migration_history +CREATE TABLE IF NOT EXISTS migration_history ( + version TEXT NOT NULL PRIMARY KEY, + created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) +); diff --git a/store/db/migration/prod/0.8/01__memo_relation.sql b/store/db/migration/prod/0.8/01__memo_relation.sql new file mode 100644 index 00000000..c4d617fc --- /dev/null +++ b/store/db/migration/prod/0.8/01__memo_relation.sql @@ -0,0 +1,11 @@ +-- memo_relation +CREATE TABLE memo_relation ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), + updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), + memo_id INTEGER NOT NULL, + related_memo_id INTEGER NOT NULL, + type TEXT NOT NULL, + description TEXT NOT NULL DEFAULT '', + UNIQUE(memo_id, related_memo_id, type) +); diff --git a/store/db/migration_history.go b/store/db/migration_history.go index bf230b00..b8207613 100644 --- a/store/db/migration_history.go +++ b/store/db/migration_history.go @@ -20,7 +20,7 @@ type MigrationHistoryFind struct { } func (db *DB) FindMigrationHistory(ctx context.Context, find *MigrationHistoryFind) (*MigrationHistory, error) { - tx, err := db.Db.Begin() + tx, err := db.Db.BeginTx(ctx, nil) if err != nil { return nil, err } @@ -40,7 +40,7 @@ func (db *DB) FindMigrationHistory(ctx context.Context, find *MigrationHistoryFi } func (db *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) { - tx, err := db.Db.Begin() + tx, err := db.Db.BeginTx(ctx, nil) if err != nil { return nil, err } @@ -111,24 +111,13 @@ func upsertMigrationHistory(ctx context.Context, tx *sql.Tx, upsert *MigrationHi version=EXCLUDED.version RETURNING version, created_ts ` - row, err := tx.QueryContext(ctx, query, upsert.Version) - if err != nil { - return nil, err - } - defer row.Close() - - row.Next() var migrationHistory MigrationHistory - if err := row.Scan( + if err := tx.QueryRowContext(ctx, query, upsert.Version).Scan( &migrationHistory.Version, &migrationHistory.CreatedTs, ); err != nil { return nil, err } - if err := row.Err(); err != nil { - return nil, err - } - return &migrationHistory, nil } diff --git a/store/db/table.go b/store/db/table.go deleted file mode 100644 index dbe05b57..00000000 --- a/store/db/table.go +++ /dev/null @@ -1,65 +0,0 @@ -package db - -import ( - "context" - "database/sql" - "strings" -) - -type Table struct { - Name string - SQL string -} - -//lint:ignore U1000 Ignore unused function temporarily for debugging -//nolint:all -func findTable(ctx context.Context, tx *sql.Tx, tableName string) (*Table, error) { - where, args := []string{"1 = 1"}, []interface{}{} - - where, args = append(where, "type = ?"), append(args, "table") - where, args = append(where, "name = ?"), append(args, tableName) - - query := ` - SELECT - tbl_name, - sql - FROM sqlite_schema - WHERE ` + strings.Join(where, " AND ") - rows, err := tx.QueryContext(ctx, query, args...) - if err != nil { - return nil, err - } - defer rows.Close() - - tableList := make([]*Table, 0) - for rows.Next() { - var table Table - if err := rows.Scan( - &table.Name, - &table.SQL, - ); err != nil { - return nil, err - } - - tableList = append(tableList, &table) - } - - if err := rows.Err(); err != nil { - return nil, err - } - - if len(tableList) == 0 { - return nil, nil - } else { - return tableList[0], nil - } -} - -func createTable(ctx context.Context, tx *sql.Tx, stmt string) error { - _, err := tx.ExecContext(ctx, stmt) - if err != nil { - return err - } - - return nil -}