Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dullbananas committed May 20, 2024
1 parent 7977ee1 commit ced9bb5
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 20 deletions.
49 changes: 40 additions & 9 deletions crates/db_schema/src/schema_setup/diff_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn get_dump(conn: &mut PgConnection) -> String {
.get_result::<String>(conn)
.expect("pg_export_snapshot failed");
let snapshot_arg = format!("--snapshot={snapshot}");*/

let output = Command::new("pg_dump")
.args(["--schema-only"])
.env("DATABASE_URL", SETTINGS.get_database_url())
Expand All @@ -40,6 +39,9 @@ const PATTERN_LEN: usize = 19;
// TODO add unit test for output
pub fn check_dump_diff(conn: &mut PgConnection, mut before: String, name: &str) {
let mut after = get_dump(conn);
if after == before {
return;
}
// Ignore timestamp differences by removing timestamps
for dump in [&mut before, &mut after] {
for index in 0.. {
Expand Down Expand Up @@ -74,9 +76,6 @@ pub fn check_dump_diff(conn: &mut PgConnection, mut before: String, name: &str)
}
}

if after == before {
return;
}
let [before_chunks, after_chunks] =
[&before, &after].map(|dump| chunks(dump).collect::<BTreeSet<_>>());

Expand All @@ -102,8 +101,8 @@ pub fn check_dump_diff(conn: &mut PgConnection, mut before: String, name: &str)
}
let after_has_more =
only_in_before.len() < only_in_after.len();
// outer iterator in the loop below should be the one with empty strings, otherwise the empty strings
// would appear to have the most similarity
// outer iterator in the loop below should not be the one with empty strings, otherwise the empty strings
// would be equally similar to any other chunk
let (chunks_gt, chunks_lt) = if after_has_more
{
only_in_before.resize_with(only_in_after.len(),Default::default);
Expand All @@ -118,15 +117,15 @@ pub fn check_dump_diff(conn: &mut PgConnection, mut before: String, name: &str)
for (before_chunk, before_chunk_filtered) in chunks_lt {
let default = Default::default();
//panic!("{:?}",(before_chunk.clone(),chunks_lt.clone()));
let (most_similar_chunk_index, (most_similar_chunk, _)) = chunks_gt
let (most_similar_chunk_index, (most_similar_chunk, most_similar_chunk_filtered)) = chunks_gt
.iter()
.enumerate()
.max_by_key(|(_, (after_chunk, after_chunk_filtered))| {
diff::chars(after_chunk_filtered, &before_chunk_filtered)
.into_iter()
.filter(|i| matches!(i, diff::Result::Both(c, _)
// This increases accuracy for some trigger function diffs
if c.is_lowercase()))
// `is_lowercase` increases accuracy for some trigger function diffs
if c.is_lowercase() || c.is_numeric()))
.count()
})
.unwrap_or((0,&default));
Expand All @@ -143,6 +142,7 @@ pub fn check_dump_diff(conn: &mut PgConnection, mut before: String, name: &str)
}
.expect("failed to build string");
}
write!(&mut output, "\n{most_similar_chunk_filtered}");
if !chunks_gt.is_empty() {
chunks_gt.swap_remove(most_similar_chunk_index);}
}
Expand Down Expand Up @@ -178,6 +178,37 @@ fn chunks<'a>(dump: &'a str) -> impl Iterator<Item = Cow<'a, str>> {
(placement, line)
});
Cow::Owned(lines.join("\n"))
} else if result.starts_with("CREATE VIEW") || result.starts_with("CREATE OR REPLACE VIEW") {
// Allow column order to change
let is_simple_select_statement = result
.lines()
.enumerate()
.all(|(i, mut line)| {
line = line.trim_start();
match (i, line.chars().next()) {
(0, Some('C')) => true, // create
(1, Some('S')) => true, // select
(_, Some('F')) if line.ends_with(';') => true, // from
(_, Some(c)) if c.is_lowercase() => true, // column name
_ => false
}
});
if is_simple_select_statement {
let mut lines = result
.lines()
.map(|line| line.strip_suffix(',').unwrap_or(line))
.collect::<Vec<_>>();
lines.sort_unstable_by_key(|line| -> (u8, &str) {
let placement = match line.trim_start().chars().next() {
Some('C') => 0,
Some('S') => 1,
Some('F') => 3,
_ => 2,
};
(placement, line)
});
Cow::Owned(lines.join("\n"))
}else{Cow::Borrowed(result)}
} else {
Cow::Borrowed(result)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ALTER TABLE activity
ADD COLUMN user_id INTEGER;
ADD COLUMN user_id INTEGER NOT NULL REFERENCES user_(id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE activity
DROP COLUMN sensitive;
Expand Down
4 changes: 4 additions & 0 deletions migrations/2021-02-25-112959_remove-categories/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ INSERT INTO category (name)
ALTER TABLE community
ADD category_id int REFERENCES category ON UPDATE CASCADE ON DELETE CASCADE NOT NULL DEFAULT 1;

-- Default is only for existing rows
ALTER TABLE community
ALTER COLUMN category_id DROP DEFAULT;

CREATE INDEX idx_community_category ON community (category_id);

61 changes: 57 additions & 4 deletions migrations/2021-03-09-171136_split_user_table_2/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ ALTER SEQUENCE person_id_seq
-- Add the columns back in
ALTER TABLE user_
ADD COLUMN password_encrypted text NOT NULL DEFAULT 'changeme',
ADD COLUMN email text,
ADD COLUMN email text UNIQUE,
ADD COLUMN admin boolean DEFAULT FALSE NOT NULL,
ADD COLUMN show_nsfw boolean DEFAULT FALSE NOT NULL,
ADD COLUMN theme character varying(20) DEFAULT 'darkly'::character varying NOT NULL,
Expand All @@ -238,7 +238,10 @@ ALTER TABLE user_
ADD COLUMN lang character varying(20) DEFAULT 'browser'::character varying NOT NULL,
ADD COLUMN show_avatars boolean DEFAULT TRUE NOT NULL,
ADD COLUMN send_notifications_to_email boolean DEFAULT FALSE NOT NULL,
ADD COLUMN matrix_user_id text;
ADD COLUMN matrix_user_id text UNIQUE;

-- Default is only for existing rows
alter table user_ alter column password_encrypted drop default;

-- Update the user_ table with the local_user data
UPDATE
Expand All @@ -264,13 +267,63 @@ CREATE UNIQUE INDEX idx_user_email_lower ON user_ (lower(email));

CREATE VIEW user_alias_1 AS
SELECT
*
id,
actor_id,
admin,
avatar,
banned,
banner,
bio,
default_listing_type,
default_sort_type,
deleted,
email,
lang,
last_refreshed_at,
local,
matrix_user_id,
name,
password_encrypted,
preferred_username,
private_key,
public_key,
published,
send_notifications_to_email,
show_avatars,
show_nsfw,
theme,
updated
FROM
user_;

CREATE VIEW user_alias_2 AS
SELECT
*
id,
actor_id,
admin,
avatar,
banned,
banner,
bio,
default_listing_type,
default_sort_type,
deleted,
email,
lang,
last_refreshed_at,
local,
matrix_user_id,
name,
password_encrypted,
preferred_username,
private_key,
public_key,
published,
send_notifications_to_email,
show_avatars,
show_nsfw,
theme,
updated
FROM
user_;

Expand Down
2 changes: 2 additions & 0 deletions migrations/2021-09-20-112945_jwt-secret/down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
DROP TABLE secret;

DROP EXTENSION pgcrypto;

2 changes: 1 addition & 1 deletion migrations/2022-07-07-182650_comment_ltrees/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ CREATE INDEX idx_path_gist ON comment USING gist (path);

-- Drop the parent_id column
ALTER TABLE comment
DROP COLUMN parent_id CASCADE,add column if not exists poop int;alter table post add column if not exists poop int;
DROP COLUMN parent_id CASCADE;

ALTER TABLE comment ENABLE TRIGGER USER;

2 changes: 1 addition & 1 deletion migrations/2022-10-06-183632_move_blocklist_to_db/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ALTER TABLE site
ADD COLUMN community_creation_admin_only boolean DEFAULT FALSE NOT NULL,
ADD COLUMN require_email_verification boolean DEFAULT FALSE NOT NULL,
ADD COLUMN require_application boolean DEFAULT TRUE NOT NULL,
ADD COLUMN application_question text DEFAULT 'to verify that you are human, please explain why you want to create an account on this site'::text,
ADD COLUMN application_question text DEFAULT 'To verify that you are human, please explain why you want to create an account on this site'::text,
ADD COLUMN private_instance boolean DEFAULT FALSE NOT NULL,
ADD COLUMN default_theme text DEFAULT 'browser'::text NOT NULL,
ADD COLUMN default_post_listing_type text DEFAULT 'Local'::text NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ALTER TABLE local_site
ADD COLUMN federation_debug int DEFAULT 0;
ADD COLUMN federation_debug boolean DEFAULT false not null;

Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ ALTER TABLE local_site
ALTER COLUMN default_post_listing_type TYPE text;

ALTER TABLE local_site
ALTER COLUMN default_post_listing_type SET DEFAULT 1;
ALTER COLUMN default_post_listing_type SET DEFAULT 'Local';

-- Drop the types
DROP TYPE listing_type_enum;
Expand Down
2 changes: 2 additions & 0 deletions migrations/2023-07-24-232635_trigram-index/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ DROP INDEX idx_person_trigram;

DROP INDEX idx_community_trigram;

DROP EXTENSION pg_trgm;

18 changes: 18 additions & 0 deletions migrations/2023-08-23-182533_scaled_rank/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ ALTER TABLE local_user
-- drop the old enum
DROP TYPE sort_type_enum__;

-- Remove int to float conversions that were automatically added to index filters
DROP INDEX
idx_comment_aggregates_nonzero_hotrank,
idx_community_aggregates_nonzero_hotrank,
idx_post_aggregates_nonzero_hotrank;

CREATE INDEX idx_community_aggregates_nonzero_hotrank ON community_aggregates (published)
WHERE
hot_rank != 0;

CREATE INDEX idx_comment_aggregates_nonzero_hotrank ON comment_aggregates (published)
WHERE
hot_rank != 0;

CREATE INDEX idx_post_aggregates_nonzero_hotrank ON post_aggregates (published DESC)
WHERE
hot_rank != 0 OR hot_rank_active != 0;

2 changes: 1 addition & 1 deletion migrations/2023-09-18-141700_login-token/down.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DROP TABLE login_token;

ALTER TABLE local_user
ADD COLUMN validator_time timestamp NOT NULL DEFAULT now();
ADD COLUMN validator_time timestamptz NOT NULL DEFAULT now();

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ALTER TABLE mod_remove_community
ADD COLUMN expires timestamp;
ADD COLUMN expires timestamptz;

0 comments on commit ced9bb5

Please sign in to comment.