feat: connect to db
This commit is contained in:
59
backend/database/database.go
Normal file
59
backend/database/database.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"sync"
|
||||
|
||||
"git.ipao.vip/rogeecn/mp-qvyun/conf"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
//go:embed migrations/*
|
||||
var MigrationFS embed.FS
|
||||
|
||||
var (
|
||||
mutex sync.Mutex
|
||||
db *sql.DB
|
||||
)
|
||||
|
||||
func Close() error {
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
func GetDB(config conf.Database) (*sql.DB, error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if db != nil {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
once := sync.OnceValues(func() (*sql.DB, error) {
|
||||
log.Debugf("connect postgres with dsn: '%s'", config.DSN())
|
||||
db, err := sql.Open("postgres", config.DSN())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connect database")
|
||||
}
|
||||
|
||||
if err := db.Ping(); err != nil {
|
||||
db.Close()
|
||||
return nil, errors.Wrap(err, "ping database")
|
||||
}
|
||||
|
||||
return db, err
|
||||
})
|
||||
|
||||
var err error
|
||||
db, err = once()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, err
|
||||
}
|
||||
9
backend/database/migrations/20241128075611_init.sql
Normal file
9
backend/database/migrations/20241128075611_init.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
SELECT 'up SQL query';
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
SELECT 'down SQL query';
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type ChannelMessages struct {
|
||||
ID int64 `sql:"primary_key"`
|
||||
ChannelID int64
|
||||
UUID int64
|
||||
Content *string
|
||||
Media string
|
||||
PublishedAt time.Time
|
||||
CreatedAt time.Time
|
||||
GroupID int64
|
||||
Published bool
|
||||
Favorite bool
|
||||
}
|
||||
24
backend/database/telegram_resource/public/model/channels.go
Normal file
24
backend/database/telegram_resource/public/model/channels.go
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Channels struct {
|
||||
ID int64 `sql:"primary_key"`
|
||||
UUID int64
|
||||
Username string
|
||||
Title string
|
||||
CreatedAt *time.Time
|
||||
UpdatedAt *time.Time
|
||||
Offset int64
|
||||
MinID int64
|
||||
ExportMedia bool
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
)
|
||||
|
||||
var ChannelMessages = newChannelMessagesTable("public", "channel_messages", "")
|
||||
|
||||
type channelMessagesTable struct {
|
||||
postgres.Table
|
||||
|
||||
// Columns
|
||||
ID postgres.ColumnInteger
|
||||
ChannelID postgres.ColumnInteger
|
||||
UUID postgres.ColumnInteger
|
||||
Content postgres.ColumnString
|
||||
Media postgres.ColumnString
|
||||
PublishedAt postgres.ColumnTimestampz
|
||||
CreatedAt postgres.ColumnTimestampz
|
||||
GroupID postgres.ColumnInteger
|
||||
Published postgres.ColumnBool
|
||||
Favorite postgres.ColumnBool
|
||||
|
||||
AllColumns postgres.ColumnList
|
||||
MutableColumns postgres.ColumnList
|
||||
}
|
||||
|
||||
type ChannelMessagesTable struct {
|
||||
channelMessagesTable
|
||||
|
||||
EXCLUDED channelMessagesTable
|
||||
}
|
||||
|
||||
// AS creates new ChannelMessagesTable with assigned alias
|
||||
func (a ChannelMessagesTable) AS(alias string) *ChannelMessagesTable {
|
||||
return newChannelMessagesTable(a.SchemaName(), a.TableName(), alias)
|
||||
}
|
||||
|
||||
// Schema creates new ChannelMessagesTable with assigned schema name
|
||||
func (a ChannelMessagesTable) FromSchema(schemaName string) *ChannelMessagesTable {
|
||||
return newChannelMessagesTable(schemaName, a.TableName(), a.Alias())
|
||||
}
|
||||
|
||||
// WithPrefix creates new ChannelMessagesTable with assigned table prefix
|
||||
func (a ChannelMessagesTable) WithPrefix(prefix string) *ChannelMessagesTable {
|
||||
return newChannelMessagesTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
|
||||
}
|
||||
|
||||
// WithSuffix creates new ChannelMessagesTable with assigned table suffix
|
||||
func (a ChannelMessagesTable) WithSuffix(suffix string) *ChannelMessagesTable {
|
||||
return newChannelMessagesTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
|
||||
}
|
||||
|
||||
func newChannelMessagesTable(schemaName, tableName, alias string) *ChannelMessagesTable {
|
||||
return &ChannelMessagesTable{
|
||||
channelMessagesTable: newChannelMessagesTableImpl(schemaName, tableName, alias),
|
||||
EXCLUDED: newChannelMessagesTableImpl("", "excluded", ""),
|
||||
}
|
||||
}
|
||||
|
||||
func newChannelMessagesTableImpl(schemaName, tableName, alias string) channelMessagesTable {
|
||||
var (
|
||||
IDColumn = postgres.IntegerColumn("id")
|
||||
ChannelIDColumn = postgres.IntegerColumn("channel_id")
|
||||
UUIDColumn = postgres.IntegerColumn("uuid")
|
||||
ContentColumn = postgres.StringColumn("content")
|
||||
MediaColumn = postgres.StringColumn("media")
|
||||
PublishedAtColumn = postgres.TimestampzColumn("published_at")
|
||||
CreatedAtColumn = postgres.TimestampzColumn("created_at")
|
||||
GroupIDColumn = postgres.IntegerColumn("group_id")
|
||||
PublishedColumn = postgres.BoolColumn("published")
|
||||
FavoriteColumn = postgres.BoolColumn("favorite")
|
||||
allColumns = postgres.ColumnList{IDColumn, ChannelIDColumn, UUIDColumn, ContentColumn, MediaColumn, PublishedAtColumn, CreatedAtColumn, GroupIDColumn, PublishedColumn, FavoriteColumn}
|
||||
mutableColumns = postgres.ColumnList{ChannelIDColumn, UUIDColumn, ContentColumn, MediaColumn, PublishedAtColumn, CreatedAtColumn, GroupIDColumn, PublishedColumn, FavoriteColumn}
|
||||
)
|
||||
|
||||
return channelMessagesTable{
|
||||
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
|
||||
|
||||
//Columns
|
||||
ID: IDColumn,
|
||||
ChannelID: ChannelIDColumn,
|
||||
UUID: UUIDColumn,
|
||||
Content: ContentColumn,
|
||||
Media: MediaColumn,
|
||||
PublishedAt: PublishedAtColumn,
|
||||
CreatedAt: CreatedAtColumn,
|
||||
GroupID: GroupIDColumn,
|
||||
Published: PublishedColumn,
|
||||
Favorite: FavoriteColumn,
|
||||
|
||||
AllColumns: allColumns,
|
||||
MutableColumns: mutableColumns,
|
||||
}
|
||||
}
|
||||
99
backend/database/telegram_resource/public/table/channels.go
Normal file
99
backend/database/telegram_resource/public/table/channels.go
Normal file
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
)
|
||||
|
||||
var Channels = newChannelsTable("public", "channels", "")
|
||||
|
||||
type channelsTable struct {
|
||||
postgres.Table
|
||||
|
||||
// Columns
|
||||
ID postgres.ColumnInteger
|
||||
UUID postgres.ColumnInteger
|
||||
Username postgres.ColumnString
|
||||
Title postgres.ColumnString
|
||||
CreatedAt postgres.ColumnTimestampz
|
||||
UpdatedAt postgres.ColumnTimestampz
|
||||
Offset postgres.ColumnInteger
|
||||
MinID postgres.ColumnInteger
|
||||
ExportMedia postgres.ColumnBool
|
||||
|
||||
AllColumns postgres.ColumnList
|
||||
MutableColumns postgres.ColumnList
|
||||
}
|
||||
|
||||
type ChannelsTable struct {
|
||||
channelsTable
|
||||
|
||||
EXCLUDED channelsTable
|
||||
}
|
||||
|
||||
// AS creates new ChannelsTable with assigned alias
|
||||
func (a ChannelsTable) AS(alias string) *ChannelsTable {
|
||||
return newChannelsTable(a.SchemaName(), a.TableName(), alias)
|
||||
}
|
||||
|
||||
// Schema creates new ChannelsTable with assigned schema name
|
||||
func (a ChannelsTable) FromSchema(schemaName string) *ChannelsTable {
|
||||
return newChannelsTable(schemaName, a.TableName(), a.Alias())
|
||||
}
|
||||
|
||||
// WithPrefix creates new ChannelsTable with assigned table prefix
|
||||
func (a ChannelsTable) WithPrefix(prefix string) *ChannelsTable {
|
||||
return newChannelsTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
|
||||
}
|
||||
|
||||
// WithSuffix creates new ChannelsTable with assigned table suffix
|
||||
func (a ChannelsTable) WithSuffix(suffix string) *ChannelsTable {
|
||||
return newChannelsTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
|
||||
}
|
||||
|
||||
func newChannelsTable(schemaName, tableName, alias string) *ChannelsTable {
|
||||
return &ChannelsTable{
|
||||
channelsTable: newChannelsTableImpl(schemaName, tableName, alias),
|
||||
EXCLUDED: newChannelsTableImpl("", "excluded", ""),
|
||||
}
|
||||
}
|
||||
|
||||
func newChannelsTableImpl(schemaName, tableName, alias string) channelsTable {
|
||||
var (
|
||||
IDColumn = postgres.IntegerColumn("id")
|
||||
UUIDColumn = postgres.IntegerColumn("uuid")
|
||||
UsernameColumn = postgres.StringColumn("username")
|
||||
TitleColumn = postgres.StringColumn("title")
|
||||
CreatedAtColumn = postgres.TimestampzColumn("created_at")
|
||||
UpdatedAtColumn = postgres.TimestampzColumn("updated_at")
|
||||
OffsetColumn = postgres.IntegerColumn("offset")
|
||||
MinIDColumn = postgres.IntegerColumn("min_id")
|
||||
ExportMediaColumn = postgres.BoolColumn("export_media")
|
||||
allColumns = postgres.ColumnList{IDColumn, UUIDColumn, UsernameColumn, TitleColumn, CreatedAtColumn, UpdatedAtColumn, OffsetColumn, MinIDColumn, ExportMediaColumn}
|
||||
mutableColumns = postgres.ColumnList{UUIDColumn, UsernameColumn, TitleColumn, CreatedAtColumn, UpdatedAtColumn, OffsetColumn, MinIDColumn, ExportMediaColumn}
|
||||
)
|
||||
|
||||
return channelsTable{
|
||||
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
|
||||
|
||||
//Columns
|
||||
ID: IDColumn,
|
||||
UUID: UUIDColumn,
|
||||
Username: UsernameColumn,
|
||||
Title: TitleColumn,
|
||||
CreatedAt: CreatedAtColumn,
|
||||
UpdatedAt: UpdatedAtColumn,
|
||||
Offset: OffsetColumn,
|
||||
MinID: MinIDColumn,
|
||||
ExportMedia: ExportMediaColumn,
|
||||
|
||||
AllColumns: allColumns,
|
||||
MutableColumns: mutableColumns,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
//
|
||||
// WARNING: Changes to this file may cause incorrect behavior
|
||||
// and will be lost if the code is regenerated
|
||||
//
|
||||
|
||||
package table
|
||||
|
||||
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
|
||||
// this method only once at the beginning of the program.
|
||||
func UseSchema(schema string) {
|
||||
ChannelMessages = ChannelMessages.FromSchema(schema)
|
||||
Channels = Channels.FromSchema(schema)
|
||||
}
|
||||
Reference in New Issue
Block a user