Get DDL of all tables in a postgresql schema - postgresql-12

I need to get the "CREATE TABLE" statements for all the tables in a particular schema of postgreSQL. How can I do so? Thanks in advance.

Related

Make a new column (migration) between two other columns?

Is there a quick/easy way to make a migration that adds a new column between two existing columns?
Note: I googled and couldn't find an obvious answer. But I also am curious if it's even good practice (since if for some reason other column/s were removed, then the migration may fail?)
If you haven't commited your changes to production you can reorder migrations by rolling them back and then changing the timestamps in the file name.
If thats not an alternative you can actually re-order the columns on some databases directly with SQL even if its not part of the migrations DSL. Migrations are after all really just a DSL to create SQL strings and run them in a repeatable way across environments.
If you can't generate the SQL you want with the DSL you can always use execute to execute a raw DSL string.
# MySQL example
class ReorderYourTableName < ActiveRecord::Migration[5.2]
def change
execute "alter table yourTableName change column yourColumnName yourColumnName dataType after yourSpecificColumnName;"
end
end
However on some DBs you can't actually reorder the columns without extensive steps of creating new columns and shuffling the data around.

How to create postgres database for Rails app manually?

How can I create postgres db for Rails app properly but in psql, not via rake db:create?
I mean, one can always write CREATE DATABASE project_name, but I don't know what happens in that rake task under the hood. Maybe there are a lot of additional params.
Update
After first answer I decided to clarify: I know how to write and use migrations, they are awesome, but my question not about them. It's about rake db:create task and pg adapter.
In other words, I just want to know which command in psql is equal to rake db:create.
If you select the db on pgadmin III it will show you the sql instructions with the local things to load. They are very importanst if you have full text index on. You must run them from the database postgres.
Rails expects table names to match model names but be plural and snake_case. For example, a User model will store records in a users table and a BlogEntry model will store records in a blog_entries table.
Rails expects a table's primary key to be named id and it expects foreign keys to match model names but be snake_case and end with _id. For example, if BlogEntry belongs_to User, Rails will expect the blog_entries table to have a user_id column.
Join tables (such as used with many-to-many relations) are expected to be named with the two models' names in plural snake case and alphabetical order. For example, a join table describing a many-to-many relation between a User model and a Blog model would be expected to have the name blogs_users and have, at the least, the columns blog_id and user_id.
Those are the basics. Of course, all of this is configurable: For example, you can use the table_name class method to tell a model to use a table with a different name, and the relation methods (belongs_to, has_many, etc.) all take options letting you specify different names.
Apart from these naming conventions Rails doesn't require anything special from a database, as long as the correct credentials and configuration are specified in config/database.yml.

"Show tables like" in postgresql

Is there any way to list all tables that contain certain keywords in psql?
In other words, is there a similar command as in mysql:
show tables like "%test%" ?
Just found the answer..
\dt *test* will pull tables: 123_test_234, test_234, 123_test etc.
\dt *test will pull tables: 123_test etc.
\dt test* will pull tables: test_234 etc.
Not sure if there is a similar question. If there is, maybe I should delete this.

ActiveRecord changes my binary column to a string column, for some reason, on migration

Using Ruby on Rails 3 and ActiveRecord 3.2.18. I have a binary column in my database. It holds binary stuff, and actually I have a bunch of records in production with that column filled. So in my db/schema.rb file I had...
...
t.binary "tin"
...
Now, after running a migration that touches this table but doesn't change that column, my schema says...
...
t.string "tin"
...
Well... I know that a string might be binary, and binary might be a string, depending on how it's stored in the database, and maybe these equate to the same column type in the end, but why is this happening and what can I do to fix it? Is it safe to deploy this change to production or will it hose my binary columns?
When you run a rake command such as rake db:migrate, Rails will recreate the schema.rb file from the schema in your own personal database. So it sounds like your database has the tin field setup as a varchar field. If your migrations set up your database this way and your production server has the same database then I wouldn't count on the production server to do the right thing. So you may need to look into how to really set a binary field.
On the other hand, if your database is setup properly and it's just the schema file that's not then it may be because... schema can't interpret every database-specific column type. In these cases, you can switch your schema to dump to schema_dump.sql instead of to schema.rb. So check this Stack Overflow post for more on that.

creating the id and timestamp columns with sql?

I'm trying to seed a Rails application with some sql statements in the seed.rb file. There are 12 values supplied, however, the table has 15 columns. The extra three columns are the automatically generated id, created_at and updated at columns that Rails includes by default. If I run a custom sql statement in the seed.rb file in the following manner....
connection = ActiveRecord::Base.connection()
query = "random sql"
connection.execute(query)
Rails doesn't create those columns for me in the way it would if I did
Employee.create!(name: "Joe")
Is there anyway to indicate to rails that I need the id and timestamp columns filled with values when I run an sql statement in seed.rb?
No, because Rails has no way of knowing whether your "random sql" even creates any records for it to fill in ids/timestamps.
When you connection.execute you are on your own, you have forsaken your ORM and given in to the temptation of SQL.
If you can do it using ActiveRecord, then do so! If not, well, that is why Rails lets you drop down to SQL (but think again. Can you really not write it in Ruby?).

Resources