I have been learning about databases and wanted to know, if is there a Datamapper method -or another "ruby" way?- for creating an empty database rather than using create database "bd_name"; in psql, in a Sinatra project?
What you should be doing is creating a Rakefile and defining a Rake task to create the database and also a task to run the migrations after you make any structural changes to your database.
With DataMapper, this is somewhat more work than with ActiveRecord (which provides it's own DB tasks).
Have a look at the Rakefile provided by the Padrino framework for inspiration.
Related
I'm confused on after we do db:migrate, what does the schema means? Does it automatically change the models if they are connected or do we need to manually change the models to reflect the schema?
If you're talking about db/schema.rb file, it is not the same thing as a migration. The migration is designed to run database commands to update the actual underlying database schema. When you run rake db:migrate it attempts to run any pending migrations on the database. If successful, it will also under the hood run rake db:schema:dump which will update the db/schema.rb file which reflects the latest version of the actual schema. If for some reason the migration fails because of some error, it will rollback and leave the schema in its previous state to having run the migration.
The models in a Rails don't touch the schema itself, but there are generators the help you create a migration file. For a better understanding of how all this works, you really should read through the documentation here
the db/schema.rb file represents the current configuration of the database, after any successful (or unsuccessful) migrations.
ActiveRecord uses db/schema.rb to 'automatically' configure attribute methods, for example if there's a users table with a column first_name then the User model will have a method name and a method name= as the getter and setter methods for that column.
You don't have to configure the models yourself, ActiveRecord does it all for you.
ActiveRecord also takes care of typecasting the variables as appropriate for the column defined in db/schema.rb.
I'm a newbie to all of this. I'm running a project that uses the sqlite3 Ruby gem, but for some reason one of the table exists already. The project contains a ".sql" and ".db" file, among others.
How can you "view" or manipulate this database if it's running from a sqlite3 gem? Specifically, can you bundle exec rake, etc on it?
From a Rails perspective Sqlite works the same as any other database such as PostreSQL or MySQL in that you can run rake tasks on it (e.g. rake db:create, or db:setup, db:reset, db:migrate, etc. all work as expected). You can also use the rails console to query for or insert new data via your active record model objects.
From a non-Rails perspective, Sqlite has various command-line and GUI interfaces that you can install to interact with a Sqlite database using SQL commands.
I'm trying to learn martini, coming from Rails. What's being used for database migrations in the martini world?
There is no such thing in martini. It is just a helper for writing web services. If you want database migrations, or a database at all, use third-party packages.
An example tool stack would be:
goose for creating migrations
gorp for a database object layer
This or a completely different setup, e.g. using Go's standard database package, can be used with martini.
My first time with Padrino and DataMapper. If I understood correctly,
padrino g model Foo will make models/foo.rb as well as db/migrate/001_create_foos.rb.
In order to create an index, I need to specify it in the model, not in the migration.
padrino rake dm:auto:{upgrade|migrate} will generate the database from models, without paying attention to db/migrate folder.
So, it seems that migrations are only kept as "just in case" for upgrading production databases. Is there a way to generate a migration from the model? If not, does everyone else also have a headache trying to keep migrations up-to-date as you change your model? How do people write their migrations?
According Padrino documentation i guess you're right. The Orm section
states that the auto namespace won't use the generated migrations.
Basically, instead of writing migrations you can directly edit your schema.rb and perform a non destructive migration with padrino rake ar:auto:upgrade
I guess you can track the database with migrations removing the auto namespace as:
rake dm:migrate # Migrates the database to the latest version
rake dm:migrate:down[version] # Migrates down using migrations
rake dm:migrate:up[version] # Migrates up using migrations
And generate migrations as described in the Migration Generator section
I would like to use ActiveRecord in a project without Rails.
I would also like to specify the database connections in config/database.yml like Rails does.
There is already one answer how multiple database connections work with Rails.
ActiveRecord talk to two databases?
I was unsuccessful using this approach and I'm not sure why. I only get the error message "database configuration does not specify adapter".
Is there some kind of magic in Rails that reads the database.yml that is not in ActiveRecord?
I could read it myself but then I don't know how to feed it to ActiveRecord so that I can use establish_connection in each model.
I found out how it works. You just fill the ActiveRecord::Base.configurations hash and then it works.
http://apidock.com/rails/ActiveRecord/Base/configurations/class