Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Fix migrations & write new migrations for next version

Smaug#67392ed59e76501a
diff --git a/backend/app/app.go b/backend/app/app.go
index e8982d4b..e29474db 100644
--- a/backend/app/app.go
+++ b/backend/app/app.go
@@ -37,11 +37,11 @@ type App struct {
func InitApp(config Config) *App {
var app App
- app.DB = DBConection(config)
+ app.DB = DBConection(config, false)
app.MinioClient, _ = MinioConnection()
app.Snowflake = utils.NewSnowflake(1609459200000)
app.Config = config
- Migrate(app.DB)
+ Migrate(&config)
return &app
}
diff --git a/backend/app/database.go b/backend/app/database.go
index 13b27223..46a0bd81 100644
--- a/backend/app/database.go
+++ b/backend/app/database.go
@@ -7,7 +7,7 @@ import (
"time"
)
-func DBConection(config Config) (conecction *sql.DB) {
+func DBConection(config Config, multiStatements bool) (conecction *sql.DB) {
Driver := config.Database.Driver
User := os.Getenv("DATABASE_USER")
Password := os.Getenv("DATABASE_PASSWORD")
@@ -15,7 +15,12 @@ func DBConection(config Config) (conecction *sql.DB) {
Port := fmt.Sprint(config.Database.Port)
Database := config.Database.Name
- conection, err := sql.Open(Driver, User+":"+Password+"@tcp("+Host+":"+Port+")/"+Database)
+ multiStatementsConfig := "?multiStatements=false"
+ if multiStatements {
+ multiStatementsConfig = "?multiStatements=true"
+ }
+
+ conection, err := sql.Open(Driver, User+":"+Password+"@tcp("+Host+":"+Port+")/"+Database+multiStatementsConfig)
if err != nil {
panic(err.Error())
diff --git a/backend/app/migrations.go b/backend/app/migrations.go
index a25525fb..17dc20c9 100644
--- a/backend/app/migrations.go
+++ b/backend/app/migrations.go
@@ -1,14 +1,14 @@
package app
import (
- "database/sql"
-
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
-func Migrate(db *sql.DB) {
+func Migrate(config *Config) {
+ db := DBConection(*config, true) // Use multiStatements for migration
+ defer db.Close()
driver, _ := mysql.WithInstance(db, &mysql.Config{})
m, err := migrate.NewWithDatabaseInstance(
"file://migrations",
diff --git a/backend/migrations/000002_change_ids_to_bigints.down.sql b/backend/migrations/000002_change_ids_to_bigints.down.sql
new file mode 100644
index 00000000..b3aaca99
--- /dev/null
+++ b/backend/migrations/000002_change_ids_to_bigints.down.sql
@@ -0,0 +1,65 @@
+-- Drop foreign keys added in the UP migration
+ALTER TABLE categories
+ DROP FOREIGN KEY categories_categories_id_fk,
+ DROP FOREIGN KEY categories_users_id_fk;
+
+ALTER TABLE connections_logs
+ DROP FOREIGN KEY connections_logs_users_id_fk;
+
+ALTER TABLE documents
+ DROP FOREIGN KEY documents_category_ib_fk,
+ DROP FOREIGN KEY documents_parent_ib_fk,
+ DROP FOREIGN KEY documents_users_id_fk;
+
+ALTER TABLE ressources
+ DROP FOREIGN KEY ressources_users_id_fk;
+
+ALTER TABLE sessions
+ DROP FOREIGN KEY users_auth_users_id_fk;
+
+-- Revert columns to VARCHAR(50) or previous types
+ALTER TABLE categories
+ CHANGE COLUMN id id VARCHAR(50) NOT NULL,
+ CHANGE COLUMN parent_id parent_id VARCHAR(50) DEFAULT NULL,
+ CHANGE COLUMN author_id author_id VARCHAR(50) NOT NULL,
+ CHANGE COLUMN workspace_id workspace_id VARCHAR(50) DEFAULT '1';
+
+ALTER TABLE connections_logs
+ CHANGE COLUMN user_id user_id VARCHAR(50) NOT NULL,
+ CHANGE COLUMN document_id document_id VARCHAR(50) DEFAULT NULL;
+
+ALTER TABLE documents
+ CHANGE COLUMN id id VARCHAR(50) NOT NULL,
+ CHANGE COLUMN category category VARCHAR(50) DEFAULT NULL,
+ CHANGE COLUMN parent_id parent_id VARCHAR(50) DEFAULT NULL,
+ CHANGE COLUMN author_id author_id VARCHAR(50) NOT NULL;
+
+ALTER TABLE ressources
+ CHANGE COLUMN id id VARCHAR(50) NOT NULL,
+ CHANGE COLUMN author_id author_id VARCHAR(50) NOT NULL;
+
+ALTER TABLE sessions
+ CHANGE COLUMN id id VARCHAR(50) NOT NULL,
+ CHANGE COLUMN user_id user_id VARCHAR(50) NOT NULL;
+
+ALTER TABLE users
+ CHANGE COLUMN id id VARCHAR(50) NOT NULL;
+
+-- Re-add original foreign key constraints (without cascade if that was the case)
+ALTER TABLE categories
+ ADD CONSTRAINT categories_categories_id_fk FOREIGN KEY (parent_id) REFERENCES categories (id),
+ ADD CONSTRAINT categories_users_id_fk FOREIGN KEY (author_id) REFERENCES users (id);
+
+ALTER TABLE connections_logs
+ ADD CONSTRAINT connections_logs_users_id_fk FOREIGN KEY (user_id) REFERENCES users (id);
+
+ALTER TABLE documents
+ ADD CONSTRAINT documents_ibfk_2 FOREIGN KEY (category) REFERENCES categories (id),
+ ADD CONSTRAINT documents_ibfk_3 FOREIGN KEY (parent_id) REFERENCES documents (id),
+ ADD CONSTRAINT documents_users_id_fk FOREIGN KEY (author_id) REFERENCES users (id);
+
+ALTER TABLE ressources
+ ADD CONSTRAINT ressources_users_id_fk FOREIGN KEY (author_id) REFERENCES users (id);
+
+ALTER TABLE sessions
+ ADD CONSTRAINT users_auth_users_id_fk FOREIGN KEY (user_id) REFERENCES users (id);
diff --git a/backend/migrations/000002_change_ids_to_bigints.up.sql b/backend/migrations/000002_change_ids_to_bigints.up.sql
new file mode 100644
index 00000000..86f19aa8
--- /dev/null
+++ b/backend/migrations/000002_change_ids_to_bigints.up.sql
@@ -0,0 +1,65 @@
+-- Drop FOREIGN KEY constraints and indexes before changing the column type
+-- and then re-add them after the change
+
+ALTER TABLE categories
+ DROP FOREIGN KEY categories_categories_id_fk,
+ DROP FOREIGN KEY categories_users_id_fk;
+
+ALTER TABLE connections_logs
+ DROP FOREIGN KEY connections_logs_users_id_fk;
+
+ALTER TABLE documents
+ DROP FOREIGN KEY documents_ibfk_2,
+ DROP FOREIGN KEY documents_ibfk_3,
+ DROP FOREIGN KEY documents_users_id_fk;
+
+ALTER TABLE ressources
+ DROP FOREIGN KEY ressources_users_id_fk;
+
+-- Change the column type from varchar(50) to bigint(20) unsigned
+ALTER TABLE categories
+ CHANGE COLUMN id id BIGINT(20) UNSIGNED NOT NULL,
+ CHANGE COLUMN parent_id parent_id BIGINT(20) UNSIGNED DEFAULT NULL,
+ CHANGE COLUMN author_id author_id BIGINT(20) UNSIGNED NOT NULL,
+ CHANGE COLUMN workspace_id workspace_id BIGINT(20) UNSIGNED DEFAULT '1';
+
+ALTER TABLE connections_logs
+ CHANGE COLUMN id id BIGINT(20) UNSIGNED NOT NULL,
+ CHANGE COLUMN user_id user_id BIGINT(20) UNSIGNED NOT NULL;
+
+ALTER TABLE documents
+ CHANGE COLUMN id id BIGINT(20) UNSIGNED NOT NULL,
+ CHANGE COLUMN category category BIGINT(20) UNSIGNED DEFAULT NULL,
+ CHANGE COLUMN parent_id parent_id BIGINT(20) UNSIGNED DEFAULT NULL,
+ CHANGE COLUMN author_id author_id BIGINT(20) UNSIGNED NOT NULL;
+
+ALTER TABLE ressources
+ CHANGE COLUMN id id BIGINT(20) UNSIGNED NOT NULL,
+ CHANGE COLUMN author_id author_id BIGINT(20) UNSIGNED NOT NULL;
+
+ALTER TABLE `sessions`
+ CHANGE COLUMN id id BIGINT(20) UNSIGNED NOT NULL,
+ CHANGE COLUMN user_id user_id BIGINT(20) UNSIGNED NOT NULL;
+
+ALTER TABLE users
+ CHANGE COLUMN id id BIGINT(20) UNSIGNED NOT NULL;
+
+-- Re-add the foreign key constraints and indexes, add also on delete cascade
+
+ALTER TABLE categories
+ ADD CONSTRAINT categories_categories_id_fk FOREIGN KEY (parent_id) REFERENCES categories (id) ON DELETE CASCADE,
+ ADD CONSTRAINT categories_users_id_fk FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE CASCADE;
+
+ALTER TABLE connections_logs
+ ADD CONSTRAINT connections_logs_users_id_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
+
+ALTER TABLE documents
+ ADD CONSTRAINT documents_category_ib_fk FOREIGN KEY (category) REFERENCES categories (id) ON DELETE CASCADE,
+ ADD CONSTRAINT documents_parent_ib_fk FOREIGN KEY (parent_id) REFERENCES documents (id) ON DELETE CASCADE,
+ ADD CONSTRAINT documents_users_id_fk FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE CASCADE;
+
+ALTER TABLE ressources
+ ADD CONSTRAINT ressources_users_id_fk FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE CASCADE;
+
+ALTER TABLE `sessions`
+ ADD CONSTRAINT users_auth_users_id_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
\ No newline at end of file