From 91fca27126e35f42494cc6fa35c959316a124d2d Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 21 Sep 2022 21:36:43 -0500 Subject: [PATCH] db: fix types of various string columns. Fix various columns to be either `character varying` or `text`, depending on what kind of text is stored in the column. `text` is used for columns that contain free-form natural language, like pool and forum topic titles, while `character varying` is used for short strings that don't contain free-form text, like URLs and status fields. Both types are treated the same by Postgres; the only difference is how we treat them in Rails. In edit forms, `text` fields use multi-line textboxes, while `character varying` fields use single-line inputs. And during search, we allow `text` fields to be searched using full-text search, but not `character varying` fields. --- app/views/forum_topics/_form.html.erb | 2 +- ...326_fix_string_types_on_multiple_tables.rb | 31 +++++++++++++++++++ db/structure.sql | 31 ++++++++++--------- 3 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20220922014326_fix_string_types_on_multiple_tables.rb diff --git a/app/views/forum_topics/_form.html.erb b/app/views/forum_topics/_form.html.erb index a8281c9f6..ca1607114 100644 --- a/app/views/forum_topics/_form.html.erb +++ b/app/views/forum_topics/_form.html.erb @@ -1,6 +1,6 @@
<%= edit_form_for(forum_topic) do |f| %> - <%= f.input :title %> + <%= f.input :title, as: :string %>
diff --git a/db/migrate/20220922014326_fix_string_types_on_multiple_tables.rb b/db/migrate/20220922014326_fix_string_types_on_multiple_tables.rb new file mode 100644 index 000000000..4304e66fa --- /dev/null +++ b/db/migrate/20220922014326_fix_string_types_on_multiple_tables.rb @@ -0,0 +1,31 @@ +class FixStringTypesOnMultipleTables < ActiveRecord::Migration[7.0] + def up + change_column :artist_urls, :url, :string + change_column :saved_searches, :query, :string + change_column :tag_aliases, :status, :string + change_column :tag_implications, :status, :string + change_column :uploads, :source, :string + change_column :uploads, :status, :string + change_column :uploads, :referer_url, :string + change_column :post_replacements, :original_url, :string + change_column :post_replacements, :replacement_url, :string + + change_column :forum_topics, :title, :text + change_column :pools, :name, :text + end + + def down + change_column :artist_urls, :url, :text + change_column :saved_searches, :query, :text + change_column :tag_aliases, :status, :text + change_column :tag_implications, :status, :text + change_column :uploads, :source, :text + change_column :uploads, :status, :text + change_column :uploads, :referer_url, :text + change_column :post_replacements, :original_url, :text + change_column :post_replacements, :replacement_url, :text + + change_column :forum_topics, :title, :string + change_column :pools, :name, :string + end +end diff --git a/db/structure.sql b/db/structure.sql index 1c3387560..b9762d5fb 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -231,7 +231,7 @@ ALTER SEQUENCE public.artist_commentary_versions_id_seq OWNED BY public.artist_c CREATE TABLE public.artist_urls ( id integer NOT NULL, artist_id integer NOT NULL, - url text NOT NULL, + url character varying NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, is_active boolean DEFAULT true NOT NULL @@ -764,7 +764,7 @@ CREATE TABLE public.forum_topics ( id integer NOT NULL, creator_id integer NOT NULL, updater_id integer NOT NULL, - title character varying NOT NULL, + title text NOT NULL, response_count integer DEFAULT 0 NOT NULL, is_sticky boolean DEFAULT false NOT NULL, is_locked boolean DEFAULT false NOT NULL, @@ -1248,7 +1248,7 @@ ALTER SEQUENCE public.pool_versions_id_seq OWNED BY public.pool_versions.id; CREATE TABLE public.pools ( id integer NOT NULL, - name character varying NOT NULL, + name text NOT NULL, description text, is_active boolean DEFAULT true NOT NULL, post_ids integer[] DEFAULT '{}'::integer[] NOT NULL, @@ -1426,8 +1426,8 @@ CREATE TABLE public.post_replacements ( id integer NOT NULL, post_id integer NOT NULL, creator_id integer NOT NULL, - original_url text NOT NULL, - replacement_url text NOT NULL, + original_url character varying NOT NULL, + replacement_url character varying NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, old_file_ext character varying, @@ -1645,7 +1645,7 @@ ALTER SEQUENCE public.rate_limits_id_seq OWNED BY public.rate_limits.id; CREATE TABLE public.saved_searches ( id integer NOT NULL, user_id integer NOT NULL, - query text NOT NULL, + query character varying NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, labels text[] DEFAULT '{}'::text[] NOT NULL @@ -1691,7 +1691,7 @@ CREATE TABLE public.tag_aliases ( consequent_name character varying NOT NULL, creator_id integer NOT NULL, forum_topic_id integer, - status text DEFAULT 'active'::text NOT NULL, + status character varying DEFAULT 'active'::text NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, approver_id integer, @@ -1730,7 +1730,7 @@ CREATE TABLE public.tag_implications ( consequent_name character varying NOT NULL, creator_id integer NOT NULL, forum_topic_id integer, - status text DEFAULT 'active'::text NOT NULL, + status character varying DEFAULT 'active'::text NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, approver_id integer, @@ -1909,12 +1909,12 @@ ALTER SEQUENCE public.upload_media_assets_id_seq OWNED BY public.upload_media_as CREATE TABLE public.uploads ( id integer NOT NULL, - source text, + source character varying, uploader_id integer NOT NULL, - status text DEFAULT 'pending'::text NOT NULL, + status character varying DEFAULT 'pending'::text NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, - referer_url text, + referer_url character varying, error text, media_asset_count integer DEFAULT 0 NOT NULL ); @@ -3435,7 +3435,7 @@ CREATE INDEX index_artist_urls_on_artist_id ON public.artist_urls USING btree (a -- Name: index_artist_urls_on_regexp_replace_lower_url; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_artist_urls_on_regexp_replace_lower_url ON public.artist_urls USING btree (((regexp_replace(lower(url), '^https?://|/$'::text, ''::text, 'g'::text) || '/'::text)) text_pattern_ops); +CREATE INDEX index_artist_urls_on_regexp_replace_lower_url ON public.artist_urls USING btree (((regexp_replace(lower((url)::text), '^https?://|/$'::text, ''::text, 'g'::text) || '/'::text)) text_pattern_ops); -- @@ -3988,7 +3988,7 @@ CREATE INDEX index_forum_topics_on_is_sticky_and_updated_at ON public.forum_topi -- Name: index_forum_topics_on_title_tsvector; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_forum_topics_on_title_tsvector ON public.forum_topics USING gin (to_tsvector('english'::regconfig, (title)::text)); +CREATE INDEX index_forum_topics_on_title_tsvector ON public.forum_topics USING gin (to_tsvector('english'::regconfig, title)); -- @@ -4506,7 +4506,7 @@ CREATE INDEX index_pools_on_is_deleted ON public.pools USING btree (is_deleted); -- Name: index_pools_on_lower_name; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_pools_on_lower_name ON public.pools USING btree (lower((name)::text)); +CREATE INDEX index_pools_on_lower_name ON public.pools USING btree (lower(name)); -- @@ -6636,6 +6636,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20220918031429'), ('20220919041622'), ('20220920224005'), -('20220921022408'); +('20220921022408'), +('20220922014326');