-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from hhertout/dev
feat: setting up SQLite & migrations process
- Loading branch information
Showing
12 changed files
with
145 additions
and
13 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
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,12 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"os" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func Connect() (*sql.DB, error) { | ||
return sql.Open("sqlite3", os.Getenv("DB_URL")) | ||
} |
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,57 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Migration struct { | ||
dbPool *sql.DB | ||
} | ||
|
||
func NewMigration(db *sql.DB) *Migration { | ||
return &Migration{ | ||
db, | ||
} | ||
} | ||
|
||
func (m Migration) Migrate() error { | ||
if err := m.migrateFromFile("mail.sql"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (m Migration) migrateFromFile(filename string) error { | ||
workingDir, _ := os.Getwd() | ||
fileOpen, err := os.Open(workingDir + "/cmd/database/migrations/" + filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer fileOpen.Close() | ||
|
||
content, err := io.ReadAll(fileOpen) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
queries := string(content) | ||
queriesSplit := strings.Split(queries, "--") | ||
|
||
for _, query := range queriesSplit { | ||
if strings.TrimSpace(query) == "" { | ||
continue | ||
} | ||
|
||
_, err = m.dbPool.Exec(query + ";") | ||
if err != nil { | ||
if err.Error() == "trigger set_viewed_param already exists" { | ||
continue | ||
} | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,4 @@ | ||
internal.sql | ||
backup.sql | ||
old.sql | ||
test.sql |
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,28 @@ | ||
/* Caution : each query must be separated with empty comment */ | ||
|
||
CREATE TABLE IF NOT EXISTS mail | ||
( | ||
_id integer PRIMARY KEY AUTOINCREMENT not null, | ||
date date, | ||
"to" varchar(255) not null, | ||
subject varchar(255) not null, | ||
sent boolean not null, | ||
error text, | ||
viewed boolean | ||
); | ||
|
||
-- | ||
|
||
CREATE TRIGGER set_viewed_param | ||
BEFORE INSERT | ||
ON "mail" | ||
BEGIN | ||
INSERT INTO "mail" ("to", "subject", "sent", "error", "viewed", "date") | ||
VALUES (NEW."to", | ||
NEW."subject", | ||
NEW."sent", | ||
NEW."error", | ||
CASE WHEN NEW."error" IS NULL THEN 1 ELSE 0 END, | ||
DATE('now')); | ||
SELECT RAISE(IGNORE); | ||
END; |
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 |
---|---|---|
|
@@ -9,7 +9,4 @@ services: | |
ports: | ||
- 4545:4545 | ||
volumes: | ||
- .:/app | ||
- db:/app/data | ||
volumes: | ||
db: | ||
- .:/app |
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