From 0f8e6004693ab58f14ac2985c521e4480e9c536a Mon Sep 17 00:00:00 2001 From: Artem Titoulenko Date: Fri, 11 Feb 2022 13:28:01 -0500 Subject: [PATCH] db migrations --- Readme.md | 4 +- cmd/.DS_Store | Bin 6148 -> 0 bytes cmd/bun/main.go | 235 ------------------ cmd/bun/migrations/20210505110026_init.go | 19 -- cmd/migrate/main.go | 118 +++++++++ .../migrations/20210505110026_fixtures.yml} | 4 +- cmd/migrate/migrations/20210505110026_init.go | 28 +++ .../migrations/20210505110847_sql.up.sql | 0 cmd/{bun => migrate}/migrations/main.go | 2 +- env/example.dev.env | 6 + main.go | 2 +- run.sh | 5 + 12 files changed, 163 insertions(+), 260 deletions(-) delete mode 100644 cmd/.DS_Store delete mode 100644 cmd/bun/main.go delete mode 100644 cmd/bun/migrations/20210505110026_init.go create mode 100644 cmd/migrate/main.go rename cmd/{bun/migrations/init_fixtures.yml => migrate/migrations/20210505110026_fixtures.yml} (90%) create mode 100644 cmd/migrate/migrations/20210505110026_init.go rename cmd/{bun => migrate}/migrations/20210505110847_sql.up.sql (100%) rename cmd/{bun => migrate}/migrations/main.go (91%) create mode 100644 env/example.dev.env create mode 100755 run.sh diff --git a/Readme.md b/Readme.md index f97184f..e6833e5 100644 --- a/Readme.md +++ b/Readme.md @@ -18,10 +18,10 @@ Run your own AIM chat server, managing users and groups. Hook up a vintage clien ## Getting Started -Clone this repository, make sure you have [Go](https://go.dev/) installed in your terminal's path, then run: +Clone this repository and make sure you have [Go](https://go.dev/) installed in your terminal's path. Copy `env/example.dev.env` to `env/dev.env` and configure your database settings. `OSCAR_BOS_HOST` should be configured to your host's IP or address. In the example dev env it asks macOS what the host's address is but this should be changed if you're on Linux. Then you can run the `run.sh` script to get started. ``` -$ go build && ./aim-oscar +$ ./run.sh ``` ### Configuration diff --git a/cmd/.DS_Store b/cmd/.DS_Store deleted file mode 100644 index 3d01f9e4214fd0981424c209d21095f9d5a0eff2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5Z<*_w-vDmp&s|*t%tTsdlEvd2X8_|4=Qa!iVei1w5dg7B#)tQ+GG@I{ytyEs$*c75Fs@v5YXB5w!#7$;X+nrpYb>=1` z&*", os.Args[0]) + } +} + +func main() { + pgconn := pgdriver.NewConnector( + pgdriver.WithNetwork("tcp"), + pgdriver.WithAddr(DB_URL), + pgdriver.WithTLSConfig(&tls.Config{InsecureSkipVerify: true}), + pgdriver.WithUser(DB_USER), + pgdriver.WithPassword(DB_PASSWORD), + pgdriver.WithDatabase("postgres"), + pgdriver.WithInsecure(true), + pgdriver.WithTimeout(5*time.Second), + pgdriver.WithDialTimeout(5*time.Second), + pgdriver.WithReadTimeout(5*time.Second), + pgdriver.WithWriteTimeout(5*time.Second), + ) + + // Set up the DB + sqldb := sql.OpenDB(pgconn) + db := bun.NewDB(sqldb, pgdialect.New()) + db.SetConnMaxIdleTime(15 * time.Second) + db.SetConnMaxLifetime(1 * time.Minute) + + ctx := context.Background() + cmd := os.Args[1] + migrator := migrate.NewMigrator(db, migrations.Migrations) + + if cmd == "init" { + if err := migrator.Init(ctx); err != nil { + panic(err) + } + } else if cmd == "up" { + group, err := migrator.Migrate(context.Background()) + if err != nil { + panic(err) + } + + if group.ID == 0 { + fmt.Printf("there are no new migrations to run\n") + return + } + + fmt.Printf("migrated to %s\n", group) + } else if cmd == "down" { + group, err := migrator.Rollback(ctx) + if err != nil { + panic(err) + } + + if group.ID == 0 { + fmt.Printf("there are no groups to roll back\n") + return + } + + fmt.Printf("rolled back %s\n", group) + } else if cmd == "status" { + ms, err := migrator.MigrationsWithStatus(ctx) + if err != nil { + panic(err) + } + fmt.Printf("migrations: %s\n", ms) + fmt.Printf("unapplied migrations: %s\n", ms.Unapplied()) + fmt.Printf("last migration group: %s\n", ms.LastGroup()) + } else if cmd == "mark_applied" { + group, err := migrator.Migrate(ctx, migrate.WithNopMigration()) + if err != nil { + panic(err) + } + + if group.ID == 0 { + fmt.Printf("there are no new migrations to mark as applied\n") + return + } + + fmt.Printf("marked as applied %s\n", group) + } +} diff --git a/cmd/bun/migrations/init_fixtures.yml b/cmd/migrate/migrations/20210505110026_fixtures.yml similarity index 90% rename from cmd/bun/migrations/init_fixtures.yml rename to cmd/migrate/migrations/20210505110026_fixtures.yml index f4f5192..b5747d5 100644 --- a/cmd/bun/migrations/init_fixtures.yml +++ b/cmd/migrate/migrations/20210505110026_fixtures.yml @@ -1,11 +1,11 @@ - model: User rows: - - uin: 10000 + - uin: 1 screen_name: toof password: bar email: toof@example.com verified: true - - uin: 10001 + - uin: 2 screen_name: artem password: bar email: artem@example.com diff --git a/cmd/migrate/migrations/20210505110026_init.go b/cmd/migrate/migrations/20210505110026_init.go new file mode 100644 index 0000000..324cd13 --- /dev/null +++ b/cmd/migrate/migrations/20210505110026_init.go @@ -0,0 +1,28 @@ +package migrations + +import ( + "aim-oscar/models" + "context" + + "github.com/uptrace/bun" + "github.com/uptrace/bun/dbfixture" +) + +func init() { + Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error { + db.RegisterModel((*models.User)(nil), (*models.Message)(nil), (*models.Buddy)(nil), (*models.EmailVerification)(nil)) + + fixture := dbfixture.New(db, dbfixture.WithRecreateTables()) + return fixture.Load(context.Background(), sqlMigrations, "20210505110026_fixtures.yml") + }, func(ctx context.Context, db *bun.DB) error { + models := []interface{}{(*models.User)(nil), (*models.Message)(nil), (*models.Buddy)(nil), (*models.EmailVerification)(nil)} + + for _, model := range models { + if _, err := db.NewDropTable().Model(model).Exec(ctx, nil); err != nil { + return err + } + } + + return nil + }) +} diff --git a/cmd/bun/migrations/20210505110847_sql.up.sql b/cmd/migrate/migrations/20210505110847_sql.up.sql similarity index 100% rename from cmd/bun/migrations/20210505110847_sql.up.sql rename to cmd/migrate/migrations/20210505110847_sql.up.sql diff --git a/cmd/bun/migrations/main.go b/cmd/migrate/migrations/main.go similarity index 91% rename from cmd/bun/migrations/main.go rename to cmd/migrate/migrations/main.go index 7dfd5e0..d956d92 100644 --- a/cmd/bun/migrations/main.go +++ b/cmd/migrate/migrations/main.go @@ -8,7 +8,7 @@ import ( var Migrations = migrate.NewMigrations() -//go:embed *.sql +//go:embed *.sql *.yml var sqlMigrations embed.FS func init() { diff --git a/env/example.dev.env b/env/example.dev.env new file mode 100644 index 0000000..79fd1be --- /dev/null +++ b/env/example.dev.env @@ -0,0 +1,6 @@ +export POSTGRES_USER=postgres +export POSTGRES_PASSWORD=password +export DB_URL="$(hostname):5432" +export DB_USER=${POSTGRES_USER} +export DB_PASSWORD=${POSTGRES_PASSWORD} +export OSCAR_BOS_HOST=$(osascript -e "IPv4 address of (system info)") diff --git a/main.go b/main.go index fed2247..b324261 100644 --- a/main.go +++ b/main.go @@ -147,7 +147,7 @@ func main() { serviceManager := NewServiceManager() serviceManager.RegisterService(0x01, &services.GenericServiceControls{OnlineCh: onlineCh, ServerHostname: OSCAR_ADDRESS}) serviceManager.RegisterService(0x02, &services.LocationServices{OnlineCh: onlineCh}) - serviceManager.RegisterService(0x03, &services.BuddyListManagement{}) + serviceManager.RegisterService(0x03, &services.BuddyListManagement{OnlineCh: onlineCh}) serviceManager.RegisterService(0x04, &services.ICBM{CommCh: commCh}) serviceManager.RegisterService(0x17, &services.AuthorizationRegistrationService{BOSAddress: OSCAR_BOS_ADDRESS}) diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a400cf5 --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +source env/dev.env +npm install +npm run dev