Restoring OSQA database to initial state - django

I installed OSQA using the bitnami installer and everything worked fine. Now, I am hacking at the osqa code base. If I need to restore the database to its initial state, do i need to reinstall OSQA or is there any command to truncate the database and load intial data.
I tried using use_osqa.bat and did a python.py manage migrate forum but it didnt work.
It uses a postgresql database

You can use the django-admin.py flush:
Returns the database to the state it was in immediately after syncdb was executed. This means that all data will be removed from the database, any post-synchronization handlers will be re-executed, and the initial_data fixture will be re-installed.
django-admin.py flush

Finally, this worked for me. Note that this is applicable only for OSQA using the bitnami environment with postgresql database.
if you want to restore your database state to the original version, first connect to postgres database using admin credentials(use any client, I used Dbeaver)
database : postgres
username : postgres
password : **admin password** : this is the same password you gave while installing bitnami-osqa
Now, drop the bitnami-osqa database and create it again(if you already have connections to bitnami_osqa, close them)
Drop-database bitnami_osqa;
Commit;
Create-database bitnami-osqa;
Commit;
Now open use_osqa.bat in your bitnami root folder and execute the following
cd apps\osqa
python manage.py syncdb --all --verbosity 2
[specify yes when it prompts to create super user and create some user]
python manage.py migrate forum --fake

Related

Django Migrations not updating Heroku PostgreSQL database

I've been stuck on an issue for a while where I have a model that I've created locally (class User(AbstractUser)), ran "python manage.py makemigrations" and "python manage.py migrate", which works and interacts correctly with the django server when run locally.
When I push this to heroku however, I receive an error response to certain api requests which essentially say "relation "app_user" does not exist". I assumed this was because somehow the postgreSQL db didn't receive the migrations when the app was deployed to heroku. After logging into the db I noticed that it had a few other tables, including "auth_user" (I assume this is a default django table), but not "app_user". I have done "heroku run bash" and "python manage.py migrate" in an attempt to migrate the changes to the postgreSQL db, but this doesn't have any effect.
I'm not sure if this will be useful but the postgreSQL server I'm using is the standard Heroku Postgres Hobby Dev add-on.
My questions are:
Should running "python manage.py migrate" on heroku update the postgreSQL database? If so, how does this work?
Given that the migrations are all on heroku, how can I manually update the postgreSQL with the migrations?
Fixed by deleting old heroku db and creating a new one. Unsure what exactly caused the issue above but I feel it may be because we deleted all migrations on the repo at some point in the past (to start from scratch), which meant the current db state was not applicable to migrations in the repo and so could not be applied. After doing this, running migrate on the remote server works perfectly.

how to reflect changes made on postgres database of django from one pc to other

We three working with Django and postgres is the database. Whenever we push the code to GitHub. The database data is not reflecting. The data I stored is visible to me only. The postgres user, password and database name are same on all our laptops. How to make when I push that has to go their databases also.
If you are talking about changes in db schema, then take a look django migrations - https://docs.djangoproject.com/en/3.1/topics/migrations/. The workflow is following:
change model (e.g. add new field, change existing field...)
generate migration file by running python manage.py makemigrations. This generates migration file in <app_folder>/migrations
Run python manage.py migrate to apply changes in models to your database
Add migration file to your version control (github or whaever) and push it
Now when your colleagues gets updated code from version control they need to run python manage.py migrate to apply model changes to their local database.
I found that it is not possible to send postgress data directly. But sqlite can be used for it. This link aids in that - https://manuelvanrijn.nl/blog/2012/01/18/convert-postgresql-to-sqlite/ . But i send data by taking backup like
From Postgres to Postgres: taking backup :
pg_dump dbname > dbname.bak
and then on new Postgres restored with:
psql test < dbname.bak

Django's --fake-initial doesn't work when migrating with existing tables

I am migrating a project from Django 1.1 to Django 3.0 I am done with the project. When I am dumping the production dump to my local in the newly converted project I get "Table already exists".
Here's what I am doing.
mysql> create database xyx;
docker exec -i <container-hash> mysql -u<user> -p<password> xyx < dbdump.sql
then I run the migrate, as I have had to do some changes to the previously given models.
./manage.py migrate --fake-initial
this is the output I get
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1050, "Table 'city' already exists")
So, what to do ?
Alright boys and girls, here's the approach I followed to solve this problem.
I dumped the entire database.
docker exec -i <container-hash> mysql -u<username> -p<password> <dbname> < dump.sql
Now I listed all the migrations I made using
./manage.py showmigrations <app-name>
This will give me the list of all the migrations I have applied, now from inspecting the migrations, I realized that from the 7th migration to the 30th migration I had done my changes.
Here's the tedious part which any sys admin can write a script to do in less than 4 lines of bash script. You can generate the raw SQL of any migration with this command.
./manage.py sqlmigrate <app-name> <migration-name> > changes-i-made.sql
Now that I have created my changes-i-made.sql file I'll need to run this script 22 more times but with >> otherwise everytime you run the command with a single > it will keep overwriting your changes file.
Now once all of your migration changes are recorded inside a file, open up your sql shell connect to the database and start pasting the changes or do some sql magic to pick all the changes directly from the file.
Once you're done go ahead and fake all the migrations, cause you don't need Django to do them you already did.
./manage.py migrate --fake
and then login to your production instance and get ready to fuck with your senior team lead who said you couldn't do it.
I just checked to see if this approach is working and the future migrations will be working, so I created one and everything works like a breeze.

How to sync local Django sqlite3 data with Heroku's postgres database?

I have a Django website served by Heroku with a model storing data about projects that I've worked on in the past. I already ran makemigrations and migrate locally before pushing to heroku with git and running heroku run python3 manage.py migrate. So, my database models and fields are synced, but I'm asking about the field values. Whenever I update the value of a field for a model instance locally, I want it (the data) to sync with Heroku, and vice versa–sync values I've updated on Heroku in the admin panel with my local sqlite3 database. Is there a command for updating the values of the database itself, or am I missing something? Because I've looked all over the internet for the last hour for how to do this one thing and I couldn't find the command to do it.
Side note: I also want the command I'm looking for to sync newly created instances, not just data for existing model instances.
Ok, I've figured it out.
First step is to run python3 manage.py dumpdata --exclude contenttypes > data.json. This copies the local database data into a file called data.json (that's created by > also if it doesn't exist).
Next, git push to heroku and run heroku run python3 manage.py migrate for good measure.
Finally, heroku run python3 manage.py loaddata data.json. This translates the data loaded from the local sqlite3 database and loads it to the heroku postgre database. Unless the translating is done when you dump the data. Regardless, this synchronizes the heroku data with the local data.
I haven't tested synchronizing the local data with the heroku data, but I'm sure it'll work the same way: heroku run python3 manage.py dumpdata --exclude contenttypes > data.json and then git fetch from heroku (I've never fetched before to sync a directory with what's on github but it should be straightforward).
That's all there is to it. If I locally change the name of a project that I worked on, update the date it was last worked on, and write a few more paragraphs on the work process, and I don't want to redo all of that in the heroku shell, then I just synchronize by dumping the data, pushing it to heroku, and loading it there.

Migrate Django development database (.sql3) to Heroku

How does one migrate their Django .sql3 development database to heroku?
Per here, and here I tried: heroku pg:psql --app sblic < database.sql3 but my Django admin shows no new uploads (even after syncdb/migrate/ or collectstatic
Perhaps there may be a way to directly upload an sql3 file to Heroku, but I went with the path of clearest certainty (convert local sql3 db to postgre db and upload a dump of postgre db to Heroku via pgbackups tool):
Create a dump of your sql3 database as a json file
With PostgreSql installed and with its bin directory in the Path environment variable, create a postgre user and database (or just plan on using your initial super user account created upon installing postgresql)
Update your settings.py with a reference to your newly created postgre database (note, 'HOST' may need to be set as 'localhost', 'user' is your postgre user login)
run python manage.py syncdb to initiate your new postgre db
Optional: if necessary, truncate your postgre db of contenttypes
load your dump from step 1 (if you have fixture issues, see here)
Now you have a working postgre local db. To migrate, first create a postgre dump
Post your postgre dump somewhere accessible by URL (such as drop box or amazon s3 as suggested in previous link).
Perform pgbackups restore command, referencing your dump url
Your heroku app should now reference the contents of your local db.
Heroku command line tool uses the psql binary. You have to install PostgreSQL on your local development machine to have psql available. From the (documentation)[https://devcenter.heroku.com/articles/heroku-postgresql#pg-psql]:
You must have PostgreSQL installed on your system to use heroku pg:psql.
You can in fact keep using your SQLite database with Heroku, but this is not recommended as it will be rewritten with your local copy if you re-deploy it to another dyno. Migrating the data to psql is recommended as described at https://devcenter.heroku.com/articles/sqlite3