Add activerecord, migrate to SQLite3
This commit is contained in:
parent
76ce0bb9f0
commit
ec2be8825e
14 changed files with 127 additions and 45 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,6 +4,6 @@
|
||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
.yardoc/
|
.yardoc/
|
||||||
.dev/
|
.dev/
|
||||||
doc/
|
.doc/
|
||||||
pkg/
|
pkg/
|
||||||
.gems/
|
.gems/
|
||||||
|
|
0
twenty-backend/Rakefile.rb
Normal file
0
twenty-backend/Rakefile.rb
Normal file
|
@ -1,5 +1,8 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
module Twenty
|
module Twenty
|
||||||
require "webrick"
|
require "webrick"
|
||||||
|
require "active_record"
|
||||||
require_relative "twenty-backend/servlet"
|
require_relative "twenty-backend/servlet"
|
||||||
|
require_relative "twenty-backend/migration"
|
||||||
|
require_relative "twenty-backend/model"
|
||||||
end
|
end
|
||||||
|
|
11
twenty-backend/lib/twenty-backend/migration.rb
Normal file
11
twenty-backend/lib/twenty-backend/migration.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
module Twenty::Migration
|
||||||
|
require_relative "migration/create_connections"
|
||||||
|
require_relative "migration/create_issues"
|
||||||
|
|
||||||
|
def self.run!
|
||||||
|
CreateConnections.migrate(:up)
|
||||||
|
CreateIssues.migrate(:up)
|
||||||
|
rescue
|
||||||
|
warn "#{$!.class}: #{$!.message}"
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,15 @@
|
||||||
|
module Twenty::Migration
|
||||||
|
class CreateConnections < ActiveRecord::Migration[7.1]
|
||||||
|
def up
|
||||||
|
create_table(:connections) do |t|
|
||||||
|
t.string :name, null: false
|
||||||
|
t.string :path, null: false
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
drop_table(:connections)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
17
twenty-backend/lib/twenty-backend/migration/create_issues.rb
Normal file
17
twenty-backend/lib/twenty-backend/migration/create_issues.rb
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
module Twenty::Migration
|
||||||
|
class CreateIssues < ActiveRecord::Migration[7.1]
|
||||||
|
def up
|
||||||
|
create_table(:issues) do |t|
|
||||||
|
t.string :title, null: false
|
||||||
|
t.text :content, null: false
|
||||||
|
t.string :state, null: false
|
||||||
|
t.belongs_to :connection, null: false
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
drop_table(:issues)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
46
twenty-backend/lib/twenty-backend/model.rb
Normal file
46
twenty-backend/lib/twenty-backend/model.rb
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
class Twenty::Model < ActiveRecord::Base
|
||||||
|
require "fileutils"
|
||||||
|
extend FileUtils
|
||||||
|
|
||||||
|
##
|
||||||
|
# @return [String]
|
||||||
|
# Returns the path to a SQLite database.
|
||||||
|
def self.database
|
||||||
|
File.join(home, "twenty.sqlite")
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @return [String]
|
||||||
|
# Returns a path to the directory where twenty writes data.
|
||||||
|
def self.home
|
||||||
|
File.join(Dir.home, ".local", "share", "twenty")
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Establishes a database connection.
|
||||||
|
# @return [void]
|
||||||
|
# @api private
|
||||||
|
def self.connect
|
||||||
|
ActiveRecord::Base.establish_connection(
|
||||||
|
adapter: 'sqlite3',
|
||||||
|
database:,
|
||||||
|
pool: 3
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates the {home home} directory.
|
||||||
|
# @return [void]
|
||||||
|
# @api private
|
||||||
|
def self.prepare
|
||||||
|
return if File.exist?(database)
|
||||||
|
mkdir_p(home)
|
||||||
|
touch(database)
|
||||||
|
end
|
||||||
|
|
||||||
|
prepare
|
||||||
|
connect
|
||||||
|
require_relative "model/connection"
|
||||||
|
require_relative "model/issue"
|
||||||
|
Twenty::Migration.run!
|
||||||
|
end
|
10
twenty-backend/lib/twenty-backend/model/connection.rb
Normal file
10
twenty-backend/lib/twenty-backend/model/connection.rb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
class Twenty::Connection < Twenty::Model
|
||||||
|
##
|
||||||
|
# Validations
|
||||||
|
validates :name, presence: true
|
||||||
|
validates :path, presence: true
|
||||||
|
|
||||||
|
##
|
||||||
|
# Associations
|
||||||
|
has_many :issues, class_name: 'Twenty::Issue'
|
||||||
|
end
|
12
twenty-backend/lib/twenty-backend/model/issue.rb
Normal file
12
twenty-backend/lib/twenty-backend/model/issue.rb
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
class Twenty::Issue < Twenty::Model
|
||||||
|
##
|
||||||
|
# Validations
|
||||||
|
validates :title, presence: true
|
||||||
|
validates :content, presence: true
|
||||||
|
validates :state, inclusion: {in: %w[open closed]}
|
||||||
|
validates :connection, presence: true
|
||||||
|
|
||||||
|
##
|
||||||
|
# Associations
|
||||||
|
belongs_to :connection, class_name: 'Twenty::Connection'
|
||||||
|
end
|
|
@ -1,6 +1,6 @@
|
||||||
class Twenty::Servlet < WEBrick::HTTPServlet::AbstractServlet
|
class Twenty::Servlet < WEBrick::HTTPServlet::AbstractServlet
|
||||||
include Twenty::DB
|
|
||||||
require_relative "servlet/connections"
|
require_relative "servlet/connections"
|
||||||
|
require_relative "servlet/issues"
|
||||||
|
|
||||||
private
|
private
|
||||||
def write(res, ary)
|
def write(res, ary)
|
||||||
|
|
4
twenty-backend/lib/twenty-backend/servlet/issues.rb
Normal file
4
twenty-backend/lib/twenty-backend/servlet/issues.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
class Twenty::Servlet::Issues < Twenty::Servlet
|
||||||
|
def do_POST(req, res)
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,6 +11,8 @@ Gem::Specification.new do |gem|
|
||||||
gem.require_paths = ["lib"]
|
gem.require_paths = ["lib"]
|
||||||
gem.summary = "twenty: backend"
|
gem.summary = "twenty: backend"
|
||||||
gem.description = gem.summary
|
gem.description = gem.summary
|
||||||
|
gem.add_runtime_dependency "activerecord", "~> 7.1"
|
||||||
|
gem.add_runtime_dependency "sqlite3", "~> 1.6"
|
||||||
gem.add_runtime_dependency "webrick", "~> 1.8"
|
gem.add_runtime_dependency "webrick", "~> 1.8"
|
||||||
gem.add_development_dependency "test-unit", "~> 3.5.7"
|
gem.add_development_dependency "test-unit", "~> 3.5.7"
|
||||||
gem.add_development_dependency "standard", "~> 1.13"
|
gem.add_development_dependency "standard", "~> 1.13"
|
||||||
|
|
|
@ -1,34 +1,5 @@
|
||||||
module Twenty
|
module Twenty
|
||||||
module DB
|
|
||||||
require "json"
|
require "json"
|
||||||
DEFAULTS = { connections: [] }
|
|
||||||
|
|
||||||
##
|
|
||||||
# FIXME:
|
|
||||||
# The following methods are required by
|
|
||||||
# both twenty-cli, and twenty-backend. Eventually
|
|
||||||
# they should be moved into their own package.
|
|
||||||
|
|
||||||
def home
|
|
||||||
File.join Dir.home, ".local", "share", "twenty"
|
|
||||||
end
|
|
||||||
|
|
||||||
def database
|
|
||||||
Ryo.from DEFAULTS.dup.merge(JSON.parse(File.binread(database_path)))
|
|
||||||
rescue
|
|
||||||
Ryo.from(DEFAULTS.dup)
|
|
||||||
end
|
|
||||||
|
|
||||||
def database_path
|
|
||||||
File.join(home, "database.json")
|
|
||||||
end
|
|
||||||
|
|
||||||
def save!(db)
|
|
||||||
File.binwrite database_path,
|
|
||||||
JSON.dump(Ryo.table_of(db, recursive: true))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require "twenty-backend"
|
require "twenty-backend"
|
||||||
require "twenty-frontend"
|
require "twenty-frontend"
|
||||||
require_relative "twenty-cli/command"
|
require_relative "twenty-cli/command"
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
class Twenty::Command::Connect < Twenty::Command
|
class Twenty::Command::Connect < Twenty::Command
|
||||||
require "fileutils"
|
|
||||||
include Twenty::DB
|
|
||||||
include FileUtils
|
|
||||||
|
|
||||||
set_banner usage: "twenty connect [OPTIONS]",
|
set_banner usage: "twenty connect [OPTIONS]",
|
||||||
description: "Connect a project to twenty"
|
description: "Connect a project to twenty"
|
||||||
|
|
||||||
|
@ -14,14 +10,9 @@ class Twenty::Command::Connect < Twenty::Command
|
||||||
private
|
private
|
||||||
|
|
||||||
def run_command(options)
|
def run_command(options)
|
||||||
mkdir_p File.dirname(database_path)
|
Twenty::Connection.new(
|
||||||
path = Dir.getwd
|
name: File.basename(Dir.getwd),
|
||||||
name = File.basename(path)
|
path: Dir.getwd,
|
||||||
db.connections.push({name:, path:})
|
).save!
|
||||||
save!(db)
|
|
||||||
end
|
|
||||||
|
|
||||||
def db
|
|
||||||
@db ||= database
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue