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.
Related
I am running the update-database command in VS to create tables in a DB for a project developed by someone else. For some reason, when i run the command, all the tables are prefixed with my domain\username. The same also happens for one of my collegues, meaning we now have two sets of tables! Any idea how we stop this happening?
This happens on when you are not a database owner and the object names are not schema-qualified. So either run the migration as a database owner, or specify a schema in the model configuration to force all the objects into the dbo schema.
everyone. I use Symfony 4.2 and following database-first approach and have auto-generated entities; But then I need to do some changes in field definitions in entities but I don't want to affect the database structure. Everything works well but if I try to create a migration, doctrine includes all the differences in migrations, and I find no way to prevent this behaviour. I've tried schema_filter: ~^migration_versions$~ but somehow it doesn't help.
So the questions:
1) is it a normal application state on prod when column definitions slightly differ in database and entities?
2) how can I say to doctrine to ignore differences in some tables when creating migrations? Thanks.
When you run bin/console doctrine:migrations:diff it will generate a file in your src\Migrations\ directory. You can edit the generated file to remove whatever you don't want to change before you run bin/console doctrine:migrations:migrate.
I don't suggest doing this on a production server though, and especially if you do so then you should certainly have a backup of your database.
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.
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
In my project, current approach is to create database if not already exists using CreateDatabaseIfNotExists and doing seeding initial data from that Intializer as well. I also added Code First Migration support after upgrade to Entity 4.4, so that in the future when we change the modle/database structure we can update client side database without drop their exist database.
However it didn't seems to working well, for example, I am now stuck on design time where forms wouldn't load and the error message is something like The model backing the 'myEntities' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).. But the model and database is indeed the updated version, just seems Migration didn't recognize the database generated by CreateDatabaseIfNotExists, but at the same time all seems working well at run time.
Also after that I noticed that if I let CreateDatabaseIfNotExists initialize a database, Add-migration afterwards will fail and complain that pending migration and ask me to do a update-database. When I try to do a the update-database, it will fail as well because the migration path seems assume the database is in initial setup state and will trying to running all the migration scripts while none should be run as the database generated by CreateDatabaseIfNotExists is indeed sync with current model and should not be migrated at all.
I discovered that there is a MigrationHistory table in System Tables, that table will always save the database initialize history regardless of whether the initializer is CreateDatabaseIfNotExists or MigrateDatabaseToLatestVersion. The difference is that if database is initialized by CreateDatabaseIfNotExists1, everytime the database initialized, the migriationId for that initialize record will be different, butMigrateDatabaseToLatestVersion` will always save same set of migrationId for each of the Migration steps. I guess that is how Entity Framework 5.0 works.
So in the end, I give up and rewrite my DB Access code to seeding the initial database data
in other part of my code rather than the CreateDatabaseIfNotExists or MigrationsConfiguration` class because either suit my need.