db

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2022 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MoveCmd = &cobra.Command{
		Use:   "db-move pg-url [sqlite-path]",
		Short: "move a signald database from sqlite to postgres",
		Long: `move a signald sqlite database into a postgres database.
	If sqlite-path is not specified, the default (~/.config/signald/signald.db) will be used.

	Please note that signald must NOT be running while this command runs.

	After the data is moved, the sqlite file will be deleted`,
		Annotations: map[string]string{common.AnnotationNoSocketConnection: "true"},
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("at least one argument required")
			}
			postgresURL = args[0]
			if len(args) > 1 {
				sqlitePath = args[1]
			} else {
				usr, _ := user.Current()
				sqlitePath = fmt.Sprintf("%s/.config/signald/signald.db", usr.HomeDir)
			}
			return nil
		},
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			source, err := sql.Open("sqlite3", sqlitePath)
			if err != nil {
				return err
			}
			defer source.Close()

			if err := source.Ping(); err != nil {
				log.Println("error connecting to source database")
				return err
			}

			if err := verifyMigration(source); err != nil {
				return err
			}

			dest, err := sql.Open("postgres", postgresURL)
			if err != nil {
				return err
			}
			defer dest.Close()

			if err := dest.Ping(); err != nil {
				log.Println("error connecting to destination database")
				return err
			}

			if err := createSchema(dest); err != nil {
				log.Println("error creating schema in postgres")
				return err
			}
			log.Println("created schema")

			migrate := func(fn func(*sql.DB, *sql.DB) error, targetName string) {
				if err = fn(source, dest); err != nil {
					log.Println("error moving", targetName)
					panic(err)
				}
				log.Println("moved", targetName)
			}
			defer func() {
				if r := recover(); r != nil && r != err {

					panic(r)
				}
			}()

			migrate(moveAccounts, "accounts table")
			migrate(moveRecipients, "recipients table")
			migrate(movePrekeys, "prekeys table")
			migrate(moveSessions, "sessions table")
			migrate(moveSignedPrekeys, "signed prekeys table")
			migrate(moveIdentityKeys, "identity keys table")
			migrate(moveAccountData, "account data")
			migrate(movePendingAccountData, "pending account data table")
			migrate(moveSenderKeys, "sender keys table")
			migrate(moveSenderKeyShared, "sender key shared table")
			migrate(moveGroups, "groups table")
			migrate(moveGroupCredentials, "group credentials table")
			migrate(moveContacts, "contacts table")
			migrate(moveProfileKeys, "profile keys table")
			migrate(moveProfiles, "profiles tables")
			migrate(moveProfileCapabilities, "profile capabilities tables")
			migrate(moveProfileBadges, "profile badges tables")

			if err := os.Remove(sqlitePath); err != nil {
				log.Println("error deleting sqlite file")
				return err
			}
			log.Println("sqlite file deleted, your data is now in postgres :)")
			return nil
		},
	}
)

Functions

This section is empty.

Types

type Migration

type Migration struct {
	InstalledRank int
	Version       string
	Description   string
	Script        string
	Checksum      int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL