reverse engineer mysql database to create django app - mysql

I basically want to take an existing mysql database structure created and used by a php app (codeigniter framework) and reverse engineer it to a django app. is there some tool to do this? south migrations maybe?

Create a project, and point your settings # your database
Then run
./manage.py inspectdb
This will print out a python models file for the DB you're pointing at
You can output this to a file by doing something like
./manage.py inspectdb > models.py
And then you can move the file to the most suitable location, and edit it as needed.

Related

Can I Delete migrations models django?

I'm creating my site with Django and MySQL (all are the latest versions), but my database plan was changed. now I want to edit my MySQL database. the project is still testing in fact I don't need any data from the database. I'm new to python, and Django with MySQL. please help with this.
thank you
Delete all files in migrations folder except __init__.py
Delete database file db.sqlite3
Execute python manage.py makemigrations
Execute python manage.py migrate
Now you should have new database file created and initial migrations applied for each application.
You can follow next steps if you don't need any data in your DB
Delete all migrations files (Save them just in case)
Create new database
Set new database in settings.py
makemigrations and migrate on new database

Django - recreate database from migration files | Sync production and development databases

My situation is the following:
I have a Django project in a production environment which uses a mysql database.
Also, I have a copy of the same Django project in a local environment which uses a mysql database too. What I want to do is set the following workflow to deploy database changes into production:
Make changes in local models.
Run makemigrations.
Run migrate.
Add, commit and upload the migration file to production.
Run simply "migrate" in production environment to update the
production database.
Actually, for some past reason both databases aren't completetly synced and for do this workflow, I need both databases works with same migration files and keep synced.
Any help on how to do that? How can i create this local database fully synced with production?
I tried the following things:
Create an empty local database and simply apply the "migrate" command.
Export the production database schema and create a local database based on it.
None of the above worked because when i try to run locally the django server, first he says that i have 100 migrations unaplied and, when i do the migrate says...
django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for []
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
in an app with no migrations; see https://docs.djangoproject.com/en/1.11/topics/migrations/#dependencies for more
Thank you so much.

How to import the data from a data dump (SqLite3) into django?

I have a data dump file(.sql) containing certain data, I would like to import it to my database(currently Sqlite3 but thinking about changing to MySQL) in order to use it in my test website.
Also, I need to know if it's possible to add the models too automatically, I presume it needs to be added manually, but any how, if there is any way to solve it, please suggest it.
There is a way to help you generate the Django models automatically given you have an existing database.
However this is a shortcut. You may then fine tune your models and add them to your app as needed.
Structuring your models in apps might force you to use the Models db_table meta option.
If at some point you would like to switch databases (Sqlite3 -> MySQL) you can export (dump) your current data to json. Then you could import (load) the data to the new database (after creating the database tables with migrate command). To do this you can use Django management commands:
Dump data
Load data
I was able to get an alternative answer after researching a bit.
Since I'm having a PostgreSQL data dump file with a file extension '.sql', I was capable of running a single command that imported the whole data dump into my local database, which is PostgreSQL. I'm using PgAdmin4 as my database management system and I installed psql during the installation of PgAdmin4, I added the psql to the path of my command prompt, hence it was accessible.
In order to import the data dump, I used the command provided below,
psql -U <username> -d <database_name> < <file.sql>
The '<' after database_name is necessary, so be sure to include it.
Here is the username of the configured account, is the database to which the data dump should be added, , is the file containing the data dump.

How to connect to a mysql database from Django?

I have an inscription.sql on a WAMP localhost:80 server, and I have also Django running on visual studio via a localhost:xxxx server.
I want to connect Django to the mysql database located on the wamp server (I have successfully installed MysqlClient).
Is there anyway to do this?
See the official guide to using an already existant database with Django.
To sum up, what you need to do is:
Make sure Django can connect to your database by setting the right DATABASE setting. From your comment, this seems to be the case.
Generate models (Python code) that match the tables in your database using the python manage.py inspectdb command. You must paste the generated code in the models.py file of an installed app and review them to make sure they fit your needs.
Create the tables required by Django with the python manage.py migrate command.
From the docs
Jus set the DATABASES variable in settings, then run python manage.py inspectdb > models.py put the generated models.py in the app's folder and finally run python manage.py migrate

importing sql file to mysql programmatically from a CloudBees deployed Java app

What is the best way to import a full .sql file (with DDL and DML sentences) to a mysql database from a Java application deployed in CloudBees?
Should I try to get the Runtime process and see if something like this works
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("mysql -p -h ServerName DbName < dump.sql");
(recommended solution in a previous question for a self-hosted environment, not sure if in a CloudBees hosted application I can execute process that access mysql)?
Is there a better solution? (again, to execute it from within the application, the .sql file to import will be provided by the user as part of his interaction with the web application).
I´d really like to avoid parsing the .sql file and sending the sentences one by one through jdbc.
I haven't tried this myself, but Flyway seems like it would let you import your SQL file during initialization of your app on CloudBees.
Flyway is an attempt to bring the popular Ruby concept of database migrations to Java. Flyway will let you place your .sql files inside the classpath of your app, and you can then use some Java code to update your database as needed.
Based on their migration docs, you should be able to place your .sql file as a file named V1__Initial_version.sql into a /db/migration/ directory on your classpath. You would then use something like the following code to trigger the migration when the app starts:
import com.googlecode.flyway.core.Flyway;
...
Flyway flyway = new Flyway();
flyway.setDataSource(...);
flyway.migrate();
I noticed that the Flyway FAQ explains that the database is locked during migrations, so this approach should work even if you scale out your application on CloudBees to use more than one instance (very nice!!).
Give it a try I guess. mysql cmd tools may or may not be on host. If they aren't we can probably add them.
The other option would be using a Jenkins job to do it. You could expose an api that jenkins calls and loads the database.