summaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/bundb/admin.go5
-rw-r--r--internal/db/bundb/bundb.go32
-rw-r--r--internal/db/bundb/bundb_test.go5
-rw-r--r--internal/db/bundb/migrations/README.md70
-rw-r--r--internal/db/bundb/migrations/main.go28
5 files changed, 135 insertions, 5 deletions
diff --git a/internal/db/bundb/admin.go b/internal/db/bundb/admin.go
index 6a51ffeb1..dd973ef2d 100644
--- a/internal/db/bundb/admin.go
+++ b/internal/db/bundb/admin.go
@@ -29,6 +29,7 @@ import (
"strings"
"time"
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -113,7 +114,7 @@ func (a *adminDB) NewSignup(ctx context.Context, username string, reason string,
PrivateKey: key,
PublicKey: &key.PublicKey,
PublicKeyURI: newAccountURIs.PublicKeyURI,
- ActorType: gtsmodel.ActivityStreamsPerson,
+ ActorType: ap.ActorPerson,
URI: newAccountURIs.UserURI,
InboxURI: newAccountURIs.InboxURI,
OutboxURI: newAccountURIs.OutboxURI,
@@ -207,7 +208,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error {
PrivateKey: key,
PublicKey: &key.PublicKey,
PublicKeyURI: newAccountURIs.PublicKeyURI,
- ActorType: gtsmodel.ActivityStreamsPerson,
+ ActorType: ap.ActorPerson,
URI: newAccountURIs.UserURI,
InboxURI: newAccountURIs.InboxURI,
OutboxURI: newAccountURIs.OutboxURI,
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go
index 6fcc56e51..7ddcab5c7 100644
--- a/internal/db/bundb/bundb.go
+++ b/internal/db/bundb/bundb.go
@@ -37,11 +37,13 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/cache"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/dialect/sqlitedialect"
+ "github.com/uptrace/bun/migrate"
_ "modernc.org/sqlite"
)
@@ -73,6 +75,32 @@ type bunDBService struct {
conn *DBConn
}
+func doMigration(ctx context.Context, db *bun.DB, log *logrus.Logger) error {
+ l := log.WithField("func", "doMigration")
+
+ migrator := migrate.NewMigrator(db, migrations.Migrations)
+
+ if err := migrator.Init(ctx); err != nil {
+ return err
+ }
+
+ group, err := migrator.Migrate(ctx)
+ if err != nil {
+ if err.Error() == "migrate: there are no any migrations" {
+ return nil
+ }
+ return err
+ }
+
+ if group.ID == 0 {
+ l.Info("there are no new migrations to run")
+ return nil
+ }
+
+ l.Infof("MIGRATED DATABASE TO %s", group)
+ return nil
+}
+
// NewBunDBService returns a bunDB derived from the provided config, which implements the go-fed DB interface.
// Under the hood, it uses https://github.com/uptrace/bun to create and maintain a database connection.
func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger) (db.DB, error) {
@@ -130,6 +158,10 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger)
conn.RegisterModel(t)
}
+ if err := doMigration(ctx, conn.DB, log); err != nil {
+ return nil, fmt.Errorf("db migration error: %s", err)
+ }
+
accounts := &accountDB{config: c, conn: conn, cache: cache.NewAccountCache()}
ps := &bunDBService{
diff --git a/internal/db/bundb/bundb_test.go b/internal/db/bundb/bundb_test.go
index b789375af..faa67456d 100644
--- a/internal/db/bundb/bundb_test.go
+++ b/internal/db/bundb/bundb_test.go
@@ -24,7 +24,6 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
- "github.com/superseriousbusiness/gotosocial/internal/oauth"
)
type BunDBStandardTestSuite struct {
@@ -35,8 +34,8 @@ type BunDBStandardTestSuite struct {
log *logrus.Logger
// standard suite models
- testTokens map[string]*oauth.Token
- testClients map[string]*oauth.Client
+ testTokens map[string]*gtsmodel.Token
+ testClients map[string]*gtsmodel.Client
testApplications map[string]*gtsmodel.Application
testUsers map[string]*gtsmodel.User
testAccounts map[string]*gtsmodel.Account
diff --git a/internal/db/bundb/migrations/README.md b/internal/db/bundb/migrations/README.md
new file mode 100644
index 000000000..fee262936
--- /dev/null
+++ b/internal/db/bundb/migrations/README.md
@@ -0,0 +1,70 @@
+# Migrations
+
+## How do I write a migration file?
+
+[See here](https://bun.uptrace.dev/guide/migrations.html#migration-names)
+
+As a template, take one of the existing migration files and modify it, or use the below code snippet:
+
+```go
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package migrations
+
+import (
+ "context"
+
+ "github.com/uptrace/bun"
+)
+
+func init() {
+ up := func(ctx context.Context, db *bun.DB) error {
+ return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+ // your logic here
+ return nil
+ })
+ }
+
+ down := func(ctx context.Context, db *bun.DB) error {
+ return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+ // your logic here
+ return nil
+ })
+ }
+
+ if err := Migrations.Register(up, down); err != nil {
+ panic(err)
+ }
+}
+```
+
+## File format
+
+Bun requires a very specific format: 14 digits, then letters or underscores.
+
+You can use the following bash command on your branch to generate a suitable migration filename.
+
+```bash
+echo "$(date --utc +%Y%m%H%M%S%N | head -c 14)_$(git rev-parse --abbrev-ref HEAD).go"
+```
+
+## Rules of thumb
+
+1. **DON'T DROP TABLES**!!!!!!!!
+2. Don't make something `NOT NULL` if it's likely to already contain `null` fields.
diff --git a/internal/db/bundb/migrations/main.go b/internal/db/bundb/migrations/main.go
new file mode 100644
index 000000000..7f4e76027
--- /dev/null
+++ b/internal/db/bundb/migrations/main.go
@@ -0,0 +1,28 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package migrations
+
+import (
+ "github.com/uptrace/bun/migrate"
+)
+
+var (
+ // Migrations provides migration logic for bun
+ Migrations = migrate.NewMigrations()
+)