This commit is contained in:
Dan Ponte 2023-07-02 20:39:39 +00:00
commit 5a20ce6347
6 changed files with 242 additions and 0 deletions

6
README.md Normal file
View file

@ -0,0 +1,6 @@
These are some helpful resources to use lemmy on FreeBSD, including rc scripts and syslog/newsyslog configurations.
Also, if after running lemmy the first time, it dies due to being unable to run migrations requiring system trigger,
run `sql/run_migrations.sql` against your DB and try to start it again..
Someday maybe I will make a port.

View file

@ -0,0 +1 @@
/var/log/lemmy/lemmy.log 600 10 1000 * JC

46
rc.d/lemmy Executable file
View file

@ -0,0 +1,46 @@
#!/bin/sh
# PROVIDE: lemmy
# REQUIRE: NETWORKING SYSLOG DAEMON
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# lemmy_enable (bool): Set to NO by default.
# Set it to YES to enable lemmy.
#
. /etc/rc.subr
: ${lemmy_enable="NO"}
: ${lemmy_user="lemmy"}
: ${lemmy_group="lemmy"}
: ${lemmy_config="/usr/local/etc/lemmy/lemmy.hjson"}
: ${lemmy_pictrspath="/var/lib/pictrs"}
: ${lemmy_pictrsaddr="127.0.0.1:8080"}
: ${lemmy_facility:="news"}
: ${lemmy_priority:="err"}
: ${lemmy_tag:="lemmy"}
name="lemmy"
rcvar=lemmy_enable
load_rc_config $name
lemmy_pidfile="/var/run/${name}.pid"
pidfile=${lemmy_pidfile}
procname=/usr/local/lemmy/lemmy_server
command=/usr/sbin/daemon
start_cmd=lemmy_start
lemmy_start() {
echo "Starting ${name}."
cd ${lemmy_pictrspath}/..
export LEMMY_CONFIG_LOCATION=${lemmy_config}
export PICTRS_PATH=${lemmy_pictrspath}
export PICTRS_ADDR=${lemmy_pictrsaddr}
/usr/sbin/daemon -S -s ${lemmy_priority} -l ${lemmy_facility} -T ${lemmy_tag} -p ${lemmy_pidfile} -f -u $lemmy_user $procname
}
run_rc_command "$1"

53
rc.d/lemmyui Executable file
View file

@ -0,0 +1,53 @@
#!/bin/sh
# PROVIDE: lemmyui
# REQUIRE: NETWORKING SYSLOG DAEMON lemmy
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# lemmyui_enable (bool): Set to NO by default.
# Set it to YES to enable lemmyui.
#
. /etc/rc.subr
: ${lemmyui_enable="NO"}
: ${lemmyui_user="lemmy"}
: ${lemmyui_group="lemmy"}
: ${lemmyui_dir="/srv/lemmy-ui"}
: ${lemmyui_inthost="localhost:8536"}
: ${lemmyui_exthost="lemmy"}
: ${lemmyui_host="0.0.0.0:1234"}
: ${lemmyui_https="true"}
: ${lemmyui_facility:="news"}
: ${lemmyui_priority:="err"}
: ${lemmyui_tag:="lemmyui"}
name="lemmyui"
rcvar=lemmyui_enable
load_rc_config $name
lemmyui_pidfile="/var/run/${name}.pid"
pidfile=${lemmyui_pidfile}
procname=/usr/local/bin/node
command=/usr/sbin/daemon
start_cmd=lemmyui_start
cmd="node "
lemmyui_start() {
echo "Starting ${name}."
cd "${lemmyui_dir}"
export LEMMY_UI_HOST="${lemmyui_host}"
export LEMMY_UI_LEMMY_INTERNAL_HOST="${lemmyui_inthost}"
export LEMMY_UI_LEMMY_EXTERNAL_HOST="${lemmyui_exthost}"
export LEMMY_UI_HTTPS="${lemmyui_https}"
/usr/sbin/daemon -S -s ${lemmyui_priority} -l ${lemmyui_facility} -T ${lemmyui_tag} -p ${lemmyui_pidfile} -f -u $lemmyui_user ${procname} dist/js/server.js
}
run_rc_command "$1"

135
sql/run_migrations.sql Normal file
View file

@ -0,0 +1,135 @@
-- Use this after starting lemmy for the first time when postgres logs show you
-- an error about being unable to delete triggers. This file is a reproduction
-- of [this][0] plus the changes from [this PR][1] with the additions of just
-- this comment and the final line at the end to update the migrations table
-- added.
--
-- This file needs to be run as your `lemmy` database user against the `lemmy`
-- database. Make sure to also pass `-v ON_ERROR_STOP=1` to `psql` (or
-- equivalent for whatever tool you're using) to prevent weirdness.
--
-- [0]: https://github.com/LemmyNet/lemmy/blob/c32585b03429f0f76d1e4ff738786321a0a9df98/migrations/2022-07-07-182650_comment_ltrees/up.sql
-- [1]: https://github.com/LemmyNet/lemmy/pull/3002
-- Remove the comment.read column, and create a new comment_reply table,
-- similar to the person_mention table.
--
-- This is necessary because self-joins using ltrees would be too tough with SQL views
--
-- Every comment should have a row here, because all comments have a recipient,
-- either the post creator, or the parent commenter.
create table comment_reply(
id serial primary key,
recipient_id int references person on update cascade on delete cascade not null,
comment_id int references comment on update cascade on delete cascade not null,
read boolean default false not null,
published timestamp not null default now(),
unique(recipient_id, comment_id)
);
-- Ones where parent_id is null, use the post creator recipient
insert into comment_reply (recipient_id, comment_id, read)
select p.creator_id, c.id, c.read from comment c
inner join post p on c.post_id = p.id
where c.parent_id is null;
-- Ones where there is a parent_id, self join to comment to get the parent comment creator
insert into comment_reply (recipient_id, comment_id, read)
select c2.creator_id, c.id, c.read from comment c
inner join comment c2 on c.parent_id = c2.id;
-- Drop comment_alias view
drop view comment_alias_1;
alter table comment drop column read;
create extension ltree;
alter table comment add column path ltree not null default '0';
alter table comment_aggregates add column child_count integer not null default 0;
-- The ltree path column should be the comment_id parent paths, separated by dots.
-- Stackoverflow: building an ltree from a parent_id hierarchical tree:
-- https://stackoverflow.com/a/1144848/1655478
create temporary table comment_temp as
WITH RECURSIVE q AS (
SELECT h, 1 AS level, ARRAY[id] AS breadcrumb
FROM comment h
WHERE parent_id is null
UNION ALL
SELECT hi, q.level + 1 AS level, breadcrumb || id
FROM q
JOIN comment hi
ON hi.parent_id = (q.h).id
)
SELECT (q.h).id,
(q.h).parent_id,
level,
breadcrumb::VARCHAR AS path,
text2ltree('0.' || array_to_string(breadcrumb, '.')) as ltree_path
FROM q
ORDER BY
breadcrumb;
-- Remove indexes and foreign key constraints, and disable triggers for faster updates
alter table comment disable trigger user;
alter table comment drop constraint if exists comment_creator_id_fkey;
alter table comment drop constraint if exists comment_parent_id_fkey;
alter table comment drop constraint if exists comment_post_id_fkey;
alter table comment drop constraint if exists idx_comment_ap_id;
drop index if exists idx_comment_creator;
drop index if exists idx_comment_parent;
drop index if exists idx_comment_post;
drop index if exists idx_comment_published;
-- Add the ltree column
update comment c
set path = ct.ltree_path
from comment_temp ct
where c.id = ct.id;
-- Update the child counts
update comment_aggregates ca set child_count = c2.child_count
from (
select c.id, c.path, count(c2.id) as child_count from comment c
left join comment c2 on c2.path <@ c.path and c2.path != c.path
group by c.id
) as c2
where ca.comment_id = c2.id;
-- Delete comments at a depth of > 150, otherwise the index creation below will fail
delete from comment where nlevel(path) > 150;
-- Delete from comment where there is a missing post
delete from comment c where not exists (
select from post p where p.id = c.post_id
);
-- Delete from comment where there is a missing creator_id
delete from comment c where not exists (
select from person p where p.id = c.creator_id
);
-- Re-enable old constraints and indexes
alter table comment add constraint "comment_creator_id_fkey" FOREIGN KEY (creator_id) REFERENCES person(id) ON UPDATE CASCADE ON DELETE CASCADE;
alter table comment add constraint "comment_post_id_fkey" FOREIGN KEY (post_id) REFERENCES post(id) ON UPDATE CASCADE ON DELETE CASCADE;
alter table comment add constraint "idx_comment_ap_id" unique (ap_id);
create index idx_comment_creator on comment (creator_id);
create index idx_comment_post on comment (post_id);
create index idx_comment_published on comment (published desc);
-- Create the index
create index idx_path_gist on comment using gist (path);
-- Drop the parent_id column
alter table comment drop column parent_id cascade;
alter table comment enable trigger user;
-- This was not part of the original file but is necessary to unblock the rest
-- of the migrations
insert into __diesel_schema_migrations (version) values ('20220707182650');

1
syslog.d/lemmy.conf Normal file
View file

@ -0,0 +1 @@
news.err /var/log/lemmy/lemmy.log