From fc369eb30aeca8560ac73edd6132a77be327d162 Mon Sep 17 00:00:00 2001 From: piraces Date: Thu, 30 Mar 2023 22:41:02 +0200 Subject: [PATCH] fix: migrations --- cmd/rsslay/main.go | 9 ++++++++- scripts/check_nitter_column.sql | 1 + scripts/create_nitter_column.sql | 1 + scripts/schema.sql | 4 ++-- scripts/scripts.go | 6 ++++++ 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 scripts/check_nitter_column.sql create mode 100644 scripts/create_nitter_column.sql diff --git a/cmd/rsslay/main.go b/cmd/rsslay/main.go index d1b5745..d4d20ed 100644 --- a/cmd/rsslay/main.go +++ b/cmd/rsslay/main.go @@ -334,10 +334,17 @@ func InitDatabase(r *Relay) *sql.DB { log.Printf("[INFO] database opened at %s", *finalConnection) - // Run migration. + // Run migrations if _, err := sqlDb.Exec(scripts.SchemaSQL); err != nil { log.Fatalf("[FATAL] cannot migrate schema: %v", err) } + if _, err := sqlDb.Exec(scripts.CheckNitterColumnSQL); err != nil { + _, err := sqlDb.Exec(scripts.CreateNitterColumnSQL) + if err != nil { + log.Fatalf("[FATAL] cannot migrate schema from previous versions: %v", err) + } + } + return sqlDb } diff --git a/scripts/check_nitter_column.sql b/scripts/check_nitter_column.sql new file mode 100644 index 0000000..096a7ea --- /dev/null +++ b/scripts/check_nitter_column.sql @@ -0,0 +1 @@ +SELECT nitter from feeds \ No newline at end of file diff --git a/scripts/create_nitter_column.sql b/scripts/create_nitter_column.sql new file mode 100644 index 0000000..ef95c9e --- /dev/null +++ b/scripts/create_nitter_column.sql @@ -0,0 +1 @@ +ALTER TABLE feeds ADD COLUMN nitter INTEGER DEFAULT 0 \ No newline at end of file diff --git a/scripts/schema.sql b/scripts/schema.sql index 301dba8..a345a22 100644 --- a/scripts/schema.sql +++ b/scripts/schema.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS feeds ( publickey VARCHAR(64) PRIMARY KEY, privatekey VARCHAR(64) NOT NULL, - url TEXT NOT NULL + url TEXT NOT NULL, + nitter INTEGER DEFAULT 0 ); -ALTER TABLE feeds ADD COLUMN nitter INTEGER DEFAULT 0; diff --git a/scripts/scripts.go b/scripts/scripts.go index 7d23065..b68cb14 100644 --- a/scripts/scripts.go +++ b/scripts/scripts.go @@ -4,3 +4,9 @@ import _ "embed" //go:embed schema.sql var SchemaSQL string + +//go:embed check_nitter_column.sql +var CheckNitterColumnSQL string + +//go:embed create_nitter_column.sql +var CreateNitterColumnSQL string