Set up migrations table on initial setup

This includes the appmigrations table in the schema files, and inserts
the current database version when running writefreely --init-db
This commit is contained in:
Matt Baer 2019-01-24 17:08:08 -05:00
parent 5e686a5b0f
commit 3a6118c207
4 changed files with 47 additions and 0 deletions

10
app.go
View File

@ -613,5 +613,15 @@ func adminInitDatabase(app *app) {
log.Info("Created.")
}
}
// Set up migrations table
log.Info("Updating appmigrations table...")
err = migrations.SetInitialMigrations(migrations.NewDatastore(app.db.DB, app.db.driverName))
if err != nil {
log.Error("Unable to set initial migrations: %v", err)
os.Exit(1)
}
log.Info("Done.")
os.Exit(0)
}

View File

@ -58,6 +58,19 @@ var migrations = []Migration{
New("support user invites", supportUserInvites), // -> V1 (v0.8.0)
}
// CurrentVer returns the current migration version the application is on
func CurrentVer() int {
return len(migrations)
}
func SetInitialMigrations(db *datastore) error {
_, err := db.Exec("INSERT INTO appmigrations (version, migrated, result) VALUES (?, "+db.now()+", ?)", CurrentVer(), "")
if err != nil {
return err
}
return nil
}
func Migrate(db *datastore) error {
var version int
var err error

View File

@ -34,6 +34,18 @@ CREATE TABLE IF NOT EXISTS `appcontent` (
-- --------------------------------------------------------
--
-- Table structure for table `appmigrations`
--
CREATE TABLE `appmigrations` (
`version` int(11) NOT NULL,
`migrated` datetime NOT NULL,
`result` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `collectionattributes`
--

View File

@ -32,6 +32,18 @@ CREATE TABLE IF NOT EXISTS `appcontent` (
-- --------------------------------------------------------
--
-- Table structure for table appmigrations
--
CREATE TABLE `appmigrations` (
`version` INT NOT NULL,
`migrated` DATETIME NOT NULL,
`result` TEXT NOT NULL
);
-- --------------------------------------------------------
--
-- Table structure for table collectionattributes
--