add basic demo data
This commit is contained in:
27
demodata.sql
Normal file
27
demodata.sql
Normal file
@@ -0,0 +1,27 @@
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'O dingos, o chateaux!','Jean-Patrick Manchette', 6);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Le petit bleu de la côte Ouest','Jean-Patrick Manchette', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'D''un château l''autre','Louis-Ferdinand Céline', 10);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Les dieux ont soif','Anatole France', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Rigodon','Louis-Ferdinand Céline', 10);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Un barrage contre le Pacifique','Marguerite Duras', NULL);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Salammbô','Flaubert Gustave', 8);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Langage Machine','Romain Lucazeau', 5);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'La Ville et les chiens','Mario Vargas Llosa', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'La Fille Du Capitaine','A. Pouchkine', 8);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Aurélien','Louis Aragon', 8);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Duo','Colette', 9);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Gargantua','François Rabelais', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'The Life of Jesus','Ernest Renan', NULL);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'L''Homme sans qualités, tome 1','Robert Musil', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'The Green House','Mario Vargas Llosa', 6);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Le coup de pistolet','Alexandre Pouchkine', 8);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'De sang-froid','Truman Capote', 6);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'La position du tireur couché','Jean-Patrick Manchette', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Vers le Phare','Virginia Woolf', 6);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'L''insoutenable légèreté de l''être', 'Milan Kundera', 8);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Le complot contre l''Amérique','Philip Roth', 6);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Nord','Louis-Ferdinand Céline', 10);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Sa majesté des mouches','William Golding', 5);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Le Pavillon d''or','Yukio Mishima', 8);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Le meurtre d''O-tsuya','Junichiro Tanizaki', 7);
|
||||
INSERT INTO books(created_at, title, author, rating) VALUES ('NOW', 'Dojoji et autres nouvelles','Yukio Mishima', 8);
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
)
|
||||
|
||||
func Initdb(databaseDir string) *gorm.DB {
|
||||
func Initdb(databaseDir string, demoDataPath string) *gorm.DB {
|
||||
createDbFolderIfMissing(databaseDir)
|
||||
db, err := gorm.Open(
|
||||
sqlite.Open(
|
||||
@@ -22,9 +22,23 @@ func Initdb(databaseDir string) *gorm.DB {
|
||||
}
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&model.Book{})
|
||||
var book model.Book
|
||||
queryResult := db.Limit(1).Find(&book)
|
||||
if queryResult.RowsAffected == 0 && demoDataPath != "" {
|
||||
migrateSchema(db, demoDataPath)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func migrateSchema(db *gorm.DB, demoDataPath string) {
|
||||
log.Printf("Loading demo data file %s\n", demoDataPath)
|
||||
data, err := os.ReadFile(demoDataPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
db.Exec(string(data))
|
||||
}
|
||||
|
||||
func createDbFolderIfMissing(databaseDir string) {
|
||||
_, openFileErr := os.Open(databaseDir)
|
||||
if os.IsNotExist(openFileErr) {
|
||||
|
||||
2
main.go
2
main.go
@@ -20,7 +20,7 @@ func GetBookHandler(c *gin.Context, db *gorm.DB) {
|
||||
|
||||
func main() {
|
||||
c := config.LoadConfig("plm.toml")
|
||||
db := db.Initdb(".")
|
||||
db := db.Initdb(".", c.DemoDataPath)
|
||||
r := gin.Default()
|
||||
r.Use(cors.Default()) // All origins allowed by default
|
||||
r.GET("/books", func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user