2019-05-23 08:47:20 +00:00
|
|
|
package multidevice
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
import "database/sql"
|
2019-05-23 08:47:20 +00:00
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
type sqlitePersistence struct {
|
2019-05-23 08:47:20 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func newSQLitePersistence(db *sql.DB) *sqlitePersistence {
|
|
|
|
return &sqlitePersistence{db: db}
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetActiveInstallations returns the active installations for a given identity
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) GetActiveInstallations(maxInstallations int, identity []byte) ([]*Installation, error) {
|
2019-05-23 08:47:20 +00:00
|
|
|
stmt, err := s.db.Prepare(`SELECT installation_id, version
|
|
|
|
FROM installations
|
|
|
|
WHERE enabled = 1 AND identity = ?
|
|
|
|
ORDER BY timestamp DESC
|
|
|
|
LIMIT ?`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var installations []*Installation
|
|
|
|
rows, err := stmt.Query(identity, maxInstallations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
2019-06-26 09:32:59 +00:00
|
|
|
var (
|
|
|
|
installationID string
|
|
|
|
version uint32
|
|
|
|
)
|
2019-05-23 08:47:20 +00:00
|
|
|
err = rows.Scan(
|
|
|
|
&installationID,
|
|
|
|
&version,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
installations = append(installations, &Installation{
|
|
|
|
ID: installationID,
|
|
|
|
Version: version,
|
2019-06-26 09:32:59 +00:00
|
|
|
Enabled: true,
|
2019-05-23 08:47:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return installations, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-26 09:32:59 +00:00
|
|
|
// GetInstallations returns all the installations for a given identity
|
|
|
|
// we both return the installations & the metadata
|
|
|
|
// metadata is currently stored in a separate table, as in some cases we
|
|
|
|
// might have metadata for a device, but no other information on the device
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) GetInstallations(identity []byte) ([]*Installation, error) {
|
2019-06-26 09:32:59 +00:00
|
|
|
installationMap := make(map[string]*Installation)
|
|
|
|
var installations []*Installation
|
|
|
|
|
|
|
|
// We query both tables as sqlite does not support full outer joins
|
|
|
|
installationsStmt, err := s.db.Prepare(`SELECT installation_id, version, enabled, timestamp FROM installations WHERE identity = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer installationsStmt.Close()
|
|
|
|
|
|
|
|
installationRows, err := installationsStmt.Query(identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for installationRows.Next() {
|
2019-07-01 14:28:13 +00:00
|
|
|
var installation Installation
|
2019-06-26 09:32:59 +00:00
|
|
|
err = installationRows.Scan(
|
2019-07-01 14:28:13 +00:00
|
|
|
&installation.ID,
|
|
|
|
&installation.Version,
|
|
|
|
&installation.Enabled,
|
|
|
|
&installation.Timestamp,
|
2019-06-26 09:32:59 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-01 14:28:13 +00:00
|
|
|
// We initialized to empty in this case as we want to
|
|
|
|
// return metadata as well in this endpoint, but not in others
|
|
|
|
installation.InstallationMetadata = &InstallationMetadata{}
|
|
|
|
installationMap[installation.ID] = &installation
|
2019-06-26 09:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metadataStmt, err := s.db.Prepare(`SELECT installation_id, name, device_type, fcm_token FROM installation_metadata WHERE identity = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer metadataStmt.Close()
|
|
|
|
|
|
|
|
metadataRows, err := metadataStmt.Query(identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for metadataRows.Next() {
|
|
|
|
var (
|
|
|
|
installationID string
|
|
|
|
name sql.NullString
|
|
|
|
deviceType sql.NullString
|
|
|
|
fcmToken sql.NullString
|
|
|
|
installation *Installation
|
|
|
|
)
|
|
|
|
err = metadataRows.Scan(
|
|
|
|
&installationID,
|
|
|
|
&name,
|
|
|
|
&deviceType,
|
|
|
|
&fcmToken,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, ok := installationMap[installationID]; ok {
|
|
|
|
installation = installationMap[installationID]
|
|
|
|
} else {
|
|
|
|
installation = &Installation{ID: installationID}
|
|
|
|
}
|
|
|
|
installation.InstallationMetadata = &InstallationMetadata{
|
|
|
|
Name: name.String,
|
|
|
|
DeviceType: deviceType.String,
|
|
|
|
FCMToken: fcmToken.String,
|
|
|
|
}
|
|
|
|
installationMap[installationID] = installation
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, installation := range installationMap {
|
|
|
|
installations = append(installations, installation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return installations, nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
// AddInstallations adds the installations for a given identity, maintaining the enabled flag
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) AddInstallations(identity []byte, timestamp int64, installations []*Installation, defaultEnabled bool) ([]*Installation, error) {
|
2019-05-23 08:47:20 +00:00
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 09:32:59 +00:00
|
|
|
var insertedInstallations []*Installation
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
for _, installation := range installations {
|
|
|
|
stmt, err := tx.Prepare(`SELECT enabled, version
|
|
|
|
FROM installations
|
|
|
|
WHERE identity = ? AND installation_id = ?
|
|
|
|
LIMIT 1`)
|
|
|
|
if err != nil {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
var oldEnabled bool
|
|
|
|
// We don't override version once we saw one
|
|
|
|
var oldVersion uint32
|
|
|
|
latestVersion := installation.Version
|
|
|
|
|
|
|
|
err = stmt.QueryRow(identity, installation.ID).Scan(&oldEnabled, &oldVersion)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 14:29:14 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
stmt, err = tx.Prepare(`INSERT INTO installations(identity, installation_id, timestamp, enabled, version)
|
|
|
|
VALUES (?, ?, ?, ?, ?)`)
|
2019-05-23 08:47:20 +00:00
|
|
|
if err != nil {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
2019-06-03 14:29:14 +00:00
|
|
|
defer stmt.Close()
|
2019-05-23 08:47:20 +00:00
|
|
|
|
|
|
|
_, err = stmt.Exec(
|
|
|
|
identity,
|
|
|
|
installation.ID,
|
|
|
|
timestamp,
|
2019-06-03 14:29:14 +00:00
|
|
|
defaultEnabled,
|
|
|
|
latestVersion,
|
2019-05-23 08:47:20 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
2019-06-26 09:32:59 +00:00
|
|
|
insertedInstallations = append(insertedInstallations, installation)
|
2019-05-23 08:47:20 +00:00
|
|
|
} else {
|
2019-06-03 14:29:14 +00:00
|
|
|
// We update timestamp if present without changing enabled, only if this is a new bundle
|
|
|
|
// and we set the version to the latest we ever saw
|
|
|
|
if oldVersion > installation.Version {
|
|
|
|
latestVersion = oldVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err = tx.Prepare(`UPDATE installations
|
|
|
|
SET timestamp = ?, enabled = ?, version = ?
|
|
|
|
WHERE identity = ?
|
|
|
|
AND installation_id = ?
|
|
|
|
AND timestamp < ?`)
|
2019-05-23 08:47:20 +00:00
|
|
|
if err != nil {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
2019-06-03 14:29:14 +00:00
|
|
|
defer stmt.Close()
|
2019-05-23 08:47:20 +00:00
|
|
|
|
|
|
|
_, err = stmt.Exec(
|
2019-06-03 14:29:14 +00:00
|
|
|
timestamp,
|
|
|
|
oldEnabled,
|
|
|
|
latestVersion,
|
2019-05-23 08:47:20 +00:00
|
|
|
identity,
|
|
|
|
installation.ID,
|
|
|
|
timestamp,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
_ = tx.Rollback()
|
2019-06-26 09:32:59 +00:00
|
|
|
return nil, err
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 09:32:59 +00:00
|
|
|
return insertedInstallations, nil
|
2019-05-23 08:47:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableInstallation enables the installation
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) EnableInstallation(identity []byte, installationID string) error {
|
2019-05-23 08:47:20 +00:00
|
|
|
stmt, err := s.db.Prepare(`UPDATE installations
|
|
|
|
SET enabled = 1
|
|
|
|
WHERE identity = ? AND installation_id = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(identity, installationID)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableInstallation disable the installation
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) DisableInstallation(identity []byte, installationID string) error {
|
2019-05-23 08:47:20 +00:00
|
|
|
stmt, err := s.db.Prepare(`UPDATE installations
|
|
|
|
SET enabled = 0
|
|
|
|
WHERE identity = ? AND installation_id = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(identity, installationID)
|
|
|
|
return err
|
|
|
|
}
|
2019-06-26 09:32:59 +00:00
|
|
|
|
|
|
|
// SetInstallationMetadata sets the metadata for a given installation
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) SetInstallationMetadata(identity []byte, installationID string, metadata *InstallationMetadata) error {
|
2019-06-26 09:32:59 +00:00
|
|
|
stmt, err := s.db.Prepare(`INSERT INTO installation_metadata(name, device_type, fcm_token, identity, installation_id) VALUES(?,?,?,?,?)`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(metadata.Name, metadata.DeviceType, metadata.FCMToken, identity, installationID)
|
|
|
|
return err
|
|
|
|
}
|