~kota/lists

2912cfea77142c6188d83a95724e3311aef31dca — Dakota Walsh 2 months ago 4aadb98
add database migration system
M main.go => main.go +1 -15
@@ 62,19 62,5 @@ func main() {
}

func openDB(dsn string) (*sql.DB, error) {
	db, err := sql.Open("sqlite3", dsn)
	if err != nil {
		return nil, err
	}
	if err := db.Ping(); err != nil {
		return nil, err
	}
	stmt := `CREATE TABLE IF NOT EXISTS lists (
				name TEXT NOT NULL PRIMARY KEY,
				body TEXT NOT NULL
			);`
	if _, err := db.Exec(stmt); err != nil {
		return nil, err
	}
	return db, nil
	return sql.Open("sqlite3", dsn)
}

A migrations/000001_create_lists_table.down.sql => migrations/000001_create_lists_table.down.sql +1 -0
@@ 0,0 1,1 @@
DROP TABLE IF EXISTS lists;

A migrations/000001_create_lists_table.up.sql => migrations/000001_create_lists_table.up.sql +4 -0
@@ 0,0 1,4 @@
CREATE TABLE IF NOT EXISTS lists (
	name TEXT NOT NULL PRIMARY KEY,
	body TEXT NOT NULL
);