Browse Source

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
tags/v0.8.0
Matt Baer 5 years ago
parent
commit
3a6118c207
4 changed files with 47 additions and 0 deletions
  1. +10
    -0
      app.go
  2. +13
    -0
      migrations/migrations.go
  3. +12
    -0
      schema.sql
  4. +12
    -0
      sqlite.sql

+ 10
- 0
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)
}

+ 13
- 0
migrations/migrations.go 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


+ 12
- 0
schema.sql View File

@@ -35,6 +35,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`
--



+ 12
- 0
sqlite.sql View File

@@ -33,6 +33,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
--



Loading…
Cancel
Save