lemmy/migrations/2022-09-08-102358_site-and-community-languages/up.sql
Dessalines be1389420b
Adding SQL format checking via pg_format / pgFormatter (#3740)
* SQL format checking, 1.

* SQL format checking, 2.

* SQL format checking, 3.

* SQL format checking, 4.

* SQL format checking, 5.

* Running pg_format

* Getting rid of comment.

* Upping pg_format version.

* Using git ls-files for sql format check.

* Fixing sql lints.

* Addressing PR comments.
2023-08-02 12:44:51 -04:00

62 lines
1.5 KiB
SQL

CREATE TABLE site_language (
id serial PRIMARY KEY,
site_id int REFERENCES site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
language_id int REFERENCES
LANGUAGE ON
UPDATE CASCADE ON DELETE CASCADE NOT NULL,
UNIQUE (site_id, language_id)
);
CREATE TABLE community_language (
id serial PRIMARY KEY,
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
language_id int REFERENCES
LANGUAGE ON
UPDATE CASCADE ON DELETE CASCADE NOT NULL,
UNIQUE (community_id, language_id)
);
-- update existing users, sites and communities to have all languages enabled
DO $$
DECLARE
xid integer;
BEGIN
FOR xid IN
SELECT
id
FROM
local_user LOOP
INSERT INTO local_user_language (local_user_id, language_id) (
SELECT
xid,
language.id AS lid
FROM
LANGUAGE);
END LOOP;
FOR xid IN
SELECT
id
FROM
site LOOP
INSERT INTO site_language (site_id, language_id) (
SELECT
xid,
language.id AS lid
FROM
LANGUAGE);
END LOOP;
FOR xid IN
SELECT
id
FROM
community LOOP
INSERT INTO community_language (community_id, language_id) (
SELECT
xid,
language.id AS lid
FROM
LANGUAGE);
END LOOP;
END;
$$;