From af0492dff0c33a6470a846b2c94b8def5a6e9301 Mon Sep 17 00:00:00 2001 From: Sven Bohm Date: Wed, 17 Jul 2019 09:23:07 -0700 Subject: [PATCH 1/2] script to convert table and column names from CamelCase to snake_case --- util/Readme.md | 8 ++++ util/snake_case/Gemfile | 7 ++++ util/snake_case/Readme.md | 22 +++++++++++ util/snake_case/to_snake_case.rb | 66 ++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 util/Readme.md create mode 100644 util/snake_case/Gemfile create mode 100644 util/snake_case/Readme.md create mode 100644 util/snake_case/to_snake_case.rb diff --git a/util/Readme.md b/util/Readme.md new file mode 100644 index 0000000..84a3969 --- /dev/null +++ b/util/Readme.md @@ -0,0 +1,8 @@ +# Utilities directory + +A place to hold various utilities to help with dealing with lter_metabase + +## snake_case + +A ruby script to convert the tables in the `lter_metabase` schema from CamelCase +to glorious, easy on the fingers snake_case. diff --git a/util/snake_case/Gemfile b/util/snake_case/Gemfile new file mode 100644 index 0000000..4bfd250 --- /dev/null +++ b/util/snake_case/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'main' +gem 'pg' +gem 'sequel' diff --git a/util/snake_case/Readme.md b/util/snake_case/Readme.md new file mode 100644 index 0000000..74e9379 --- /dev/null +++ b/util/snake_case/Readme.md @@ -0,0 +1,22 @@ +# snake case + +A ruby script to convert the tables in the `lter_metabase` schema from CamelCase +to glorious, easy on the fingers snake_case. + +## Usage + +Install required gems + +``` +$ bundle +``` + +to run as a test in a transaction (nothing actually is changed in the db) +``` +$ ruby to_snake_case lter_metabase +``` + +to apply the changes +``` +$ ruby to_snake_case lter_metabase --production +```` diff --git a/util/snake_case/to_snake_case.rb b/util/snake_case/to_snake_case.rb new file mode 100644 index 0000000..95188a9 --- /dev/null +++ b/util/snake_case/to_snake_case.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'logger' +require 'main' +require 'sequel' +require 'pg' + +# monkey patch from ActiveSupport +class String + def underscore + self.gsub(/::/, '/'). + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + tr('-', '_'). + downcase + end +end + +# convert tables and columns of the lter_metabase schema to snake_case if it's +# not already +class Snakeify + def rename_columns(db, table_schema_and_name, table) + db.alter_table(table_schema_and_name) do + table.columns.each do |column_name| + next if column_name.to_s == column_name.to_s.underscore + + rename_column(column_name, column_name.to_s.underscore) + end + end + end + + def rename_table(db, table_schema_and_name, table_name) + db.rename_table(table_schema_and_name, Sequel.qualify(:lter_metabase, table_name.underscore)) + end + + def to_snake_case(db_name, production) + pr = Sequel.postgres(database: db_name, loggers: [Logger.new($stdout)]) + pr.transaction do + query = "select table_name from information_schema.tables where table_schema = 'lter_metabase'" + tables = pr[query] + + tables.each do |table_hash| + table_name = table_hash[:table_name] + table_schema_and_name = Sequel.qualify(:lter_metabase, table_name) + table = pr[table_schema_and_name] + + rename_columns(pr, table_schema_and_name, table) + next if table_name == table_name.underscore + + rename_table(pr, table_schema_and_name, table_name) + end + raise unless production + end + end +end + +Main do + argument 'db_name' + option 'production' + + def run + snake = Snakeify.new + snake.to_snake_case(params['db_name'].value, + params['production'].value) + end +end From 5ad521af5ef496c1ff2474ef45545cf7f720d7f1 Mon Sep 17 00:00:00 2001 From: Sven Bohm Date: Wed, 17 Jul 2019 09:25:56 -0700 Subject: [PATCH 2/2] fix spelling error --- util/snake_case/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/snake_case/Readme.md b/util/snake_case/Readme.md index 74e9379..a964fd1 100644 --- a/util/snake_case/Readme.md +++ b/util/snake_case/Readme.md @@ -18,5 +18,5 @@ $ ruby to_snake_case lter_metabase to apply the changes ``` -$ ruby to_snake_case lter_metabase --production +$ ruby to_snake_case.rb lter_metabase --production ````