func()

in src/middleware/db.go [134:173]


func (s *SQLStorage) SchemaInit() error {
	sl := make([]string, 0)

	sl = append(sl, `CREATE TABLE todo (
		id SERIAL PRIMARY KEY,
		title varchar(512) DEFAULT NULL,
		updated timestamp DEFAULT NULL,
		completed timestamp DEFAULT NULL)`)
	sl = append(sl, `INSERT INTO todo (id, title, updated, completed)
					VALUES
	  				(1,'Install and configure todo app','2021-10-28 12:00:00','2021-10-28 12:00:00'),
					(2,'Add your own todo','2021-10-28 12:00:00',NULL),
					(3,'Mark task 1 done','2021-10-27 14:26:00',NULL)`)

	sl = append(sl, `SELECT setval('todo_id_seq', (SELECT MAX(id) FROM todo)+1)`)

	// Get new Transaction. See http://golang.org/pkg/database/sql/#DB.Begin
	txn, err := s.db.Begin()
	if err != nil {
		return err
	}

	defer func() {
		// Rollback the transaction after the function returns.
		// If the transaction was already commited, this will do nothing.
		_ = txn.Rollback()
	}()

	for _, q := range sl {
		// Execute the query in the transaction.
		s.log(fmt.Sprintf("Executing sql: %s", q))
		_, err := txn.Exec(q)
		if err != nil {
			return err
		}
	}

	// Commit the transaction.
	return txn.Commit()
}