jpignata/temping

Underscore being dropped somewhere between table name and table creation

originalpete opened this issue · 1 comments

Somewhere between initialising a new Temping table and actually creating that table in SQL, the underlying table name changes and drops an underscore.

Here's my Rails code:

        table_name = "foo_bar_"+SecureRandom.hex(5)
        klass = Temping.create table_name, temporary: false do
          with_columns do |t|
            t.integer :foo
            t.integer :bar
          end

and the output:

 (4.7ms)  CREATE TABLE "foo_bar2c125cd329s" ("id" serial primary key)
   (0.6ms)  ALTER TABLE "foo_bar2c125cd329s" ADD "foo" integer
   (0.5ms)  ALTER TABLE "foo_bar2c125cd329s" ADD "bar" integer
 => FooBar2c125cd329(id: integer, foo: integer, bar: integer) 

Note how the initial table prefix of foo_bar_ has been changed to just foo_bar when the table is created.

kitop commented

That's because of how Rails works with classify and tableize.

"foo_bar_123".classify # => FooBar123
"FooBar123".tableize # => "foo_bar123s"
"foo_bar_a123".classify # => "FooBarA213"
"FooBarA213".tableize # => "foo_bar_a123s"

If you want to use that setup, I'd suggest you add a letter before SecureRandom to ensure the tables are always consistently prefixed.