A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
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.
 
 
 
 
 

55 lines
1.6 KiB

  1. /*
  2. * Copyright © 2019-2021 Musing Studio LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package migrations
  11. import (
  12. "context"
  13. "database/sql"
  14. wf_db "github.com/writefreely/writefreely/db"
  15. )
  16. func oauth(db *datastore) error {
  17. dialect := wf_db.DialectMySQL
  18. if db.driverName == driverSQLite {
  19. dialect = wf_db.DialectSQLite
  20. }
  21. return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
  22. createTableUsersOauth, err := dialect.
  23. Table("oauth_users").
  24. SetIfNotExists(false).
  25. Column(dialect.Column("user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
  26. Column(dialect.Column("remote_user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
  27. ToSQL()
  28. if err != nil {
  29. return err
  30. }
  31. createTableOauthClientState, err := dialect.
  32. Table("oauth_client_states").
  33. SetIfNotExists(false).
  34. Column(dialect.Column("state", wf_db.ColumnTypeVarChar, wf_db.OptionalInt{Set: true, Value: 255})).
  35. Column(dialect.Column("used", wf_db.ColumnTypeBool, wf_db.UnsetSize)).
  36. Column(dialect.Column("created_at", wf_db.ColumnTypeDateTime, wf_db.UnsetSize).SetDefaultCurrentTimestamp()).
  37. UniqueConstraint("state").
  38. ToSQL()
  39. if err != nil {
  40. return err
  41. }
  42. for _, table := range []string{createTableUsersOauth, createTableOauthClientState} {
  43. if _, err := tx.ExecContext(ctx, table); err != nil {
  44. return err
  45. }
  46. }
  47. return nil
  48. })
  49. }