π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
Setup migrations system
Smaug#6739784796248f91
diff --git a/backend/app/app.go b/backend/app/app.goindex 0893c7b4..e8982d4b 100644--- a/backend/app/app.go+++ b/backend/app/app.go@@ -42,5 +42,6 @@ func InitApp(config Config) *App {app.Snowflake = utils.NewSnowflake(1609459200000)app.Config = config+ Migrate(app.DB)return &app}diff --git a/backend/app/migrations.go b/backend/app/migrations.gonew file mode 100644index 00000000..a25525fb--- /dev/null+++ b/backend/app/migrations.go@@ -0,0 +1,24 @@+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) {+ driver, _ := mysql.WithInstance(db, &mysql.Config{})+ m, err := migrate.NewWithDatabaseInstance(+ "file://migrations",+ "mysql",+ driver,+ )+ if err != nil {+ panic(err.Error())+ }+ if err := m.Up(); err != nil && err.Error() != "no change" {+ panic(err.Error())+ }+}diff --git a/backend/go.mod b/backend/go.modindex ff48a3bc..976d28bf 100644--- a/backend/go.mod+++ b/backend/go.mod@@ -6,7 +6,10 @@ require github.com/minio/minio-go v6.0.14+incompatiblerequire (github.com/davecgh/go-spew v1.1.1 // indirect+ github.com/hashicorp/errwrap v1.1.0 // indirect+ github.com/hashicorp/go-multierror v1.1.1 // indirectgithub.com/pmezard/go-difflib v1.0.0 // indirect+ go.uber.org/atomic v1.7.0 // indirect)require (@@ -28,6 +31,7 @@ require (github.com/go-sql-driver/mysql v1.9.1 // indirectgithub.com/goccy/go-json v0.10.5 // indirectgithub.com/golang-jwt/jwt/v5 v5.2.2 // indirect+ github.com/golang-migrate/migrate/v4 v4.18.2github.com/google/uuid v1.6.0 // indirectgithub.com/joho/godotenv v1.5.1 // indirectgithub.com/json-iterator/go v1.1.12 // indirectdiff --git a/backend/go.sum b/backend/go.sumindex ece4c644..cf408ab4 100644--- a/backend/go.sum+++ b/backend/go.sum@@ -38,9 +38,16 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=+github.com/golang-migrate/migrate/v4 v4.18.2 h1:2VSCMz7x7mjyTXx3m2zPokOY82LTRgxK1yQYKo6wWQ8=+github.com/golang-migrate/migrate/v4 v4.18.2/go.mod h1:2CM6tJvn2kqPXwnXO/d3rAQYiyoIm180VsO8PRX6Rpk=github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=+github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=+github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=+github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=+github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=+github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=@@ -93,6 +100,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkSgithub.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=+go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=+go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw=golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=diff --git a/backend/migrations/000001_create_users_table.down.sql b/backend/migrations/000001_create_users_table.down.sqlnew file mode 100644index 00000000..59e4c582--- /dev/null+++ b/backend/migrations/000001_create_users_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `users`;\ No newline at end of filediff --git a/backend/migrations/000001_create_users_table.up.sql b/backend/migrations/000001_create_users_table.up.sqlnew file mode 100644index 00000000..5765c42a--- /dev/null+++ b/backend/migrations/000001_create_users_table.up.sql@@ -0,0 +1,13 @@+CREATE TABLE IF NOT EXISTS `users` (+ `id` varchar(50) NOT NULL,+ `username` varchar(25) NOT NULL,+ `firstname` varchar(25) DEFAULT NULL,+ `lastname` varchar(25) DEFAULT NULL,+ `role` int NOT NULL DEFAULT '1',+ `avatar` varchar(75) DEFAULT NULL,+ `email` varchar(50) NOT NULL,+ `password` varchar(255) NOT NULL,+ `created_timestamp` bigint NOT NULL,+ `updated_timestamp` bigint NOT NULL,+ PRIMARY KEY (`id`)+)diff --git a/backend/migrations/000002_create_categories_table.down.sql b/backend/migrations/000002_create_categories_table.down.sqlnew file mode 100644index 00000000..9b7f0d9c--- /dev/null+++ b/backend/migrations/000002_create_categories_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `categories`;\ No newline at end of filediff --git a/backend/migrations/000002_create_categories_table.up.sql b/backend/migrations/000002_create_categories_table.up.sqlnew file mode 100644index 00000000..e040181f--- /dev/null+++ b/backend/migrations/000002_create_categories_table.up.sql@@ -0,0 +1,15 @@+CREATE TABLE IF NOT EXISTS `categories` (+ `id` varchar(50) NOT NULL,+ `name` varchar(50) NOT NULL,+ `icon` text,+ `order` int DEFAULT NULL,+ `role` int NOT NULL DEFAULT '1' COMMENT '1 = category; 2 = workspace',+ `workspace_id` varchar(50) DEFAULT '1',+ `parent_id` varchar(50) DEFAULT NULL,+ `author_id` varchar(50) NOT NULL,+ PRIMARY KEY (`id`),+ KEY `categories_categories_id_fk` (`parent_id`),+ KEY `categories_users_id_fk` (`author_id`),+ CONSTRAINT `categories_categories_id_fk` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`),+ CONSTRAINT `categories_users_id_fk` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`)+)\ No newline at end of filediff --git a/backend/migrations/000003_create_documents_table.down.sql b/backend/migrations/000003_create_documents_table.down.sqlnew file mode 100644index 00000000..c9100ee8--- /dev/null+++ b/backend/migrations/000003_create_documents_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `documents`;\ No newline at end of filediff --git a/backend/migrations/000003_create_documents_table.up.sql b/backend/migrations/000003_create_documents_table.up.sqlnew file mode 100644index 00000000..ae176ec9--- /dev/null+++ b/backend/migrations/000003_create_documents_table.up.sql@@ -0,0 +1,21 @@+CREATE TABLE IF NOT EXISTS `documents` (+ `id` varchar(50) NOT NULL,+ `name` varchar(50) NOT NULL,+ `description` varchar(255) DEFAULT NULL,+ `tags` varchar(200) DEFAULT NULL,+ `category` varchar(50) DEFAULT NULL,+ `parent_id` varchar(50) DEFAULT NULL,+ `accessibility` tinyint(1) NOT NULL,+ `content_markdown` longtext,+ `content_html` longtext,+ `author_id` varchar(50) NOT NULL,+ `created_timestamp` bigint NOT NULL,+ `updated_timestamp` bigint NOT NULL,+ PRIMARY KEY (`id`),+ KEY `author_id` (`author_id`),+ KEY `category` (`category`),+ KEY `documents_ibfk_3` (`parent_id`),+ CONSTRAINT `documents_ibfk_2` FOREIGN KEY (`category`) REFERENCES `categories` (`id`),+ CONSTRAINT `documents_ibfk_3` FOREIGN KEY (`parent_id`) REFERENCES `documents` (`id`) ON DELETE CASCADE,+ CONSTRAINT `documents_users_id_fk` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`)+)\ No newline at end of filediff --git a/backend/migrations/000004_create_ressources_table.down.sql b/backend/migrations/000004_create_ressources_table.down.sqlnew file mode 100644index 00000000..a1a8d808--- /dev/null+++ b/backend/migrations/000004_create_ressources_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `ressources`;\ No newline at end of filediff --git a/backend/migrations/000004_create_ressources_table.up.sql b/backend/migrations/000004_create_ressources_table.up.sqlnew file mode 100644index 00000000..3cbe9ea6--- /dev/null+++ b/backend/migrations/000004_create_ressources_table.up.sql@@ -0,0 +1,13 @@+CREATE TABLE IF NOT EXISTS `ressources` (+ `id` varchar(50) NOT NULL,+ `filename` varchar(50) NOT NULL,+ `file_size` int NOT NULL,+ `file_type` varchar(100) NOT NULL,+ `original_path` varchar(100) NOT NULL,+ `transformed_path` varchar(100) DEFAULT NULL,+ `author_id` varchar(50) NOT NULL,+ `created_timestamp` bigint NOT NULL,+ PRIMARY KEY (`id`),+ KEY `ressources_users_id_fk` (`author_id`),+ CONSTRAINT `ressources_users_id_fk` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`)+)\ No newline at end of filediff --git a/backend/migrations/000005_create_sessions_table.down.sql b/backend/migrations/000005_create_sessions_table.down.sqlnew file mode 100644index 00000000..b4e182fc--- /dev/null+++ b/backend/migrations/000005_create_sessions_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `sessions`;\ No newline at end of filediff --git a/backend/migrations/000005_create_sessions_table.up.sql b/backend/migrations/000005_create_sessions_table.up.sqlnew file mode 100644index 00000000..02e6e5da--- /dev/null+++ b/backend/migrations/000005_create_sessions_table.up.sql@@ -0,0 +1,12 @@+CREATE TABLE IF NOT EXISTS `sessions` (+ `id` varchar(50) NOT NULL,+ `user_id` varchar(50) NOT NULL,+ `refresh_token` varchar(255) DEFAULT NULL,+ `expire_token` bigint DEFAULT NULL,+ `last_refresh_timestamp` bigint DEFAULT NULL,+ `active` int DEFAULT NULL,+ `login_timestamp` bigint DEFAULT NULL,+ `logout_timestamp` bigint DEFAULT NULL,+ PRIMARY KEY (`id`),+ KEY `users_auth_users_id_fk` (`user_id`)+)\ No newline at end of filediff --git a/backend/migrations/000006_create_connections_logs_table.down.sql b/backend/migrations/000006_create_connections_logs_table.down.sqlnew file mode 100644index 00000000..bdf25822--- /dev/null+++ b/backend/migrations/000006_create_connections_logs_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `connections_logs`;\ No newline at end of filediff --git a/backend/migrations/000006_create_connections_logs_table.up.sql b/backend/migrations/000006_create_connections_logs_table.up.sqlnew file mode 100644index 00000000..9c5b1f2d--- /dev/null+++ b/backend/migrations/000006_create_connections_logs_table.up.sql@@ -0,0 +1,12 @@+CREATE TABLE IF NOT EXISTS `connections_logs` (+ `id` varchar(50) NOT NULL,+ `user_id` varchar(50) NOT NULL,+ `ip_adress` varchar(50) DEFAULT NULL,+ `user_agent` varchar(200) DEFAULT NULL,+ `location` varchar(100) DEFAULT NULL,+ `type` varchar(10) NOT NULL,+ `timestamp` bigint NOT NULL,+ PRIMARY KEY (`id`),+ KEY `connections_logs_users_id_fk` (`user_id`),+ CONSTRAINT `connections_logs_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)+)\ No newline at end of filediff --git a/backend/migrations/000007_create_city_ipv4_table.down.sql b/backend/migrations/000007_create_city_ipv4_table.down.sqlnew file mode 100644index 00000000..d8d11bbd--- /dev/null+++ b/backend/migrations/000007_create_city_ipv4_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `city_ipv4_complete`;\ No newline at end of filediff --git a/backend/migrations/000007_create_city_ipv4_table.up.sql b/backend/migrations/000007_create_city_ipv4_table.up.sqlnew file mode 100644index 00000000..c4cb1d9c--- /dev/null+++ b/backend/migrations/000007_create_city_ipv4_table.up.sql@@ -0,0 +1,16 @@+CREATE TABLE IF NOT EXISTS `city_ipv4_complete` (+ `network_start_integer` bigint DEFAULT NULL,+ `network_last_integer` bigint DEFAULT NULL,+ `geoname_id` int DEFAULT NULL,+ `registered_country_geoname_id` int DEFAULT NULL,+ `represented_country_geoname_id` text,+ `is_anonymous_proxy` int DEFAULT NULL,+ `is_satellite_provider` int DEFAULT NULL,+ `postal_code` text,+ `latitude` double DEFAULT NULL,+ `longitude` double DEFAULT NULL,+ `accuracy_radius` int DEFAULT NULL,+ `is_anycast` text,+ KEY `City_IPv4_complete_City_Locations_fr_geoname_id_fk` (`geoname_id`),+ CONSTRAINT `City_IPv4_complete_City_Locations_fr_geoname_id_fk` FOREIGN KEY (`geoname_id`) REFERENCES `city_locations_fr` (`geoname_id`)+)\ No newline at end of filediff --git a/backend/migrations/000008_create_city_ipv6_table.down.sql b/backend/migrations/000008_create_city_ipv6_table.down.sqlnew file mode 100644index 00000000..22e2dfc0--- /dev/null+++ b/backend/migrations/000008_create_city_ipv6_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `city_ipv6_complete`;\ No newline at end of filediff --git a/backend/migrations/000008_create_city_ipv6_table.up.sql b/backend/migrations/000008_create_city_ipv6_table.up.sqlnew file mode 100644index 00000000..60415733--- /dev/null+++ b/backend/migrations/000008_create_city_ipv6_table.up.sql@@ -0,0 +1,16 @@+CREATE TABLE IF NOT EXISTS `city_ipv6_complete` (+ `network_start_integer` double DEFAULT NULL,+ `network_last_integer` double DEFAULT NULL,+ `geoname_id` int DEFAULT NULL,+ `registered_country_geoname_id` int DEFAULT NULL,+ `represented_country_geoname_id` text,+ `is_anonymous_proxy` int DEFAULT NULL,+ `is_satellite_provider` int DEFAULT NULL,+ `postal_code` text,+ `latitude` double DEFAULT NULL,+ `longitude` text,+ `accuracy_radius` text,+ `is_anycast` text,+ KEY `City_IPv6_complete_City_Locations_fr_geoname_id_fk` (`geoname_id`),+ CONSTRAINT `City_IPv6_complete_City_Locations_fr_geoname_id_fk` FOREIGN KEY (`geoname_id`) REFERENCES `city_locations_fr` (`geoname_id`)+)\ No newline at end of filediff --git a/backend/migrations/000009_create_city_location_table.down.sql b/backend/migrations/000009_create_city_location_table.down.sqlnew file mode 100644index 00000000..59a2c44d--- /dev/null+++ b/backend/migrations/000009_create_city_location_table.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS `city_locations_fr`;\ No newline at end of filediff --git a/backend/migrations/000009_create_city_location_table.up.sql b/backend/migrations/000009_create_city_location_table.up.sqlnew file mode 100644index 00000000..f515ce92--- /dev/null+++ b/backend/migrations/000009_create_city_location_table.up.sql@@ -0,0 +1,17 @@+CREATE TABLE IF NOT EXISTS `city_locations_fr` (+ `geoname_id` int NOT NULL,+ `locale_code` text,+ `continent_code` text,+ `continent_name` text,+ `country_iso_code` text,+ `country_name` text,+ `subdivision_1_iso_code` text,+ `subdivision_1_name` text,+ `subdivision_2_iso_code` text,+ `subdivision_2_name` text,+ `city_name` text,+ `metro_code` text,+ `time_zone` text,+ `is_in_european_union` text,+ PRIMARY KEY (`geoname_id`)+)\ No newline at end of filediff --git a/backend/migrations_old/000001_init.down.sql b/backend/migrations_old/000001_init.down.sqlnew file mode 100644index 00000000..8c4cb644--- /dev/null+++ b/backend/migrations_old/000001_init.down.sql@@ -0,0 +1,9 @@+DROP TABLE IF EXISTS `city_ipv6_complete`;+DROP TABLE IF EXISTS `city_ipv4_complete`;+DROP TABLE IF EXISTS `city_locations_fr`;+DROP TABLE IF EXISTS `sessions`;+DROP TABLE IF EXISTS `ressources`;+DROP TABLE IF EXISTS `documents`;+DROP TABLE IF EXISTS `connections_logs`;+DROP TABLE IF EXISTS `categories`;+DROP TABLE IF EXISTS `users`;diff --git a/backend/migrations_old/000001_init.up.sql b/backend/migrations_old/000001_init.up.sqlnew file mode 100644index 00000000..3f2ff2d6--- /dev/null+++ b/backend/migrations_old/000001_init.up.sql@@ -0,0 +1,5 @@+++++