2020-03-03 17:47:38 +00:00
|
|
|
/*
|
2021-04-06 21:24:07 +00:00
|
|
|
* Copyright © 2019-2021 A Bunch Tell LLC.
|
2020-03-03 17:47:38 +00:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2021-04-06 21:24:07 +00:00
|
|
|
wf_db "github.com/writefreely/writefreely/db"
|
2019-12-23 19:30:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func oauth(db *datastore) error {
|
|
|
|
dialect := wf_db.DialectMySQL
|
|
|
|
if db.driverName == driverSQLite {
|
|
|
|
dialect = wf_db.DialectSQLite
|
|
|
|
}
|
|
|
|
return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
createTableUsersOauth, err := dialect.
|
2019-12-31 16:28:05 +00:00
|
|
|
Table("oauth_users").
|
2020-03-03 17:48:04 +00:00
|
|
|
SetIfNotExists(false).
|
2019-12-23 19:30:32 +00:00
|
|
|
Column(dialect.Column("user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
|
|
|
|
Column(dialect.Column("remote_user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
|
|
|
|
ToSQL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createTableOauthClientState, err := dialect.
|
2019-12-31 16:28:05 +00:00
|
|
|
Table("oauth_client_states").
|
2020-03-03 17:48:04 +00:00
|
|
|
SetIfNotExists(false).
|
2019-12-23 19:30:32 +00:00
|
|
|
Column(dialect.Column("state", wf_db.ColumnTypeVarChar, wf_db.OptionalInt{Set: true, Value: 255})).
|
|
|
|
Column(dialect.Column("used", wf_db.ColumnTypeBool, wf_db.UnsetSize)).
|
2020-03-03 17:26:23 +00:00
|
|
|
Column(dialect.Column("created_at", wf_db.ColumnTypeDateTime, wf_db.UnsetSize).SetDefaultCurrentTimestamp()).
|
2019-12-23 19:30:32 +00:00
|
|
|
UniqueConstraint("state").
|
|
|
|
ToSQL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range []string{createTableUsersOauth, createTableOauthClientState} {
|
|
|
|
if _, err := tx.ExecContext(ctx, table); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|