Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions database/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
nurl "net/url"
"strconv"
"strings"
"sync/atomic"
"time"

"go.uber.org/atomic"

"github.com/gocql/gocql"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/multistmt"
Expand Down Expand Up @@ -199,14 +198,14 @@ func (c *Cassandra) Close() error {
}

func (c *Cassandra) Lock() error {
if !c.isLocked.CAS(false, true) {
if !c.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}

func (c *Cassandra) Unlock() error {
if !c.isLocked.CAS(true, false) {
if !c.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions database/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"net/url"
"strconv"
"strings"
"sync/atomic"
"time"

"go.uber.org/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/multistmt"
Expand Down Expand Up @@ -291,14 +290,14 @@ func (ch *ClickHouse) Drop() (err error) {
}

func (ch *ClickHouse) Lock() error {
if !ch.isLocked.CAS(false, true) {
if !ch.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}

return nil
}
func (ch *ClickHouse) Unlock() error {
if !ch.isLocked.CAS(true, false) {
if !ch.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}

Expand Down
2 changes: 1 addition & 1 deletion database/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
nurl "net/url"
"regexp"
"strconv"
"sync/atomic"

"github.com/cockroachdb/cockroach-go/v2/crdb"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/hashicorp/go-multierror"
"github.com/lib/pq"
"go.uber.org/atomic"
)

func init() {
Expand Down
6 changes: 3 additions & 3 deletions database/firebird/firebird.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"fmt"
"io"
nurl "net/url"
"sync/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/hashicorp/go-multierror"
_ "github.com/nakagami/firebirdsql"
"go.uber.org/atomic"
)

func init() {
Expand Down Expand Up @@ -107,14 +107,14 @@ func (f *Firebird) Close() error {
}

func (f *Firebird) Lock() error {
if !f.isLocked.CAS(false, true) {
if !f.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}

func (f *Firebird) Unlock() error {
if !f.isLocked.CAS(true, false) {
if !f.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion database/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"strconv"
"sync/atomic"
"time"

"github.com/cenkalti/backoff/v4"
Expand All @@ -16,7 +17,6 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.uber.org/atomic"
)

func init() {
Expand Down
3 changes: 1 addition & 2 deletions database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
"os"
"strconv"
"strings"
"sync/atomic"
"time"

"go.uber.org/atomic"

"github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate/v4/database"
"github.com/hashicorp/go-multierror"
Expand Down
3 changes: 1 addition & 2 deletions database/pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"

"go.uber.org/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/multistmt"
Expand Down
3 changes: 1 addition & 2 deletions database/pgx/v5/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"

"go.uber.org/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/multistmt"
Expand Down
3 changes: 1 addition & 2 deletions database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"

"go.uber.org/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/multistmt"
Expand Down
6 changes: 3 additions & 3 deletions database/ql/ql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"io"
nurl "net/url"
"strings"
"sync/atomic"

"github.com/hashicorp/go-multierror"
"go.uber.org/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
Expand Down Expand Up @@ -166,13 +166,13 @@ func (m *Ql) Drop() (err error) {
return nil
}
func (m *Ql) Lock() error {
if !m.isLocked.CAS(false, true) {
if !m.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}
func (m *Ql) Unlock() error {
if !m.isLocked.CAS(true, false) {
if !m.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions database/redshift/redshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
nurl "net/url"
"strconv"
"strings"

"go.uber.org/atomic"
"sync/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
Expand Down Expand Up @@ -127,14 +126,14 @@ func (p *Redshift) Close() error {

// Redshift does not support advisory lock functions: https://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-functions.html
func (p *Redshift) Lock() error {
if !p.isLocked.CAS(false, true) {
if !p.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}

func (p *Redshift) Unlock() error {
if !p.isLocked.CAS(true, false) {
if !p.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions database/rqlite/rqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
nurl "net/url"
"strconv"
"strings"

"go.uber.org/atomic"
"sync/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
Expand Down Expand Up @@ -143,7 +142,7 @@ func (r *Rqlite) Close() error {
// If the implementation can't provide this functionality, return nil.
// Return database.ErrLocked if database is already locked.
func (r *Rqlite) Lock() error {
if !r.isLocked.CAS(false, true) {
if !r.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
Expand All @@ -152,7 +151,7 @@ func (r *Rqlite) Lock() error {
// Unlock should release the lock. Migrate will call this function after
// all migrations have been run.
func (r *Rqlite) Unlock() error {
if !r.isLocked.CAS(true, false) {
if !r.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions database/snowflake/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
nurl "net/url"
"strconv"
"strings"

"go.uber.org/atomic"
"sync/atomic"

"github.com/golang-migrate/migrate/v4/database"
"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -159,14 +158,14 @@ func (p *Snowflake) Close() error {
}

func (p *Snowflake) Lock() error {
if !p.isLocked.CAS(false, true) {
if !p.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}

func (p *Snowflake) Unlock() error {
if !p.isLocked.CAS(true, false) {
if !p.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
14 changes: 4 additions & 10 deletions database/spanner/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"regexp"
"strconv"
"strings"
"sync/atomic"

"cloud.google.com/go/spanner"
sdb "cloud.google.com/go/spanner/admin/database/apiv1"
Expand All @@ -20,7 +21,6 @@ import (

adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"github.com/hashicorp/go-multierror"
uatomic "go.uber.org/atomic"
"google.golang.org/api/iterator"
)

Expand All @@ -32,11 +32,6 @@ func init() {
// DefaultMigrationsTable is used if no custom table is specified
const DefaultMigrationsTable = "SchemaMigrations"

const (
unlockedVal = 0
lockedVal = 1
)

// Driver errors
var (
ErrNilConfig = errors.New("no config")
Expand Down Expand Up @@ -64,7 +59,7 @@ type Spanner struct {

config *Config

lock *uatomic.Uint32
lock atomic.Bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this cleanup!

}

type DB struct {
Expand Down Expand Up @@ -96,7 +91,6 @@ func WithInstance(instance *DB, config *Config) (database.Driver, error) {
sx := &Spanner{
db: instance,
config: config,
lock: uatomic.NewUint32(unlockedVal),
}

if err := sx.ensureVersionTable(); err != nil {
Expand Down Expand Up @@ -153,15 +147,15 @@ func (s *Spanner) Close() error {
// Lock implements database.Driver but doesn't do anything because Spanner only
// enqueues the UpdateDatabaseDdlRequest.
func (s *Spanner) Lock() error {
if swapped := s.lock.CAS(unlockedVal, lockedVal); swapped {
if swapped := s.lock.CompareAndSwap(false, true); swapped {
return nil
}
return ErrLockHeld
}

// Unlock implements database.Driver but no action required, see Lock.
func (s *Spanner) Unlock() error {
if swapped := s.lock.CAS(lockedVal, unlockedVal); swapped {
if swapped := s.lock.CompareAndSwap(true, false); swapped {
return nil
}
return ErrLockNotHeld
Expand Down
7 changes: 3 additions & 4 deletions database/sqlcipher/sqlcipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
nurl "net/url"
"strconv"
"strings"

"go.uber.org/atomic"
"sync/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
Expand Down Expand Up @@ -178,14 +177,14 @@ func (m *Sqlite) Drop() (err error) {
}

func (m *Sqlite) Lock() error {
if !m.isLocked.CAS(false, true) {
if !m.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}

func (m *Sqlite) Unlock() error {
if !m.isLocked.CAS(true, false) {
if !m.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions database/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
nurl "net/url"
"strconv"
"strings"

"go.uber.org/atomic"
"sync/atomic"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
Expand Down Expand Up @@ -178,14 +177,14 @@ func (m *Sqlite) Drop() (err error) {
}

func (m *Sqlite) Lock() error {
if !m.isLocked.CAS(false, true) {
if !m.isLocked.CompareAndSwap(false, true) {
return database.ErrLocked
}
return nil
}

func (m *Sqlite) Unlock() error {
if !m.isLocked.CAS(true, false) {
if !m.isLocked.CompareAndSwap(true, false) {
return database.ErrNotLocked
}
return nil
Expand Down
Loading