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
pull/73/head
Matt Baer 6 years ago
parent 5e686a5b0f
commit 3a6118c207
  1. 10
      app.go
  2. 13
      migrations/migrations.go
  3. 12
      schema.sql
  4. 12
      sqlite.sql

@ -613,5 +613,15 @@ func adminInitDatabase(app *app) {
log.Info("Created.") 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) os.Exit(0)
} }

@ -58,6 +58,19 @@ var migrations = []Migration{
New("support user invites", supportUserInvites), // -> V1 (v0.8.0) 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 { func Migrate(db *datastore) error {
var version int var version int
var err error var err error

@ -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` -- Table structure for table `collectionattributes`
-- --

@ -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 -- Table structure for table collectionattributes
-- --

Loading…
Cancel
Save