Downgrade migration version in aqueduct - dart

Is there any way to downgrade migration version in aqueduct. Problem is if I delete migration file which is no longer needed then by running
aqueduct db generate
it will create migration version with number of deleted version so when running
aqueduct db upgrade..
then the database are not upgraded due to low version number in migration file so I have to manually change version number to be able to upgrade the db

If you haven't deployed your database on a production server, then I recommend just dropping the database, recreating it, and starting over with a fresh single migration file.
If you have deployed the database to a live server, then I wouldn't remove any migration files. Just make sure the result of all the migrations is the schema that you need. (If there is a way to decrease the database version number, I'm not sure what it is.)
On another note, don't run
aqueduct db generate
on your live server. Instead, do it on your dev machine and then use the generated migration file to update the database on the production server. I discovered this the hard way when my dev machine server and production server database versions got out of sync (because I deleted a migration file "that I didn't need".)

Related

Ruby on Rails accidentally db:rollback

I'm new to rails, and I accidentally ran rails db:rollback command in a development.
Next I did rails db:migrate:up VERSION=XXXX to change status of the file I rolled back from down to up.
The migration file was about images. However my images were gone in a development mode due to rollback, files status the same as before I ran rails db:rollback.
In this case, if I pushed this to remote repository, and it's merged in production, the images already there will be gone as well as mine in development?
When add_column method in migration is run you just add column in migration so it will run for production & development environment. Now you added images through localhost to your application and stored in database. So those will be stored to database regardless migration.
Rollback will remove column running remove_column so it will hamper your development as removing column will make you loose all data inside column of table. So on production it does not deal same.
Images are getting pushed to production database or remote repository, it is just to add or remove column only so rollback will affect only your local/development
Unless you're doing something wacky in your migrations, any goofs like this you make to your development database will not effect production. That's why dev and prod databases are separate.
The general problem of "is it safe to push to production" can be mitigated by adding a staging server which runs in the production Rails environment, but is used for additional manual testing of new features. Once everything checks out in staging then push to production. Many services provide a "pipeline" to do this for you, for example Heroku Pipelines.

Rails4: production environment create database not using many migrations

I am deploying a rails app to nginx.
There are many migrations in the development stage.
How to create the production schema in a simple way instead of reading many migration.rb files ?
Because I deleted several migration files during develepment. Now when deploy production environment it shows me some errors
Thanks
You can use the schema.rb file (via rake db:schema:load), but beware it will drop all existing tables. If you have existing data you will lose it.
Once you load the schema then it sets the database version, so only new migrations that are created after the schema file was created will run in the future.
Here is some info for Rails 4.2 about schema dumping:
Schema Dumping and You

Grails deployment on production - manually db migration

I'm coming from the world of python, django where usually our deployment flow was as follow:
tar/gz our code release
unpack on the production
Run db migration manually via south
Run the app
Grails is a little bit different than python/django mainly because the end product is a compiled war. My biggest problem is the manual DB migration. I don't want to run it automatically, one suggested solution that I saw is to use dbm-update-sql to generate manual sql file, but in order to produce it I need my local DB to have the same version as the production DB - I don't like it.
any other suggestions ? it looks to me like the only way to run it manually is to deploy yhe source code on the machine and run the dbm commands there.
You can run dbm-update-sql against the production database, it won't make any changes since like all of the -sql scripts it's there to show you what would be done in the event of a real migration. To be safe, create a user that doesn't have permission to make any changes and use that when you run the script. Create a custom environment in DataSource.groovy with that user info and the production connection info and specify that environment when running the script.
I'd highly recommend not deploying the source to your production systems. Since, you want to manually control your database migrations, outside of the normal flow of a Grails application I'd recommend you look at using liquibase as a stand alone tool.
Obviously since you don't want to manage having a copy of your production schema to diff against this is going to be a lot of manual work for you (e.g. keeping your changes up to date).
The database migration plugin can be used to create sql scripts that you manually run, but you do need a production schema to diff against. I'd recommend you head this route, but you seem set against doing so.

SQLite > PostgreSQL Rails migrations

I'm developing on my localhost using sqlite and deploying to Postgres on heroku, where the live app is already on heroku.
I've done a lot of development lately which includes several db structure migrations, so I've pulled the Postgres live database to my localhost to run the migrations locally to test (and swapped my local db to Postgres).
When I run rake db:migrate, it tries to run all the migrations from the beginning of time, rather than just the new ones.
How does rake know which migrations to run?
When you migrate first time , then rake will run all the migration .
When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration. As a result, each migration class is applied exactly once to the database.
source: https://stackoverflow.com/a/6319952/1970061
previously your database was different i.e sqlite, but when you switched to postgres you created new database, hence all table must be created for new database therefore all migration is running

After setting up local dev environment with database-migration plugin, how to then update test servers

Following the documentation I was able to get the database-migration plugin working on an existing project which already has quite a few tables and is running in production. I went through the following process locally:
Pulled down latest production database
Source production database to local dev
grails dbm-generate-changelog changelog.groovy
grails dbm-changelog-sync
grails dbm-gorm-diff 2012-06-25-latest.groovy --add
grails dbm-update
I understand why I had to do each of those locally to get to a point of applying future change sets. However, now I want to run my 2012-06-25-latest.groovy on one of my test servers. It already has the latest database based on our production database.
I tried just running dbm-update but without the sync it failed creating tables that already exist. So I ran dbm-changelog-sync but then when running dbm-update it didn't actually apply the latest file.
I know that I can add a context (tag) to the change sets and specify that context when running dbm-update but I want to know if this is the only way to go about this or if my workflow needs to be modified; what is the best way to go about applying the changelog to the test server?
What I ended up doing is deleting all the rows in the DATABASECHANGELOG table where the FILENAME = '2012-06-25-latest.groovy'. I then ran dbm-status and it told me I had 256 changes waiting. I then ran dbm-update and all is well.
I'm not sure this is how it was supposed to be done, but it seems to have worked.
UPDATE: In addition to this I could probably run the entire migration on an empty database, and then do a mysqldump of the production database with INSERT statements only.

Resources