diff --git a/migrations/drivers.go b/migrations/drivers.go index f19b6c9..800d2a6 100644 --- a/migrations/drivers.go +++ b/migrations/drivers.go @@ -1,94 +1,103 @@ /* * Copyright © 2019 Musing Studio LLC. * * This file is part of WriteFreely. * * WriteFreely is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, included * in the LICENSE file in this source code package. */ package migrations import ( "fmt" ) // TODO: use now() from writefreely pkg func (db *datastore) now() string { if db.driverName == driverSQLite { return "strftime('%Y-%m-%d %H:%M:%S','now')" } return "NOW()" } func (db *datastore) typeInt() string { if db.driverName == driverSQLite { return "INTEGER" } return "INT" } func (db *datastore) typeSmallInt() string { if db.driverName == driverSQLite { return "INTEGER" } return "SMALLINT" } func (db *datastore) typeTinyInt() string { if db.driverName == driverSQLite { return "INTEGER" } return "TINYINT" } func (db *datastore) typeText() string { return "TEXT" } func (db *datastore) typeChar(l int) string { if db.driverName == driverSQLite { return "TEXT" } return fmt.Sprintf("CHAR(%d)", l) } func (db *datastore) typeVarChar(l int) string { if db.driverName == driverSQLite { return "TEXT" } return fmt.Sprintf("VARCHAR(%d)", l) } func (db *datastore) typeBool() string { if db.driverName == driverSQLite { return "INTEGER" } return "TINYINT(1)" } func (db *datastore) typeDateTime() string { return "DATETIME" } +func (db *datastore) typeIntPrimaryKey() string { + if db.driverName == driverSQLite { + // From docs: "In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT + // ROWID tables) which is always a 64-bit signed integer." + return "INTEGER PRIMARY KEY" + } + return "INT AUTO_INCREMENT PRIMARY KEY" +} + func (db *datastore) collateMultiByte() string { if db.driverName == driverSQLite { return "" } return " COLLATE utf8_bin" } func (db *datastore) engine() string { if db.driverName == driverSQLite { return "" } return " ENGINE = InnoDB" } func (db *datastore) after(colName string) string { if db.driverName == driverSQLite { return "" } return " AFTER " + colName } diff --git a/migrations/v13.go b/migrations/v13.go index b6c6c3a..5bb954b 100644 --- a/migrations/v13.go +++ b/migrations/v13.go @@ -1,59 +1,58 @@ /* * Copyright © 2021 A Bunch Tell LLC. * * This file is part of WriteFreely. * * WriteFreely is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, included * in the LICENSE file in this source code package. */ package migrations func supportLetters(db *datastore) error { t, err := db.Begin() if err != nil { t.Rollback() return err } _, err = t.Exec(`CREATE TABLE publishjobs ( - id ` + db.typeInt() + ` auto_increment, + id ` + db.typeIntPrimaryKey() + `, post_id ` + db.typeVarChar(16) + ` not null, action ` + db.typeVarChar(16) + ` not null, - delay ` + db.typeTinyInt() + ` not null, - PRIMARY KEY (id) + delay ` + db.typeTinyInt() + ` not null )`) if err != nil { t.Rollback() return err } _, err = t.Exec(`CREATE TABLE emailsubscribers ( id ` + db.typeChar(8) + ` not null, collection_id ` + db.typeInt() + ` not null, user_id ` + db.typeInt() + ` null, email ` + db.typeVarChar(255) + ` null, subscribed ` + db.typeDateTime() + ` not null, token ` + db.typeChar(16) + ` not null, confirmed ` + db.typeBool() + ` default 0 not null, allow_export ` + db.typeBool() + ` default 0 not null, constraint eu_coll_email unique (collection_id, email), constraint eu_coll_user unique (collection_id, user_id), PRIMARY KEY (id) )`) if err != nil { t.Rollback() return err } err = t.Commit() if err != nil { t.Rollback() return err } return nil }