Minimal URL shortener
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
804 B

  1. package main
  2. import (
  3. "database/sql"
  4. "log"
  5. "github.com/lopezator/migrator"
  6. )
  7. func migrateDatabase() {
  8. dbWriteLock.Lock()
  9. defer dbWriteLock.Unlock()
  10. m, err := migrator.New(
  11. migrator.Migrations(
  12. &migrator.Migration{
  13. Name: "00001",
  14. Func: func(tx *sql.Tx) error {
  15. _, err := tx.Exec(`
  16. drop table if exists gorp_migrations;
  17. create table if not exists redirect(slug text not null primary key, url text not null, type text not null default 'url', hits integer default 0 not null);
  18. insert or replace into redirect (slug, url) values ('source', 'https://git.jlel.se/jlelse/GoShort');
  19. `)
  20. return err
  21. },
  22. },
  23. ),
  24. )
  25. if err != nil {
  26. log.Fatal(err.Error())
  27. return
  28. }
  29. if err := m.Migrate(appDb); err != nil {
  30. log.Fatal(err.Error())
  31. return
  32. }
  33. return
  34. }