From dea0ef508abecba3f5892f7bae05bdf2148d537d Mon Sep 17 00:00:00 2001 From: Runar Ingebrigtsen Date: Fri, 23 Jan 2026 12:20:13 +0100 Subject: [PATCH] switch install state to db --- README.md | 2 +- app/controllers/application_controller.rb | 2 +- app/controllers/setup_controller.rb | 12 ++---------- app/models/setup_state.rb | 16 ++++++++++++++++ db/migrate/20260122131000_create_setup_states.rb | 10 ++++++++++ db/structure.sql | 2 ++ 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 app/models/setup_state.rb create mode 100644 db/migrate/20260122131000_create_setup_states.rb diff --git a/README.md b/README.md index 80f2bdc..43c6541 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ When translators disagree on a translation or want to suggest alternatives (regi ## Setup / First User -When the file `.installed` is missing, the `/setup` route is accessible for creating the initial admin account. The first user created will be the system's default contact email (accessible via `User.first.email`). +When setup has not been completed, the `/setup` route is accessible for creating the initial admin account. Completion is tracked in the database. The first user created will be the system's default contact email (accessible via `User.first.email`). For detailed setup instructions, see [SETUP_GUIDE.md](docs/SETUP_GUIDE.md). diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 956f744..71997a4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -57,6 +57,6 @@ class ApplicationController < ActionController::Base end def setup_completed? - File.exist?(Rails.root.join(".installed")) + SetupState.installed? end end diff --git a/app/controllers/setup_controller.rb b/app/controllers/setup_controller.rb index d250be0..e67e6d4 100644 --- a/app/controllers/setup_controller.rb +++ b/app/controllers/setup_controller.rb @@ -11,7 +11,7 @@ class SetupController < ApplicationController @user.invitation_accepted_at = Time.current if @user.save - create_installed_marker + SetupState.mark_installed! session[:user_id] = @user.id redirect_to admin_root_path, notice: "Setup complete! Welcome to Sanasto Wiki." else @@ -28,15 +28,7 @@ class SetupController < ApplicationController end def setup_completed? - File.exist?(installed_marker_path) - end - - def installed_marker_path - Rails.root.join(".installed") - end - - def create_installed_marker - FileUtils.touch(installed_marker_path) + SetupState.installed? end def user_params diff --git a/app/models/setup_state.rb b/app/models/setup_state.rb new file mode 100644 index 0000000..4ad9437 --- /dev/null +++ b/app/models/setup_state.rb @@ -0,0 +1,16 @@ +class SetupState < ApplicationRecord + def self.installed? + first&.installed? || false + end + + def self.mark_installed! + record = first_or_initialize + record.installed = true + record.installed_at ||= Time.current + record.save! + end + + def self.reset! + delete_all + end +end diff --git a/db/migrate/20260122131000_create_setup_states.rb b/db/migrate/20260122131000_create_setup_states.rb new file mode 100644 index 0000000..40e8209 --- /dev/null +++ b/db/migrate/20260122131000_create_setup_states.rb @@ -0,0 +1,10 @@ +class CreateSetupStates < ActiveRecord::Migration[8.1] + def change + create_table :setup_states do |t| + t.boolean :installed, null: false, default: false + t.datetime :installed_at + + t.timestamps + end + end +end diff --git a/db/structure.sql b/db/structure.sql index cfb7fb5..ab820a9 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -88,7 +88,9 @@ BEGIN INSERT INTO entries_fts(entries_fts, rowid, fi, en, sv, no, ru, de, notes) VALUES('delete', old.id, old.fi, old.en, old.sv, old.no, old.ru, old.de, old.notes); END; +CREATE TABLE IF NOT EXISTS "setup_states" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "installed" boolean DEFAULT FALSE NOT NULL, "installed_at" datetime(6), "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL); INSERT INTO "schema_migrations" (version) VALUES +('20260122131000'), ('20260122130000'), ('20260122124151'), ('20260122123837'),