I have a strnage problem.
My django project has myapp module/application. My project uses south to do the schema migrations.
On localhost i have run ./manage.py schemamigration myapp --initial, then i have run migrate command.
But when in production environment i execute migrate command, this doesn't create the correponding table (of myapp models) in database.
It's strange because if i execute migrate --list, myapp has to migration and they are all marked (with * symbol ).
So, i'm thinking about deleting myapp and recreating it from scratch (with corresponding migrations). Is there better solution?
EDIT:
i have tried to delete myapp and to recreate it from scratch. So i have also delete tables of myapp in database (on localhost and on production server), and after all i have executed:
schemamigration myapp --initial command on localhost
migrate myapp command on localhost
migrate myapp 0001 --fake on production server
but South continues to not create the tables of myapp in database of production server.
If you accidentally or intentionally dropped a table in your DB and your are trying to run ./manage migrate myappThis will not create the dropped table in your DB.
Because South does not touch base with your DB.
In case you want to re-create your table. Migrate your schema to a previous version and migrate it latest. Please use the below code accordingly
manage.py migrate myapp 0002 --fake
manage.py migrate myapp
note: 002 is your previous migration version.
if you deleted your tables you shouldn't be running --fake unless you did a manage.py syncdb first. With no table, you should be able to run python manage.py migrate myapp and be done with it (or a manage.py syncdb). The first migration created by --initial has create table statements in it.
--fake explicitly tells south to not do anything but pretend it migrated (performed DB changes) and mark the history table as such.
I know its a bit late, but had this same problem and I found that the problem was that my manage.py was pointing to the wrong settings file hence wrong DB. Ensure your manage.py is pointing to the correct settings file and migrations are being made to the correct DB. This can arise if you are using multiple manage.py files or multiple settings files.
Related
I am using sqlite as my database for an offline app which is made in electron.
For creating the database, I was using knex migrations.
Problem is, it will run fine in development, i would migrate the database and start the electron process.
But while packaging the app for production build, I need the migrations to run on the client machine on the first start up. So that the database would be created and when there is an application update, a new migration would keep the database updated.
What is the appropriate approach for this. How do i run the migrations on app start up, or how do i keep the migrations in the bundle.
Won't all the code be kept in app.asar? Will the migration code be run from there?
Also, where should the database be created in the client machine?
If you are using electron builder then you can add this to the electron-builder.json
"extraFiles": "migrations/*",
where migrations is the folder where you keep the migrations.
To migrate it automatically on running
you can add the following code
const client = knex(config[env]);
client.migrate.latest(config);
First of, I want to say, that I am not a DB expert and I have no experience with the heroku service.
I want to deploy a play framework application to the heroku service. And I need a database to do so. So I created a postgresql database with this command, since it's supported by heroku:
Users-MacBook-Air:~ user$ heroku addons:create heroku-postgresql -a name_of_app
And I got this as response
Creating heroku-postgresql on ⬢ benchmarkingsoccerclubs... free
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
So the DB is now existing but empty of course. For development I worked with a local H2 Database.
Now I would want to populate the DB on heroku using a sql file, since it's quite a lot of data. But I couldn't find how to do that. Is there a command for the heroku CLI, where I can hand over the sql file as an argument and it populates the database? The File basically consists of a few tables which get created and around 10000 Insert commands.
EDIT: I also have CSV files from all the tables. So if there is a way how I can populate the Postgres DB with those would be also great
First, run the following to get your database's name
heroku pg:info --app <name_of_app>
In the output, note the value of "Add-on", which should look something like this:
Add-on: postgresql-angular-12345
Then, issue the following command:
heroku pg:psql <Add-on> --app <name_of_app> < my_sql_file.sql
For example (assuming your sql commands are in file test.sql):
heroku pg:psql postgresql-angular-12345 --app my_cool_app < test.sql
I created the Models of my database on VS for Mac and used terminal for create migrations:
dotnet ef migrations add IntitialMigration
Then, I updated my database:
dotnet ef database update
But after I changed my Models and I created another migration:
dotnet ef migrations add SecondMigration
And tried to update the database:
dotnet ef database update SecondMigration
I received the following error:
There is already an object named 'Emails' in the database.
I've searched and I found a probably solution:
Add-Migration SecondMigration -IgnoreChanges
But this only works at PMC on Windows. I'm using VS for Mac and all the commands are typed on Terminal. Does anybody know how to update a database using Migrations after change the Models?
I found the solution. It seems that on VS 2017 for Windows, when you add a Migration, it is automatically added on the solution. But, on VS for Mac, that doesn't happen. You need to manually add the each new migration to the solution. I created the InitialMigration migration and update it to the database. For some reason I don't know why, I removed the migration from the solution and I created the new SecondMigration migration. That's the reason that all the tables that were on InitialMigration were on SecondMigration too.
If you just run update database it will also try to update with previous migration. (which is why its complains about adding whats already there). You can run an update just for one single migration by adding the name of the migration you want to run after the command.
I have a VS2015 database project (sqlproj) and I created a lot of test data. I added a parameter to the PostDeploymentScript.sql file and when I need an empty database, I set it false and when I publish it doesn't include test data. When I need a demo database I set it true and when I publish, it also adds test data after deployment.
On the other hand, I want to create two different DACPAC files to prevent manual process and build both of them automatically at once. I searched a little bit and found several articles like this:
http://www.techrepublic.com/blog/data-center/auto-deploy-and-version-your-sql-server-database-with-ssdt/
but I couldn't apply what he said. What am I missing?
I created an (almost) empty database project (Lets say Base.sqlproj) which adds lookup table data after deployment. I created another DB project (Base_Plus_TestData.sqlproj) and added a database reference for the first database.
What I need is, if client needs to deploy empty database I'd like to give them the Base.DACPAC. If client needs to deploy a demo database with test data, I want to give them Base_Plus_TestData.DACPAC.
What should I do for this purpose and what am I doing wrong?
There a couple of extra options over what you already do with a switch to include data, I would choose the first :)
1 - Just give customers who want demo data a script to run after deploying the database (you could do something like use a powershell script/.net app to deploy your data and optionally the data)
2 - The post deploy script can be edited in a dacpac, you could build your project, copy the dacpac and then edit the post deploy script to include your data on one of the dacpacs.
3 - Create a separate ssdt project that references your main database project with a "same database" reference and the extra post deploy script - wheb you build you will get two dacpacs you can deploy either together if you want data or just the database.
If you also have data in your original dacpac to deploy you will need to copy it into the "with data" dacpac.
Ed
I'm looking for the updated Django 1.5 command that can do the following action.
python manage.py reset <app>
What I want to do basically is DROP tables and UPDATE the database structure inside with a manage.py command.
The thing is that reset command is no longer working and
manage.py flush
or
manage.py sqlclear <app>
are just dropping the database / table content.
What's the updated reset version for Django 1.5?
I think what you are looking for is South. South is a 3rd party tool, which may soon be integrated into Django, that assists you with database migrations and schema changes. As is stands, Django 1.5 does not deal with schema changes very well, if at all. The only way to adjust a schema in Django 1.5 is to add new models. You wouldn't want to engage in the practice of adding a new model to fulfill a desired table alteration or deletion. Most developers turn to a 3rd part solution when they need to make schema adjustments.
See http://south.readthedocs.org/en/latest/about.html
See Tutorial http://south.readthedocs.org/en/latest/tutorial/part1.html#tutorial-part-1
South is database agnostic and deals with database migrations automatically for you. So if you change your schema it will detect it in models.py and make the appropriate changes. You can include South as an app to your Django project and install it through pip
Hope this helps
As a MySQL alternative, you can create a application.py next to manage.py with the following code.
This will DROP, CREATE, and UPDATE your database with your models.py
#!/usr/bin/python
import MySQLdb
import subprocess
dbname = "mydbname"
db = MySQLdb.connect(host="127.0.0.1", user="username", passwd="superpassword", db=dbname)
cur = db.cursor()
#Drop all database to Drop all tables
cur.execute("DROP DATABASE "+dbname)
#Recreate the DB
cur.execute("CREATE DATABASE "+dbname)
#Sync with manage.py
proc = subprocess.call(['python','manage.py','syncdb'])
print "\n\nFinished!"