package migrate import ( "fmt" "strings" ) // Sqlite3 returns a SQLite dialect implementation func Sqlite3() Dialect { return sqlite3{} } type sqlite3 struct{} // quoteIdentifier safely quotes a SQL identifier to prevent SQL injection func (s sqlite3) quoteIdentifier(identifier string) string { // Replace any existing quotes with double quotes (SQL escape mechanism) escaped := strings.ReplaceAll(identifier, `"`, `""`) return fmt.Sprintf(`"%s"`, escaped) } func (s sqlite3) CreateTable(table string) string { return fmt.Sprintf(`CREATE TABLE %s ( id INTEGER PRIMARY KEY AUTOINCREMENT, description VARCHAR, applied TIMESTAMP);`, s.quoteIdentifier(table)) } func (s sqlite3) TableExists(table string) string { return fmt.Sprintf("SELECT * FROM %s;", s.quoteIdentifier(table)) } func (s sqlite3) CheckVersion(table string) string { return fmt.Sprintf("SELECT id FROM %s ORDER BY id DESC LIMIT 0, 1;", s.quoteIdentifier(table)) } func (s sqlite3) InsertVersion(table string) string { return fmt.Sprintf("INSERT INTO %s(description, applied) VALUES (?,?);", s.quoteIdentifier(table)) }