-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
228 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ncdmv_server | ||
ncdmv.db | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"flag" | ||
"log" | ||
"os" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/sqlite" | ||
"github.com/golang-migrate/migrate/v4/source/file" | ||
) | ||
|
||
const ( | ||
dbPathEnvVar = "NCDMV_DB_PATH" | ||
) | ||
|
||
var ( | ||
down = flag.Bool("down", false, "up or down") | ||
count = flag.Int("count", 0, "number of migrations") | ||
dbPathFlag = flag.String("db_path", "./ncdmv.db", "path to SQLite DB file") | ||
migrationsPath = flag.String("migrations_path", "./migrations", "path to migrations directory") | ||
) | ||
|
||
func main() { | ||
dbPath := os.Getenv(dbPathEnvVar) | ||
if dbPath == "" { | ||
if *dbPathFlag != "" { | ||
dbPath = *dbPathFlag | ||
} else { | ||
log.Fatalf("No DB path specified") | ||
} | ||
} | ||
|
||
db, err := sql.Open("sqlite", dbPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer db.Close() | ||
|
||
instance, err := sqlite.WithInstance(db, &sqlite.Config{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fileSource, err := (&file.File{}).Open(*migrationsPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
m, err := migrate.NewWithInstance("file", fileSource, "sqlite", instance) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if !*down { | ||
if *count == 0 { | ||
if err := m.Up(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} else { | ||
if err := m.Steps(*count); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} else { | ||
if *count == 0 { | ||
if err := m.Down(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} else { | ||
if err := m.Steps(*count * -1); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- BEGIN; | ||
|
||
DROP TABLE IF EXISTS notification; | ||
DROP TABLE IF EXISTS notification_method; | ||
DROP TABLE IF EXISTS appointment; | ||
|
||
-- COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- BEGIN; | ||
|
||
CREATE TABLE appointment ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
location TEXT NOT NULL, | ||
time DATETIME NOT NULL, | ||
create_timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE TABLE notification_method ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
discord_webhook TEXT, | ||
create_timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE TABLE notification ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
appointment_id INTEGER REFERENCES appointment(id) NOT NULL, | ||
notification_method_id INTEGER REFERENCES notification_method(id) NOT NULL, | ||
create_timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- COMMIT; |