Skip to content

Commit

Permalink
fix(db): don't drop tables
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jul 27, 2023
1 parent d9a682f commit aebc847
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 33 deletions.
7 changes: 7 additions & 0 deletions cmd/soft/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -146,6 +148,11 @@ func newDefaultLogger(cfg *config.Config) (*log.Logger, *os.File, error) {
func initBackendContext(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
cfg := config.FromContext(ctx)
if _, err := os.Stat(cfg.DataPath); errors.Is(err, fs.ErrNotExist) {
if err := os.MkdirAll(cfg.DataPath, os.ModePerm); err != nil {
return fmt.Errorf("create data directory: %w", err)
}
}
dbx, err := db.Open(ctx, cfg.DB.Driver, cfg.DB.DataSource)
if err != nil {
return fmt.Errorf("open database: %w", err)
Expand Down
5 changes: 0 additions & 5 deletions server/db/migrate/0001_create_tables_postgres.down.sql
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
DROP TABLE IF EXISTS collabs;
DROP TABLE IF EXISTS repos;
DROP TABLE IF EXISTS public_keys;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS settings;
5 changes: 0 additions & 5 deletions server/db/migrate/0001_create_tables_sqlite.down.sql
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
DROP TABLE IF EXISTS collabs;
DROP TABLE IF EXISTS repos;
DROP TABLE IF EXISTS public_keys;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS settings;
2 changes: 0 additions & 2 deletions server/db/migrate/0002_create_lfs_tables_postgres.down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
DROP TABLE IF EXISTS lfs_locks;
DROP TABLE IF EXISTS lfs_objects;
2 changes: 1 addition & 1 deletion server/db/migrate/0002_create_lfs_tables_postgres.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CREATE TABLE IF NOT EXISTS lfs_locks (
CONSTRAINT repo_id_fk
FOREIGN KEY(repo_id) REFERENCES repos(id)
ON DELETE CASCADE
ON UPDATE CASCADE
ON UPDATE CASCADE,
CONSTRAINT user_id_fk
FOREIGN KEY(user_id) REFERENCES users(id)
ON DELETE CASCADE
Expand Down
2 changes: 0 additions & 2 deletions server/db/migrate/0002_create_lfs_tables_sqlite.down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
DROP TABLE IF EXISTS lfs_locks;
DROP TABLE IF EXISTS lfs_objects;
2 changes: 0 additions & 2 deletions server/db/migrate/0003_password_tokens_postgres.down.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
DROP TABLE IF EXISTS access_tokens;

ALTER TABLE users DROP COLUMN password;

16 changes: 1 addition & 15 deletions server/db/migrate/0003_password_tokens_sqlite.down.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
DROP TABLE IF EXISTS access_tokens;
DROP TABLE IF EXISTS users_old;

ALTER TABLE users RENAME TO users_old;

CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
admin BOOLEAN NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL
);

INSERT INTO users (username, admin, created_at, updated_at)
SELECT username, admin, created_at, updated_at FROM users_old;
ALTER TABLE users DROP COLUMN password;
2 changes: 1 addition & 1 deletion server/db/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Rollback(ctx context.Context, dbx *db.DB) error {
}
}

if len(migrations) < int(migrs.Version) {
if migrs.Version == 0 || len(migrations) < int(migrs.Version) {
return fmt.Errorf("there are no migrations to rollback")
}

Expand Down

0 comments on commit aebc847

Please sign in to comment.