2020-03-03 18:47:38 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2019-2021 Musing Studio LLC.
|
2020-03-03 18:47:38 +01: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 20:30:32 +01:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2021-04-06 23:24:07 +02:00
|
|
|
wf_db "github.com/writefreely/writefreely/db"
|
2019-12-23 20:30:32 +01: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 17:28:05 +01:00
|
|
|
Table("oauth_users").
|
2020-03-03 18:48:04 +01:00
|
|
|
SetIfNotExists(false).
|
2019-12-23 20:30:32 +01: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 17:28:05 +01:00
|
|
|
Table("oauth_client_states").
|
2020-03-03 18:48:04 +01:00
|
|
|
SetIfNotExists(false).
|
2019-12-23 20:30:32 +01: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 18:26:23 +01:00
|
|
|
Column(dialect.Column("created_at", wf_db.ColumnTypeDateTime, wf_db.UnsetSize).SetDefaultCurrentTimestamp()).
|
2019-12-23 20:30:32 +01: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
|
|
|
|
})
|
|
|
|
}
|