feat(db): split migrations into dialect-specific sqlite and postgres packages

This commit is contained in:
ryan
2026-08-28 15:36:03 +08:00
parent 06508b6f13
commit 692b4b4851
29 changed files with 830 additions and 99 deletions
@@ -1,4 +1,5 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS w_schedules (
id BIGINT PRIMARY KEY,
name VARCHAR(128) NOT NULL,
@@ -15,6 +16,9 @@ CREATE INDEX IF NOT EXISTS idx_w_schedules_is_active ON w_schedules (is_active);
INSERT INTO w_schedules (id, name, task_type, cron, payload, is_active, created_at, updated_at)
VALUES (1, '系统定期垃圾清理', 'system_cleanup', '0 3 * * *', '{}', TRUE, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;
-- +goose StatementEnd
-- +goose Down
DROP TABLE IF EXISTS w_schedules;
-- +goose StatementBegin
DROP TABLE IF EXISTS w_schedules;
-- +goose StatementEnd
@@ -0,0 +1,24 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS w_schedules (
id BIGINT PRIMARY KEY,
name VARCHAR(128) NOT NULL,
task_type VARCHAR(64) NOT NULL,
cron VARCHAR(64) NOT NULL,
payload TEXT,
is_active BOOLEAN NOT NULL DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_w_schedules_is_active ON w_schedules (is_active);
-- Seed initial cleanup task
INSERT INTO w_schedules (id, name, task_type, cron, payload, is_active, created_at, updated_at)
VALUES (1, '系统定期垃圾清理', 'system_cleanup', '0 3 * * *', '{}', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS w_schedules;
-- +goose StatementEnd
@@ -16,9 +16,10 @@ import (
"Wavelet/core"
"Wavelet/core/contracts"
"Wavelet/pkg/config"
)
//go:embed migrations/*.sql
//go:embed migrations/*/*.sql
var cronMigrations embed.FS
// Option configures the Asynq cron scheduler driver plugin.
@@ -148,7 +149,25 @@ func (p *Plugin) Start(_ context.Context) error {
opts.Location = p.location
}
p.scheduler = asynq.NewScheduler(p.redisOpt, opts)
opt := p.redisOpt
if opt == nil {
if RedisOpt != nil {
opt = RedisOpt
} else {
redisCfg := config.Config.Redis
addr := "127.0.0.1:6379"
if len(redisCfg.Addrs) > 0 && redisCfg.Addrs[0] != "" {
addr = redisCfg.Addrs[0]
}
opt = asynq.RedisClientOpt{
Addr: addr,
Username: redisCfg.Username,
Password: redisCfg.Password,
DB: redisCfg.DB,
}
}
}
p.scheduler = asynq.NewScheduler(opt, opts)
}
if p.coreCtx != nil && p.coreCtx.Schedules() != nil {