Skip to content

PRIMARY KEY only for table, FOREIGN TABLE not support it #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pkg
/.idea
17 changes: 15 additions & 2 deletions lib/activerecord-clean-db-structure/clean_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,24 @@ def run
# Remove useless comment lines
dump.gsub!(/^--$/, '')

# remove comments
dump.gsub!(/^-- Name.+ Type: (COMMENT|EXTENSION|INDEX|TABLE|TYPE)$/, '')

### NEED FOR TEST DATABASE `rails db:setup`
# remove server and user for FDW
dump.gsub!(/^-- Name: [\w ]+; Type: SERVER.+?;/m, '')
dump.gsub!(/^-- Name: [\w ]+; Type: USER MAPPING.+?;/m, '')

# FDW table to native table
dump.gsub!(/^CREATE FOREIGN TABLE (.+?)\nSERVER.+?;/m, "CREATE TABLE \\1;")
###

# Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
#
# This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
dump.gsub!(/^ id integer NOT NULL(,)?$/, ' id SERIAL PRIMARY KEY\1')
dump.gsub!(/^ id bigint NOT NULL(,)?$/, ' id BIGSERIAL PRIMARY KEY\1')
dump.gsub!(/^CREATE TABLE ([\w.]+) \(\n id integer NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id SERIAL PRIMARY KEY\\2;")
dump.gsub!(/^CREATE TABLE ([\w.]+) \(\n id bigint NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id BIGSERIAL PRIMARY KEY\\2;")

dump.gsub!(/^ id uuid DEFAULT (public\.)?uuid_generate_v4\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1uuid_generate_v4() PRIMARY KEY\2')
dump.gsub!(/^ id uuid DEFAULT (public\.)?gen_random_uuid\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1gen_random_uuid() PRIMARY KEY\2')
dump.gsub!(/^CREATE SEQUENCE [\w\.]+_id_seq\s+(AS integer\s+)?START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
Expand Down