example.env when migrating to v7 issues
First, you should not delete everything below line 64, then you will lose the database settings you need to migrate your database.
Second, what's supposed to go in here? "username:password" are easy, but whats my "host_name" shall the number stay the same? And what's my database name?
postgres://username:password@host_name:5432/database_name
And third, it's a little too easy to just tell us you don't support with database migration. The database is the most crucial thing of ones farmbot, once it's ruined, all the data of the plants is lost. Is like fucking up ones precious work with just a tiny update. There wasn't even a warning to consider backing up before updating. And as the previous versions were relatively unstable (at least for me) the temptation to update was pretty big. So please help me out now as I'm being trapped between the version :( And by helping me, helping all the others that will follow...
Sorry for my choice of words, I was a little aroused yesterday. The fear of losing my precious database and the worry of not being able to sort this out myself as the instructions are not explicit enough, was a little too much
@derletztename You have a few options:
Option 1: Stay on API v6, Don't Migrate
If you would rather not deal with data migration, you have the option to downgrade. This is the quickest solution.
- Pick a release that worked well for you in the past (v6.6.3 is the latest v7 version).
- Revert to that tag using Git.
Option 2: Upgrade the API, Use Old DB
- Set the
DATABASE_URLto your old database. Yourhostwill be your machines real IP address (notlocalhostor0.0.0.0). The database name is most likely something likefarmbot_production,farmbot_prodorfarmbot_development, depending on what yourRAILS_ENVwas set to. - You may need to configure PostgreSQL to allow remote connections. Please learn about how to secure a PostgreSQL database if you choose this option. I can't provide PostgreSQL training.
Option 3: Migrate old DB to Docker with pg_dump
- Create a backup using pg_dump. The command you want is probably
pg_dump -U postgres -h localhost -p 5432 farmbot_development > my_backup(orfarmbot_prodorfarmbot_production- it depends on your local setup). - Run the command below. You will need to change two things before you can copy/paste:
- Replace
password123with whatever yourPOSTGRES_PASSWORDvar is set to - Replace
farmbot_developmentwith the real DB name. IfRAILS_ENVisproduction, usefarmbot_prod. Otherwise, usefarmbot_development.
- Replace
sudo docker-compose run web psql -d postgres://postgres:password123@db/farmbot_development < my_backup
Other Notes
Ruby on Rails stores database info (password, username, host, etc) in Rails.configuration.database_configuration. You can access it from the Rails console sudo docker-compose run web rails c (v7) or via rails c (<= v6).
Thank you Rick for this far more detailed answer. I think the last one is the most sustainable one. And in this new more explicitly explained way I might be able to manage it. There is just a little issue directly in the beginning. When I want to create the backup I get asked for a password. In your notes you have mentioned even that somehow. And while reading here I learned that the database infos can either be stored in the config/database.yml or in ENV['DATABASE_URL']. So I tried setting the passwort in the database.yml which seems to change nothing at all. So you used the ways via ENV['DATABASE_URL']? And to change data in that I need to use Rails commands? I tried that as well, but man this is another level again. So concluding, could you maybe please explain if you have used the yml or the ENV['DATABASE_URL'] or both together and how to set the passwort then? Because in the instructions there are different ways for each configuration style
I think I managed to change the password with the database.yml, as following your commands there appears the password several times in the list. But still when trying to make that backup when I get asked for the password, I get an error saying that it's wrong. I think I always have used the development environment not production..
derletztename@permabot_server:~/farmbot-web-app$ rails c
W, [2018-10-24T09:14:34.643560 #3980] WARN -- Skylight: [SKYLIGHT] [3.0.0] Running Skylight in development mode. No data will be reported until you deploy your app.
(To disable this message for all local apps, run `skylight disable_dev_warning`.)
Loading development environment (Rails 5.2.1)
[1] pry(main)> Rails.configuration.database_configuration
=> {"default"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01"},
"development"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_development"},
"test"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_test"},
"production"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
:...skipping...
=> {"default"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01"},
"development"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_development"},
"test"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_test"},
"production"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_prod"}}
:...skipping...
=> {"default"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01"},
"development"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_development"},
"test"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_test"},
"production"=>
{"adapter"=>"postgresql",
"encoding"=>"unicode",
"host"=>"db",
"pool"=>5,
"username"=>"postgres",
"password"=>"permabot01",
"database"=>"farmbot_prod"}}
@derletztename Rails apps (not just Farmbot, but all Rails applications) allow multiple ways of database configuration, one via ENV vars and another via YML files. This is explained in the Rails documentation:
environment variable still takes precedence in cases of any conflicts [with YML files].
If there is still a conflict, you can always just delete database.yml or delete DATABASE_URL from your .env file, depending on the situation.
I'm really feeling stupid right now. I deleted the line in the .env file and changed the password in the database.yml and it really has changed if I look with the rails console (rails c), but still I get this error below. Where else could this password been set or what else could be the issue? I mean it clearly looks like being a password issue, but maybe there is something else behind?
derletztename@permabot_server:~/farmbot-web-app$ pg_dump -U postgres -h localhost -p 5432 farmbot_development > Paul_backup
Password:
pg_dump: [archiver (db)] connection to database "farmbot_development" failed: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
@derletztename: Try resetting your PostgreSQL password.
Go in to PostgreSQL:
$ sudo -u postgres psql
Then list all users:
psql (10.5 (Ubuntu 10.5-0ubuntu0.18.04))
Type "help" for help.
postgres=# \du
Which will show something like this:
List of roles
Role name | Attributes | Member of
--------------------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rick | Superuser | {}
You can change your password with:
postgres=# ALTER USER your_username_here with password 'whatever';
Please refer to StackOverflow and the official documentation for more information. Many of these operations are specific to Docker, PostgreSQL and Rails.
That's had done the trick for me, now the passwort is set :)
\password postgres
I guess I messed things up a little? After finally migrating the database as described I proceded the installing steps in the instructions:
# Install application specific Ruby dependencies
sudo docker-compose run web bundle install
# Install application specific Javascript deps
sudo docker-compose run web npm install
# Create a database in PostgreSQL
sudo docker-compose run web bundle exec rails db:create db:migrate
# Generate a set of *.pem files for data encryption
sudo docker-compose run web rake keys:generate # ⚠ SKIP THIS STEP IF UPGRADING!
# Build the UI assets via WebPack
sudo docker-compose run web npm run build
# Run the server! ٩(^‿^)۶
sudo docker-compose up
The Web App Server is running fine and I can log in, but now there are no more plants in the plant designer. But I did the backup with pg_dump as described before, so nothing is lost, right? Otherwise I might get a little heard attack :p
@derletztename The backup file is just SQL. Look for your points in there, you should see them if you backed up the correct database.
Yeah, everything seems to be in there, it's several pages long and I recognised my sequences and stuff.
Could you please tell me how to get the infos out of there into the new database?
Just migrate again with this Snippet?:
sudo docker-compose run web psql -d postgres://postgres:password123@db/farmbot_development < my_backup
So I tried to migrate it but I received a lot of errors. Here is a extract of the 3 blocks with errors. Could you please help me integrate the old database in the new one? I have no idea what's wrong and I have spend some long hours with an friend with a lot coding experience who couldn't figure it out as well :(
ALTER SEQUENCE
ERROR: relation "edge_nodes" already exists
ERROR: role "derletztename" does not exist
ERROR: relation "edge_nodes_id_seq" already exists
ERROR: role "derletztename" does not exist
ALTER SEQUENCE
ERROR: relation "farm_events" already exists
ERROR: role "derletztename" does not exist
ERROR: relation "farm_events_id_seq" already exists
ERROR: role "derletztename" does not exist
.
.
.
ALTER TABLE
ERROR: duplicate key value violates unique constraint "ar_internal_metadata_pkey"
DETAIL: Key (key)=(environment) already exists.
CONTEXT: COPY ar_internal_metadata, line 1
COPY 0
ERROR: duplicate key value violates unique constraint "devices_pkey"
DETAIL: Key (id)=(1) already exists.
CONTEXT: COPY devices, line 1
COPY 0
.
.
.
ERROR: multiple primary keys for table "ar_internal_metadata" are not allowed
ERROR: multiple primary keys for table "delayed_jobs" are not allowed
ERROR: multiple primary keys for table "devices" are not allowed
ERROR: multiple primary keys for table "diagnostic_dumps" are not allowed
ERROR: multiple primary keys for table "edge_nodes" are not allowed