memos/store/db/postgres/migration_history.go
Irving Ou 9c18960f47
feat: support Postgres (#2569)
* skeleton of postgres

skeleton

* Adding Postgres specific db schema sql

* user test passed

* memo store test passed

* tag is working

* update user setting test done

* activity test done

* idp test passed

* inbox test done

* memo_organizer, UNTESTED

* memo relation test passed

* webhook test passed

* system setting test passed

* passed storage test

* pass resource test

* migration_history done

* fix memo_relation_test

* fixing server memo_relation test

* passes memo relation server test

* paess memo test

* final manual testing done

* final fixes

* final fixes cleanup

* sync schema

* lint

* lint

* lint

* lint

* lint
2023-12-03 13:31:29 +08:00

79 lines
1.9 KiB
Go

package postgres
import (
"context"
"time"
"github.com/Masterminds/squirrel"
"github.com/usememos/memos/store"
)
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
qb := squirrel.Select("version", "created_ts").
From("migration_history").
OrderBy("created_ts DESC")
query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql()
if err != nil {
return nil, err
}
rows, err := d.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
list := make([]*store.MigrationHistory, 0)
for rows.Next() {
var migrationHistory store.MigrationHistory
var createdTs time.Time
if err := rows.Scan(&migrationHistory.Version, &createdTs); err != nil {
return nil, err
}
migrationHistory.CreatedTs = createdTs.UnixNano()
list = append(list, &migrationHistory)
}
if err := rows.Err(); err != nil {
return nil, err
}
return list, nil
}
func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.MigrationHistory, error) {
qb := squirrel.Insert("migration_history").
Columns("version").
Values(upsert.Version).
Suffix("ON CONFLICT (version) DO UPDATE SET version = ?", upsert.Version)
query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql()
if err != nil {
return nil, err
}
_, err = d.db.ExecContext(ctx, query, args...)
if err != nil {
return nil, err
}
var migrationHistory store.MigrationHistory
var createdTs time.Time
query, args, err = squirrel.Select("version", "created_ts").
From("migration_history").
Where(squirrel.Eq{"version": upsert.Version}).
PlaceholderFormat(squirrel.Dollar).
ToSql()
if err != nil {
return nil, err
}
if err := d.db.QueryRowContext(ctx, query, args...).Scan(&migrationHistory.Version, &createdTs); err != nil {
return nil, err
}
migrationHistory.CreatedTs = createdTs.UnixNano()
return &migrationHistory, nil
}