diff --git a/cli/lib/twenty/cli/command.rb b/cli/lib/twenty/cli/command.rb index 38477f1..cf67eba 100644 --- a/cli/lib/twenty/cli/command.rb +++ b/cli/lib/twenty/cli/command.rb @@ -4,14 +4,12 @@ require "cmd" class Twenty::Command < Cmd ## - # mixins - require_relative "command/mixin/common_option_mixin" - require_relative "command/mixin/migration_mixin" - require_relative "command/mixin/sqlite_mixin" - require_relative "command/mixin/rescue_mixin" + # Hooks + require_relative "command/option" + require_relative "command/hook" ## - # commands + # Commands require_relative "command/up" require_relative "command/down" require_relative "command/connect" diff --git a/cli/lib/twenty/cli/command/connect.rb b/cli/lib/twenty/cli/command/connect.rb index c1fd3db..90d882c 100644 --- a/cli/lib/twenty/cli/command/connect.rb +++ b/cli/lib/twenty/cli/command/connect.rb @@ -5,9 +5,13 @@ class Twenty::Command::Connect < Twenty::Command description: "Connect a project to twenty" set_option "-p PATH", "--path PATH", "The path to a project", default: nil - prepend Twenty::Command::MigrationMixin - prepend Twenty::Command::SQLiteMixin - prepend Twenty::Command::RescueMixin + ## + # Hooks + # Run order: + # Rescue -> SQLiteConn -> RequireMigration -> command + prepend Hook::RequireMigration + prepend Hook::SQLiteConn + prepend Hook::Rescue def run options = parse_options(argv) diff --git a/cli/lib/twenty/cli/command/console.rb b/cli/lib/twenty/cli/command/console.rb index 35bf1ee..d1cdb10 100644 --- a/cli/lib/twenty/cli/command/console.rb +++ b/cli/lib/twenty/cli/command/console.rb @@ -3,10 +3,18 @@ class Twenty::Command::Console < Twenty::Command set_banner usage: "twenty console [OPTIONS]", description: "Start the twenty developer console" - include CommonOptionMixin - prepend Twenty::Command::MigrationMixin - prepend Twenty::Command::SQLiteMixin - prepend Twenty::Command::RescueMixin + + ## + # Option(s) + include Option::Database + + ## + # Hooks + # Run order: + # Rescue -> SQLiteConn -> RequireMigration -> command + prepend Hook::RequireMigration + prepend Hook::SQLiteConn + prepend Hook::Rescue def run options = parse_options(argv) diff --git a/cli/lib/twenty/cli/command/disconnect.rb b/cli/lib/twenty/cli/command/disconnect.rb index fe4ab13..b0a0bef 100644 --- a/cli/lib/twenty/cli/command/disconnect.rb +++ b/cli/lib/twenty/cli/command/disconnect.rb @@ -4,9 +4,12 @@ class Twenty::Command::Disconnect < Twenty::Command set_banner usage: "twenty disconnect [OPTIONS]", description: "Disconnect a project from twenty" set_option "-p PATH", "--path PATH", "The path to a project", default: nil - prepend Twenty::Command::MigrationMixin - prepend Twenty::Command::SQLiteMixin - prepend Twenty::Command::RescueMixin + + ## + # Hooks + prepend Hook::RequireMigration + prepend Hook::SQLiteConn + prepend Hook::Rescue def run options = parse_options(argv) diff --git a/cli/lib/twenty/cli/command/down.rb b/cli/lib/twenty/cli/command/down.rb index 36bcc5e..d2b4681 100644 --- a/cli/lib/twenty/cli/command/down.rb +++ b/cli/lib/twenty/cli/command/down.rb @@ -4,8 +4,12 @@ class Twenty::Command::Down < Twenty::Command set_banner usage: "twenty down [OPTIONS]", description: "Stop the twenty web server" - prepend Twenty::Command::SQLiteMixin - prepend Twenty::Command::RescueMixin + ## + # Hooks + # Run order: + # Rescue -> SQLiteConn -> command + prepend Hook::SQLiteConn + prepend Hook::Rescue def run options = parse_options(argv) diff --git a/cli/lib/twenty/cli/command/hook.rb b/cli/lib/twenty/cli/command/hook.rb new file mode 100644 index 0000000..b439dc9 --- /dev/null +++ b/cli/lib/twenty/cli/command/hook.rb @@ -0,0 +1,5 @@ +module Twenty::Command::Hook + require_relative "hook/require_migration" + require_relative "hook/sqlite_conn" + require_relative "hook/rescue" +end diff --git a/cli/lib/twenty/cli/command/hook/require_migration.rb b/cli/lib/twenty/cli/command/hook/require_migration.rb new file mode 100644 index 0000000..42c3e95 --- /dev/null +++ b/cli/lib/twenty/cli/command/hook/require_migration.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Twenty::Command::Hook + module RequireMigration + def run_command(...) + if pending_migrations? + warn "There are pending migrations.\n" \ + "Run \"twenty migrate\" first.\n" + exit(1) + else + super(...) + end + end + + private + + def pending_migrations? + Twenty::Migration.pending_migrations? + end + end +end diff --git a/cli/lib/twenty/cli/command/mixin/rescue_mixin.rb b/cli/lib/twenty/cli/command/hook/rescue.rb similarity index 93% rename from cli/lib/twenty/cli/command/mixin/rescue_mixin.rb rename to cli/lib/twenty/cli/command/hook/rescue.rb index b8867b5..1a5a95c 100644 --- a/cli/lib/twenty/cli/command/mixin/rescue_mixin.rb +++ b/cli/lib/twenty/cli/command/hook/rescue.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class Twenty::Command - module RescueMixin +module Twenty::Command::Hook + module Rescue FRAME_MAX = 15 def run(...) diff --git a/cli/lib/twenty/cli/command/hook/sqlite_conn.rb b/cli/lib/twenty/cli/command/hook/sqlite_conn.rb new file mode 100644 index 0000000..99f1dbb --- /dev/null +++ b/cli/lib/twenty/cli/command/hook/sqlite_conn.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Twenty::Command::Hook + module SQLiteConn + def run_command(options) + path = options.database || Twenty.default_database + Twenty.establish_connection(path:) + super(options) + end + end +end diff --git a/cli/lib/twenty/cli/command/migrate.rb b/cli/lib/twenty/cli/command/migrate.rb index 4804843..1b2defd 100644 --- a/cli/lib/twenty/cli/command/migrate.rb +++ b/cli/lib/twenty/cli/command/migrate.rb @@ -5,9 +5,16 @@ class Twenty::Command::Migrate < Twenty::Command description: "Migrate the database" set_option "-t TARGET", "--target TARGET", "The target version", default: nil - include CommonOptionMixin - prepend Twenty::Command::SQLiteMixin - prepend Twenty::Command::RescueMixin + ## + # Options + include Option::Database + + ## + # Hooks + # Run order: + # Rescue -> SQLiteConn -> command. + prepend Hook::SQLiteConn + prepend Hook::Rescue def run options = parse_options(argv) diff --git a/cli/lib/twenty/cli/command/mixin/migration_mixin.rb b/cli/lib/twenty/cli/command/mixin/migration_mixin.rb deleted file mode 100644 index ab8570d..0000000 --- a/cli/lib/twenty/cli/command/mixin/migration_mixin.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Twenty::Command::MigrationMixin - def run_command(...) - if pending_migrations? - warn "There are pending migrations.\n" \ - "Run \"twenty migrate\" first.\n" - exit(1) - else - super(...) - end - end - - private - - def pending_migrations? - Twenty::Migration.pending_migrations? - end -end diff --git a/cli/lib/twenty/cli/command/mixin/sqlite_mixin.rb b/cli/lib/twenty/cli/command/mixin/sqlite_mixin.rb deleted file mode 100644 index 18c07cb..0000000 --- a/cli/lib/twenty/cli/command/mixin/sqlite_mixin.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -module Twenty::Command::SQLiteMixin - def run_command(options) - path = options.database || Twenty.default_database - Twenty.establish_connection(path:) - require "twenty/server/migration" - require "twenty/server/model" - super(options) - end -end diff --git a/cli/lib/twenty/cli/command/option.rb b/cli/lib/twenty/cli/command/option.rb new file mode 100644 index 0000000..8cab3cd --- /dev/null +++ b/cli/lib/twenty/cli/command/option.rb @@ -0,0 +1,3 @@ +module Twenty::Command::Option + require_relative "option/database" +end diff --git a/cli/lib/twenty/cli/command/option/database.rb b/cli/lib/twenty/cli/command/option/database.rb new file mode 100644 index 0000000..fca3532 --- /dev/null +++ b/cli/lib/twenty/cli/command/option/database.rb @@ -0,0 +1,13 @@ +module Twenty::Command::Option + module Database + def self.included(mod) + mod.module_eval do + set_option "-d PATH", + "--database PATH", + "The path to an alternate SQLite database", + as: String, + default: nil + end + end + end +end diff --git a/cli/lib/twenty/cli/command/up.rb b/cli/lib/twenty/cli/command/up.rb index f76e836..87214a2 100644 --- a/cli/lib/twenty/cli/command/up.rb +++ b/cli/lib/twenty/cli/command/up.rb @@ -16,10 +16,17 @@ class Twenty::Command::Up < Twenty::Command "--unix PATH", "Listen on a UNIX socket" - include CommonOptionMixin - prepend Twenty::Command::MigrationMixin - prepend Twenty::Command::SQLiteMixin - prepend Twenty::Command::RescueMixin + ## + # Option(s) + include Option::Database + + ## + # Hooks + # Run order: + # Rescue -> SQLiteConn -> RequireMigration -> command + prepend Hook::RequireMigration + prepend Hook::SQLiteConn + prepend Hook::Rescue def run options = parse_options(argv) diff --git a/server/lib/twenty/server.rb b/server/lib/twenty/server.rb index 313f7a7..e28feb3 100644 --- a/server/lib/twenty/server.rb +++ b/server/lib/twenty/server.rb @@ -1,13 +1,11 @@ # frozen_string_literal: true module Twenty - require "fileutils" - require "sequel" - extend Module.new { + require "tmpdir" + require "fileutils" extend self extend FileUtils - require "tmpdir" ## # @return [String] @@ -63,6 +61,8 @@ module Twenty @connection end + require "sequel" + require_relative "server/migration" require_relative "server/graphql" require_relative "server/rack" end