From 67f4135c54c330fb5386ffe7192ac16c237a1a9c Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Sat, 9 May 2015 21:02:36 +0200 Subject: [PATCH] Import all indexes including their names. --- README.md | 6 ++---- add_index_statements.rb | 14 -------------- db_converter.py | 28 ++++++++++++++++++---------- index_create_statements.rb | 35 ----------------------------------- 4 files changed, 20 insertions(+), 63 deletions(-) delete mode 100644 add_index_statements.rb delete mode 100644 index_create_statements.rb diff --git a/README.md b/README.md index e3181b0..bd73ad6 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,11 @@ GitLab. - Guard against replacing '0000-00-00 00:00:00' inside SQL text fields. - Replace all MySQL zero-byte string literals `\0`. This is safe as of GitLab 6.8 because the GitLab database schema contains no binary columns. -- Add the `add_index_statements.rb` script to recreate indices dropped by - `db_converter.py`. - Never set 'NOT NULL' constraints on datetimes. - Drop sequences before creating them. - Preserve default values of boolean (originally `tinyint(1)`) columns. +- Import all indexes. +- Import index names. How to use ---------- @@ -41,8 +41,6 @@ Next, load your new dump into a fresh PostgreSQL database using: `psql -f databasename.psql -d gitlabhq_production` -Finally, [recreate the indexes for your GitLab version](http://doc.gitlab.com/ce/update/mysql_to_postgresql.html#rebuild-indexes). - More information ---------------- diff --git a/add_index_statements.rb b/add_index_statements.rb deleted file mode 100644 index bd2d3bd..0000000 --- a/add_index_statements.rb +++ /dev/null @@ -1,14 +0,0 @@ -ARGF.each_line.grep(/add_index/) do |index_statement| - # Skip unique indexes because the lanyrd converter preserves them - next if index_statement.include?('unique: true') - - puts < e - puts e -end - -EOS -end diff --git a/db_converter.py b/db_converter.py index 29df056..9124c59 100644 --- a/db_converter.py +++ b/db_converter.py @@ -28,7 +28,7 @@ def parse(input_filename, output_filename): creation_lines = [] enum_types = [] foreign_key_lines = [] - fulltext_key_lines = [] + index_lines = [] sequence_lines = [] cast_lines = [] num_inserts = 0 @@ -181,15 +181,23 @@ def parse(input_filename, output_filename): elif line.startswith("CONSTRAINT"): foreign_key_lines.append("ALTER TABLE \"%s\" ADD CONSTRAINT %s DEFERRABLE INITIALLY DEFERRED" % (current_table, line.split("CONSTRAINT")[1].strip().rstrip(","))) foreign_key_lines.append("CREATE INDEX ON \"%s\" %s" % (current_table, line.split("FOREIGN KEY")[1].split("REFERENCES")[0].strip().rstrip(","))) + elif line.startswith("UNIQUE KEY \""): + index_name = line.split('"')[1].split('"')[0] + index_columns = line.split("(")[1].split(")")[0] + index_lines.append("CREATE UNIQUE INDEX \"%s\" ON %s (%s)" % (index_name, current_table, index_columns)) elif line.startswith("UNIQUE KEY"): - creation_lines.append("UNIQUE (%s)" % line.split("(")[1].split(")")[0]) + index_columns = line.split("(")[1].split(")")[0] + index_lines.append("CREATE UNIQUE INDEX ON %s (%s)" % (current_table, index_columns)) + elif line.startswith("KEY \""): + index_name = line.split('"')[1].split('"')[0] + index_columns = line.split("(")[1].split(")")[0] + index_lines.append("CREATE INDEX \"%s\" ON %s (%s)" % (index_name, current_table, index_columns)) + elif line.startswith("KEY"): + index_columns = line.split("(")[1].split(")")[0] + index_lines.append("CREATE INDEX ON %s (%s)" % (current_table, index_columns)) elif line.startswith("FULLTEXT KEY"): - fulltext_keys = " || ' ' || ".join( line.split('(')[-1].split(')')[0].replace('"', '').split(',') ) - fulltext_key_lines.append("CREATE INDEX ON %s USING gin(to_tsvector('english', %s))" % (current_table, fulltext_keys)) - - elif line.startswith("KEY"): - pass + index_lines.append("CREATE INDEX ON %s USING gin(to_tsvector('english', %s))" % (current_table, fulltext_keys)) # Is it the end of the table? elif line == ");": output.write("CREATE TABLE \"%s\" (\n" % current_table) @@ -222,9 +230,9 @@ def parse(input_filename, output_filename): for line in sequence_lines: output.write("%s;\n" % line) - # Write full-text indexkeyses out - output.write("\n-- Full Text keys --\n") - for line in fulltext_key_lines: + # Write indexes out + output.write("\n-- Indexes --\n") + for line in index_lines: output.write("%s;\n" % line) # Finish file diff --git a/index_create_statements.rb b/index_create_statements.rb deleted file mode 100644 index d9e94f7..0000000 --- a/index_create_statements.rb +++ /dev/null @@ -1,35 +0,0 @@ -# index_create_statements.rb -# -# Parse db/schema.rb for GitLab and extract PostgreSQL `CREATE INDEX` -# statements. -# -# ruby index_create_statements.rb /home/git/gitlab/db/schema.rb -# -# This script was created to work around the fact that db_converter.py strips -# all plain `KEY` statements from MySQL's `CREATE TABLE` statements. - -# Hack to get to the block that contains the actual schema definition -module ActiveRecord - module Schema - def self.define(*args) - yield - end - end -end - -# We only want the add_index statements, so ignore everything else -def enable_extension(*args); end -def create_table(*args); end - -def add_index(table_name, index_columns, options) - index_name = options.delete(:name) - index_using = options.delete(:using) - - # Unique indexes are already created by the lanyrd MySQL to Postgres converter - return if options[:unique] - - # Create indexes concurrently in case the database is in production - puts "CREATE INDEX CONCURRENTLY #{index_name} ON #{table_name} USING #{index_using} (#{index_columns.join(', ')});" -end - -eval ARGF.read